callback_add_viewed_entity.inc 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. <?php
  2. /**
  3. * Search API data alteration callback that adds an URL field for all items.
  4. */
  5. class SearchApiAlterAddViewedEntity extends SearchApiAbstractAlterCallback {
  6. /**
  7. * Only support indexes containing entities.
  8. *
  9. * @see SearchApiAlterCallbackInterface::supportsIndex()
  10. */
  11. public function supportsIndex(SearchApiIndex $index) {
  12. return (bool) $index->getEntityType();
  13. }
  14. public function configurationForm() {
  15. $view_modes = array();
  16. if ($entity_type = $this->index->getEntityType()) {
  17. $info = entity_get_info($entity_type);
  18. foreach ($info['view modes'] as $key => $mode) {
  19. $view_modes[$key] = $mode['label'];
  20. }
  21. }
  22. $this->options += array('mode' => reset($view_modes));
  23. if (count($view_modes) > 1) {
  24. $form['mode'] = array(
  25. '#type' => 'select',
  26. '#title' => t('View mode'),
  27. '#options' => $view_modes,
  28. '#default_value' => $this->options['mode'],
  29. );
  30. }
  31. else {
  32. $form['mode'] = array(
  33. '#type' => 'value',
  34. '#value' => $this->options['mode'],
  35. );
  36. if ($view_modes) {
  37. $form['note'] = array(
  38. '#markup' => '<p>' . t('Entities of type %type have only a single view mode. ' .
  39. 'Therefore, no selection needs to be made.', array('%type' => $info['label'])) . '</p>',
  40. );
  41. }
  42. else {
  43. $form['note'] = array(
  44. '#markup' => '<p>' . t('Entities of type %type have no defined view modes. ' .
  45. 'This might either mean that they are always displayed the same way, or that they cannot be processed by this alteration at all. ' .
  46. 'Please consider this when using this alteration.', array('%type' => $info['label'])) . '</p>',
  47. );
  48. }
  49. }
  50. return $form;
  51. }
  52. public function alterItems(array &$items) {
  53. // Prevent session information from being saved while indexing.
  54. drupal_save_session(FALSE);
  55. // Force the current user to anonymous to prevent access bypass in search
  56. // indexes.
  57. $original_user = $GLOBALS['user'];
  58. $GLOBALS['user'] = drupal_anonymous_user();
  59. $type = $this->index->getEntityType();
  60. $mode = empty($this->options['mode']) ? 'full' : $this->options['mode'];
  61. foreach ($items as $id => &$item) {
  62. // Since we can't really know what happens in entity_view() and render(),
  63. // we use try/catch. This will at least prevent some errors, even though
  64. // it's no protection against fatal errors and the like.
  65. try {
  66. $render = entity_view($type, array(entity_id($type, $item) => $item), $mode);
  67. $text = render($render);
  68. if (!$text) {
  69. $item->search_api_viewed = NULL;
  70. continue;
  71. }
  72. $item->search_api_viewed = $text;
  73. }
  74. catch (Exception $e) {
  75. $item->search_api_viewed = NULL;
  76. }
  77. }
  78. // Restore the user.
  79. $GLOBALS['user'] = $original_user;
  80. drupal_save_session(TRUE);
  81. }
  82. public function propertyInfo() {
  83. return array(
  84. 'search_api_viewed' => array(
  85. 'label' => t('Entity HTML output'),
  86. 'description' => t('The whole HTML content of the entity when viewed.'),
  87. 'type' => 'text',
  88. ),
  89. );
  90. }
  91. }