Last active
August 29, 2015 14:05
-
-
Save manumilou/965733d985ef7919e11c to your computer and use it in GitHub Desktop.
Creating a custom target for mapping
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Creating a new custom target allows to alter the data source before the mapping. For that, the hook hook_feeds_node_processor_targets_alter() needs to be implemented. There, you can specify a target name and description, a function callback that will process the data source, and a real target, corresponding to an actuel field of the content type. | |
After flushing the cache, a new option should be available in the target dropdown. | |
Example: | |
/** | |
* Implementation of hook_feeds_node_processor_targets_alter(). | |
*/ | |
function feeds_library_feeds_processor_targets_alter(&$targets, $entity_type, $bundle_name){ | |
$targets['field_category_custom'] = array( | |
'name' => t('Sujet des Articles'), | |
'description' => t('Map le sujet des articles pour chaque parution'), | |
'callback' => 'feeds_library_set_target', | |
'real_target' => 'field_sujet_pour_document', | |
); | |
} | |
function feeds_library_set_target($source, $entity, $target, $value, $mapping) { | |
// Use mapping table to fetch | |
$result = db_query("SELECT m.sujet, t.tid FROM {sujets_sans_cote} m | |
LEFT JOIN {taxonomy_term_data} t ON t.name = m.sujet | |
WHERE m.code = :code", array(':code' => trim($value)) | |
); | |
$record = $result->fetchObject(); | |
if($record) { | |
$wrapper = entity_metadata_wrapper('node', $entity); | |
$wrapper->field_sujet_pour_document->set($record->tid); | |
} else { | |
// Log this | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment