metatag.search_api.inc 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. <?php
  2. /**
  3. * @file
  4. * Contains MetatagSearchAlterCallback.
  5. */
  6. /**
  7. * Implements hook_search_api_alter_callback_info().
  8. */
  9. function metatag_search_api_alter_callback_info() {
  10. return array(
  11. 'search_api_metatag_alter_callback' => array(
  12. 'name' => t('Meta tags'),
  13. 'description' => t("Adds the item's meta tags to the indexed data."),
  14. 'class' => 'MetatagSearchAlterCallback',
  15. ),
  16. );
  17. }
  18. /**
  19. * Only add the class if Search API is installed.
  20. */
  21. if (class_exists('SearchApiAbstractAlterCallback')) {
  22. /**
  23. * Adds meta tag values to the indexed items.
  24. */
  25. class MetatagSearchAlterCallback extends SearchApiAbstractAlterCallback {
  26. /**
  27. * {@inheritdoc}
  28. */
  29. public function supportsIndex(SearchApiIndex $index) {
  30. // Only works on entities.
  31. return (bool) $index->getEntityType();
  32. }
  33. /**
  34. * {@inheritdoc}
  35. */
  36. public function alterItems(array &$items) {
  37. $entity_type = $this->index->getEntityType();
  38. $tags = metatag_get_info('tags');
  39. foreach ($items as $id => $item) {
  40. foreach (array_keys($tags) as $tag) {
  41. $items[$id]->{'metatag_' . $tag} = NULL;
  42. if (isset($item->language) && isset($item->metatags[$item->language][$tag])) {
  43. $instance = metatag_get_instance($tag, $item->metatags[$item->language][$tag]);
  44. $items[$id]->{'metatag_' . $tag} = $instance->getValue(array('token data' => array($entity_type => $item)));
  45. }
  46. }
  47. }
  48. }
  49. /**
  50. * {@inheritdoc}
  51. */
  52. public function propertyInfo() {
  53. $properties = array();
  54. // Get available meta tags.
  55. $tags = metatag_get_info('tags');
  56. foreach ($tags as $id => $tag) {
  57. switch ($tag['class']) {
  58. case 'DrupalLinkMetaTag':
  59. $type = 'uri';
  60. break;
  61. default:
  62. $type = 'text';
  63. break;
  64. }
  65. $properties['metatag_' . $id] = array(
  66. 'label' => t('Meta tag: @label', array('@label' => $tag['label'])),
  67. 'description' => t('@label meta tag attached to an item.', array('@label' => $tag['label'])),
  68. 'type' => $type,
  69. );
  70. }
  71. return $properties;
  72. }
  73. }
  74. }