link.inc 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. <?php
  2. /**
  3. * @file
  4. * On behalf implementation of Feeds mapping API for link.module.
  5. */
  6. /**
  7. * Implements hook_feeds_processor_targets_alter().
  8. *
  9. * @see FeedsProcessor::getMappingTargets()
  10. */
  11. function link_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'] == 'link_field') {
  15. if (array_key_exists('url', $info['columns'])) {
  16. $targets[$name . ':url'] = array(
  17. 'name' => t('@name: URL', array('@name' => $instance['label'])),
  18. 'callback' => 'link_feeds_set_target',
  19. 'description' => t('The @label field of the entity.', array('@label' => $instance['label'])),
  20. 'real_target' => $name,
  21. );
  22. }
  23. if (array_key_exists('title', $info['columns'])) {
  24. $targets[$name . ':title'] = array(
  25. 'name' => t('@name: Title', array('@name' => $instance['label'])),
  26. 'callback' => 'link_feeds_set_target',
  27. 'description' => t('The @label field of the entity.', array('@label' => $instance['label'])),
  28. 'real_target' => $name,
  29. );
  30. }
  31. }
  32. }
  33. }
  34. /**
  35. * Callback for mapping. Here is where the actual mapping happens.
  36. *
  37. * When the callback is invoked, $target contains the name of the field the
  38. * user has decided to map to and $value contains the value of the feed item
  39. * element the user has picked as a source.
  40. */
  41. function link_feeds_set_target($source, $entity, $target, $value) {
  42. if (empty($value)) {
  43. return;
  44. }
  45. // Handle non-multiple value fields.
  46. if (!is_array($value)) {
  47. $value = array($value);
  48. }
  49. // Iterate over all values.
  50. list($field_name, $column) = explode(':', $target);
  51. $info = field_info_field($field_name);
  52. $field = isset($entity->$field_name) ? $entity->$field_name : array();
  53. $delta = 0;
  54. foreach ($value as $v) {
  55. if ($info['cardinality'] == $delta) {
  56. break;
  57. }
  58. if (is_object($v) && ($v instanceof FeedsElement)) {
  59. $v = $v->getValue();
  60. }
  61. if (is_scalar($v)) {
  62. $field['und'][$delta][$column] = $v;
  63. $delta++;
  64. }
  65. }
  66. $entity->$field_name = $field;
  67. }