Created
May 27, 2016 06:48
-
-
Save adamjakab/7291d1bef20c08863d63a9f9c125bb08 to your computer and use it in GitHub Desktop.
basic implementatio to handle barcode_field in feeds module
This file contains hidden or 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
<?php | |
/** | |
* Enable barcode field handling for Feeds | |
* | |
* Implements hook_feeds_processor_targets_alter(). | |
* @param array &$targets | |
* Array containing the targets to be offered to the user. Add to this array | |
* to expose additional options. | |
* @param string $entity_type | |
* The entity type of the target, for instance a 'node' entity. | |
* @param string $bundle | |
* The entity bundle to return targets for. | |
*/ | |
function mekit_barcode_feeds_processor_feeds_processor_targets_alter(array &$targets, $entity_type, $bundle) { | |
foreach (field_info_instances($entity_type, $bundle) as $fieldName => $fieldInstance) { | |
$fieldInfo = field_info_field($fieldName); | |
if ($fieldInfo['type'] == 'barcode_field') { | |
$targets[$fieldName] = [ | |
'name' => check_plain($fieldInstance["label"]), | |
'callback' => 'mekit_barcode_feeds_processor_target', | |
'description' => t('The @label field of the node.', array('@label' => $fieldInstance['label'])), | |
]; | |
} | |
} | |
} | |
/** | |
* Handle how to import data | |
* | |
* @param \FeedsSource $source | |
* @param \stdClass $entity | |
* @param string $target | |
* @param array $values | |
* @param array $mapping | |
*/ | |
function mekit_barcode_feeds_processor_target(\FeedsSource $source, $entity, $target, array $values, array $mapping) { | |
$field_name = $target; | |
$field = isset($entity->$field_name) ? $entity->$field_name : ['und' => []]; | |
$fieldInfo = field_info_field($field_name); | |
// Iterate over all values. | |
$delta = 0; | |
foreach ($values as $value) { | |
//do validation here | |
$field[LANGUAGE_NONE][$delta]['value'] = $value; | |
$delta++; | |
} | |
$entity->$field_name = $field; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment