metatag.search_api.inc 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  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. * Adds meta tag values to the indexed items.
  20. */
  21. class MetatagSearchAlterCallback extends SearchApiAbstractAlterCallback {
  22. /**
  23. * {@inheritdoc}
  24. */
  25. public function supportsIndex(SearchApiIndex $index) {
  26. // Only works on entities.
  27. return (bool) $index->getEntityType();
  28. }
  29. /**
  30. * {@inheritdoc}
  31. */
  32. public function alterItems(array &$items) {
  33. $entity_type = $this->index->getEntityType();
  34. $tags = metatag_get_info('tags');
  35. foreach ($items as $id => $item) {
  36. foreach (array_keys($tags) as $tag) {
  37. $items[$id]->{'metatag_' . $tag} = NULL;
  38. if (isset($item->language) && isset($item->metatags[$item->language][$tag])) {
  39. $instance = metatag_get_instance($tag, $item->metatags[$item->language][$tag]);
  40. $items[$id]->{'metatag_' . $tag} = $instance->getValue(array('token data' => array($entity_type => $item)));
  41. }
  42. }
  43. }
  44. }
  45. /**
  46. * {@inheritdoc}
  47. */
  48. public function propertyInfo() {
  49. $properties = array();
  50. // Get available meta tags.
  51. $tags = metatag_get_info('tags');
  52. foreach ($tags as $id => $tag) {
  53. switch ($tag['class']) {
  54. case 'DrupalLinkMetaTag':
  55. $type = 'uri';
  56. break;
  57. default:
  58. $type = 'text';
  59. break;
  60. }
  61. $properties['metatag_' . $id] = array(
  62. 'label' => t('Meta tag: @label', array('@label' => $tag['label'])),
  63. 'description' => t('@label meta tag attached to an item.', array('@label' => $tag['label'])),
  64. 'type' => $type,
  65. );
  66. }
  67. return $properties;
  68. }
  69. }