callback_add_viewed_entity.inc 3.2 KB

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