metatag.feeds.inc 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. <?php
  2. /**
  3. * @file
  4. * Feeds mapping implementation for the Metatag module.
  5. */
  6. /**
  7. * Implements hook_feeds_processor_targets_alter().
  8. */
  9. function metatag_feeds_processor_targets_alter(&$targets, $entity_type, $bundle) {
  10. if (metatag_entity_supports_metatags($entity_type)) {
  11. $info = metatag_get_info();
  12. foreach ($info['tags'] as $name => $tag) {
  13. $targets['meta_' . $name] = array(
  14. 'name' => t('Meta tag: !label', array('!label' => $tag['label'])),
  15. 'callback' => 'metatag_feeds_set_target',
  16. 'description' => $tag['description'],
  17. );
  18. }
  19. }
  20. }
  21. /**
  22. * Callback function to set value of a metatag tag.
  23. */
  24. function metatag_feeds_set_target($source, $entity, $target, $values) {
  25. // Don't do anything if we weren't given any data.
  26. if (empty($values)) {
  27. return;
  28. }
  29. // The Feed API was changed so that the $values are passed as an array rather
  30. // than a single value. For backwards compatibility, support both.
  31. if (!is_array($values)) {
  32. $values = array($values);
  33. }
  34. // Strip the prefix that was added above.
  35. $name = str_replace('meta_', '', $target);
  36. // Identify the type of entity being imported.
  37. $entity_type = $entity->feeds_item->entity_type;
  38. // Check for any existing data that may not have been loaded already.
  39. if (!isset($entity->metatags) && !empty($entity->feeds_item->entity_id) && is_numeric($entity->feeds_item->entity_id)) {
  40. $entity->metatags = array();
  41. $entity_id = $entity->feeds_item->entity_id;
  42. // Load the existing entity.
  43. $stored_entities = entity_load($entity_type, array($entity_id));
  44. if (!empty($stored_entities)) {
  45. $stored_entity = reset($stored_entities);
  46. if (!empty($stored_entity)) {
  47. // Only load meta tags for the current revision.
  48. list(, $revision_id) = entity_extract_ids($entity_type, $stored_entity);
  49. // This function returns all values for all revisions.
  50. $metatags = metatag_metatags_load($entity_type, $entity_id, $revision_id);
  51. if (!empty($metatags)) {
  52. if (!empty($metatags[$revision_id])) {
  53. // Just copy all meta tags for all languages.
  54. $entity->metatags = $metatags[$revision_id];
  55. }
  56. }
  57. }
  58. }
  59. }
  60. // Assign the value.
  61. $langcode = metatag_entity_get_language($entity_type, $entity);
  62. $entity->metatags[$langcode][$name]['value'] = $values[0];
  63. }