metatag.search_api.inc 2.1 KB

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