video_embed_field.feeds.inc 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. <?php
  2. /**
  3. * @file
  4. * On behalf implementation of Feeds mapping API for video_embed_field.module.
  5. */
  6. /**
  7. * Implements hook_feeds_processor_targets_alter().
  8. *
  9. * @see FeedsNodeProcessor::getMappingTargets()
  10. */
  11. function video_embed_field_feeds_processor_targets_alter(&$targets, $entity_type, $bundle_name) {
  12. foreach (field_info_instances($entity_type, $bundle_name) as $name => $instance) {
  13. $info = field_info_field($name);
  14. if ($info['type'] == 'video_embed_field') {
  15. $targets[$name . ":video_url"] = array(
  16. 'name' => t('@name: Embed URL', array('@name' => $instance['label'])),
  17. 'callback' => 'video_embed_field_set_target',
  18. 'description' => t('The URL for the @label field of the @entity_type.', array('@entity_type' => $entity_type, '@label' => $instance['label'])),
  19. 'real_target' => $name,
  20. );
  21. $targets[$name . ':description'] = array(
  22. 'name' => t('@name: Embed description', array('@name' => $instance['label'])),
  23. 'callback' => 'video_embed_field_set_target',
  24. 'description' => t('The description for the @label field of the @entity_type.', array('@entity_type' => $entity_type, '@label' => $instance['label'])),
  25. 'real_target' => $name,
  26. );
  27. }
  28. }
  29. }
  30. /**
  31. * Callback for mapping. Here is where the actual mapping happens.
  32. *
  33. * When the callback is invoked, $target contains the name of the field the
  34. * user has decided to map to and $value contains the value of the feed item
  35. * element the user has picked as a source.
  36. */
  37. function video_embed_field_set_target($source, $entity, $target, $value) {
  38. if (empty($value)) {
  39. return;
  40. }
  41. if (!is_array($value)) {
  42. $value = array($value);
  43. }
  44. list($field_name, $sub_field) = explode(':', $target, 2);
  45. $info = field_info_field($field_name);
  46. // Iterate over all values.
  47. $field = isset($entity->$field_name) ? $entity->$field_name : array(LANGUAGE_NONE => array());
  48. // Allow for multiple mappings to the same target.
  49. $count = call_user_func_array('array_merge_recursive', $field[LANGUAGE_NONE]);
  50. $delta = count($count[$sub_field]);
  51. foreach ($value as $v) {
  52. if ($info['cardinality'] != FIELD_CARDINALITY_UNLIMITED && $info['cardinality'] <= $delta) {
  53. break;
  54. }
  55. if (is_scalar($v)) {
  56. $field[LANGUAGE_NONE][$delta][$sub_field] = $v;
  57. $delta++;
  58. }
  59. }
  60. $entity->{$field_name} = $field;
  61. }