metatag.feeds.inc 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  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. // Identify the type of entity being imported.
  32. $entity_type = $entity->feeds_item->entity_type;
  33. // Check for any existing data that may not have been loaded already.
  34. if (!isset($entity->metatags) && !empty($entity->feeds_item->entity_id) && is_numeric($entity->feeds_item->entity_id)) {
  35. $entity->metatags = array();
  36. $entity_id = $entity->feeds_item->entity_id;
  37. // Load the existing entity.
  38. $stored_entities = entity_load($entity_type, array($entity_id));
  39. if (!empty($stored_entities)) {
  40. $stored_entity = reset($stored_entities);
  41. if (!empty($stored_entity)) {
  42. // This function returns all values for all revisions.
  43. $metatags = metatag_metatags_load($entity_type, $entity_id);
  44. if (!empty($metatags)) {
  45. // Only load meta tags for the current revision.
  46. list(, $revision_id) = entity_extract_ids($entity_type, $stored_entity);
  47. if (!empty($metatags[$revision_id])) {
  48. // Just copy all meta tags for all languages.
  49. $entity->metatags = $metatags[$revision_id];
  50. }
  51. }
  52. }
  53. }
  54. }
  55. // Assign the value.
  56. $langcode = metatag_entity_get_language($entity_type, $entity);
  57. $entity->metatags[$langcode][$name]['value'] = $value;
  58. }