我正在自定义模块中触发提要导入。我需要在该代码中设置一个空白源代码的值,因为该值不在源文件中(在本例中是xml)。有可能这样做吗?
下面是我的代码。
$path = $url . '/path_to_my_file/myfile.xml';
$feeds_source = feeds_source($feeds_importer);
$feeds_config = $feeds_source->getConfigFor($feeds_source->importer->fetcher);
$file_exists = get_headers($path, 1);
# check if file exists:
if ($file_exists[0] == 'HTTP/1.1 200 OK') {
$feeds_config['source'] = $path;
$config = array(
'process_in_background' => TRUE,
);
$feeds_source->setConfigFor($feeds_source->importer->fetcher, $feeds_config);
$feeds_source->importer->addConfig($config);
$feeds_source->save();
$feeds_source->startImport();
while (FEEDS_BATCH_COMPLETE != $feeds_source->import());
}<#>更新:
下面是一个更完整的代码。我有三个功能。第一个是原始的,但一个。注意,我从函数参数中获得了$VALUETOPASS变量。
另外两个函数是rooby建议的函数。问题是如何在最后一个函数中使用$VALUETOPASS。现在我只需要那个。
function MYMODULE_CustomImport($VALUETOPASS) {
$path = $url . '/path_to_my_file/myfile.xml';
$feeds_source = feeds_source($feeds_importer);
$feeds_config = $feeds_source->getConfigFor($feeds_source->importer->fetcher);
$file_exists = get_headers($path, 1);
# check if file exists:
if ($file_exists[0] == 'HTTP/1.1 200 OK') {
$feeds_config['source'] = $path;
$config = array(
'process_in_background' => TRUE,
);
$feeds_source->setConfigFor($feeds_source->importer->fetcher, $feeds_config);
$feeds_source->importer->addConfig($config);
$feeds_source->save();
$feeds_source->startImport();
while (FEEDS_BATCH_COMPLETE != $feeds_source->import());
}
function MYMODULE_feeds_processor_targets_alter(array &$targets, $entity_type, $bundle) {
// Set a custom value setter callback for the my field on my node type.
if ($entity_type == 'node' && $bundle == 'my_bundle') {
if (isset($targets['my_field'])) {
$targets['my_field']['callback'] = 'MYMODULE_set_custom_field_value';
}
}
}
function MYMODULE_set_custom_field_value(FeedsSource $source, $entity, $target, array $values, array $mapping) {
$xml = simplexml_load_file($url . $VALUETOPASS . '/api/xml');
$multijob_array = json_decode(json_encode((array)$xml), TRUE);
if (isset($multijob_array['action'][2]['cause']['upstreamBuild'])) {
$multijob_build = $multijob_array['action'][2]['cause']['upstreamBuild'];
} else {
$multijob_build = '';
}
$entity->{$target}[$entity->language][0]['value'] = $value;
}发布于 2018-01-29 14:13:42
在运行导入程序的代码中,只能更改字段映射。
若要在此修改字段映射配置以添加自定义数据(您还可以从此处的映射中删除字段):
// Load the importer.
$importer_id = 'my_importer';
$feeds_source = feeds_source($importer_id);
// Get the current field mappings.
$field_mappings = $feeds_source->importer->processor->getMappings();
foreach ($field_mappings as &$mapping) {
// Add an arbitrary value to the mapping of my_field for
// use later in the processor target callback.
if ($mapping['target'] == 'my_field') {
// Your logic goes here to populate this.
$mapping['my_custom_value'] = 'My custom value';
}
}
// Set the new field mappings.
$feeds_source->importer->processor->addConfig(array('mappings' => $field_mappings));
// Save the new field mappings.
$feeds_source->save();
// The previous call to FeedsProcessor::getMappings() cached
// the mapping, so we need to clear that before we run the import.
drupal_static_reset('FeedsProcessor::getMappings');
// Run the import.
$feeds_source->startImport();
while (FEEDS_BATCH_COMPLETE != $feeds_source->import());然后,要更改字段中的实际值,您必须在导入时这样做,当feed从源获取值并将其保存到目标时。
为此,您可以实现hook_feeds_processor_targets_alter(),如下所示:
/**
* Implements hook_feeds_processor_targets_alter().
*/
function my_module_feeds_processor_targets_alter(array &$targets, $entity_type, $bundle) {
// Set a custom value setter callback for the my field on my node type.
if ($entity_type == 'node' && $bundle == 'my_node_type') {
if (isset($targets['my_field'])) {
$targets['my_field']['callback'] = 'my_module_set_custom_field_value';
}
}
}
/**
* Custom feeds target setter callback.
*/
function my_module_set_custom_field_value(FeedsSource $source, $entity, $target, array $values, array $mapping) {
// Set the field value to our custom value if it's there.
if (!empty($mapping['my_custom_value'])) {
$entity->{$target}[$entity->language][0]['value'] = $mapping['my_custom_value'];
}
}https://drupal.stackexchange.com/questions/254661
复制相似问题