callback_add_viewed_entity.inc 3.1 KB

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