link.inc 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. <?php
  2. /**
  3. * @file
  4. * On behalf implementation of Feeds mapping API for link.module.
  5. */
  6. /**
  7. * Implements hook_feeds_processor_targets().
  8. */
  9. function link_feeds_processor_targets($entity_type, $bundle_name) {
  10. $targets = array();
  11. foreach (field_info_instances($entity_type, $bundle_name) as $name => $instance) {
  12. $info = field_info_field($name);
  13. if ($info['type'] == 'link_field') {
  14. if (array_key_exists('url', $info['columns'])) {
  15. $targets[$name . ':url'] = array(
  16. 'name' => t('@name: URL', array('@name' => $instance['label'])),
  17. 'callback' => 'link_feeds_set_target',
  18. 'description' => t('The @label field of the entity.', array('@label' => $instance['label'])),
  19. 'real_target' => $name,
  20. );
  21. }
  22. if (array_key_exists('title', $info['columns'])) {
  23. $targets[$name . ':title'] = array(
  24. 'name' => t('@name: Title', array('@name' => $instance['label'])),
  25. 'callback' => 'link_feeds_set_target',
  26. 'description' => t('The @label field of the entity.', array('@label' => $instance['label'])),
  27. 'real_target' => $name,
  28. );
  29. }
  30. }
  31. }
  32. return $targets;
  33. }
  34. /**
  35. * Callback for mapping link fields.
  36. */
  37. function link_feeds_set_target(FeedsSource $source, $entity, $target, array $values, array $mapping) {
  38. $language = $mapping['language'];
  39. list($field_name, $column) = explode(':', $target);
  40. $field = isset($entity->$field_name) ? $entity->$field_name : array($language => array());
  41. $delta = 0;
  42. foreach ($values as $value) {
  43. if (is_object($value) && ($value instanceof FeedsElement)) {
  44. $value = $value->getValue();
  45. }
  46. if (is_scalar($value)) {
  47. $field[$language][$delta][$column] = (string) $value;
  48. }
  49. $delta++;
  50. }
  51. $entity->$field_name = $field;
  52. }