metatag.feeds.inc 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  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' => 'Meta tag: ' . check_plain($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, $value) {
  25. // Don't do anything if we weren't given any data.
  26. if (empty($value)) {
  27. return;
  28. }
  29. // Strip the prefix that was added above.
  30. $name = str_replace('meta_', '', $target);
  31. // Check for any existing data that may not have been loaded already.
  32. if (!isset($entity->metatags) && !empty($entity->feeds_item->entity_id) && is_numeric($entity->feeds_item->entity_id)) {
  33. $entity->metatags = array();
  34. $entity_type = $entity->feeds_item->entity_type;
  35. $entity_id = $entity->feeds_item->entity_id;
  36. // Load the existing entity.
  37. $stored_entities = entity_load($entity_type, array($entity_id));
  38. if (!empty($stored_entities)) {
  39. $stored_entity = reset($stored_entities);
  40. if (!empty($stored_entity)) {
  41. // This function returns all values for all revisions.
  42. $metatags = metatag_metatags_load($entity_type, $entity_id);
  43. if (!empty($metatags)) {
  44. // Only load meta tags for the current revision.
  45. list(, $revision_id) = entity_extract_ids($entity_type, $stored_entity);
  46. if (!empty($metatags[$revision_id])) {
  47. // Just copy all meta tags for all languages.
  48. $entity->metatags = $metatags[$revision_id];
  49. }
  50. }
  51. }
  52. }
  53. }
  54. // Assign the value.
  55. $langcode = metatag_entity_get_language($entity_type, $entity);
  56. $entity->metatags[$langcode][$name]['value'] = $value;
  57. }