You are here

public function PanelizerSearchApiAlterCallback::alterItems in Panelizer 7.3

Alter items before indexing.

Items which are removed from the array won't be indexed, but will be marked as clean for future indexing. This could for instance be used to implement some sort of access filter for security purposes (e.g., don't index unpublished nodes or comments).

Parameters

array $items: An array of items to be altered, keyed by item IDs.

Overrides SearchApiAlterCallbackInterface::alterItems

File

plugins/search_api/PanelizerSearchApiAlterCallback.class.php, line 22
Contains the PanelizerSearchApiAlterCallback class.

Class

PanelizerSearchApiAlterCallback
Search API data alteration callback that adds Panelizer content to items.

Code

public function alterItems(array &$items) {

  // Prevent session information from being saved while indexing.
  drupal_save_session(FALSE);

  // Force the current user to anonymous to prevent access bypass in search
  // indexes.
  $original_user = $GLOBALS['user'];
  $GLOBALS['user'] = drupal_anonymous_user();
  $entity_type = $this->index
    ->getEntityType();
  $entity_handler = panelizer_entity_plugin_get_handler($entity_type);
  foreach ($items as &$item) {
    $entity_id = entity_id($entity_type, $item);
    $item->search_api_panelizer_content = NULL;
    $item->search_api_panelizer_title = NULL;

    // If Search API specifies a language to view the item in, force the
    // global language_content to be Search API item language. Fieldable
    // panel panes will render in the correct language.
    if (isset($item->search_api_language)) {
      global $language_content;
      $original_language_content = $language_content;
      $languages = language_list();
      if (isset($languages[$item->search_api_language])) {
        $language_content = $languages[$item->search_api_language];
      }
      else {
        $language_content = language_default();
      }
    }
    try {
      if ($render_info = $entity_handler
        ->render_entity($item, 'page_manager')) {
        $item->search_api_panelizer_content = $render_info['content'];
        $item->search_api_panelizer_title = !empty($render_info['title']) ? $render_info['title'] : NULL;
      }
    } catch (Exception $e) {
      watchdog_exception('panelizer', $e, 'Error indexing Panelizer content for %entity_type with ID %entity_id', array(
        '%entity_type' => $entity_type,
        '%entity_id' => $entity_id,
      ));
    }

    // Restore the language_content global if it was overridden.
    if (isset($original_language_content)) {
      $language_content = $original_language_content;
    }
  }

  // Restore the user.
  $GLOBALS['user'] = $original_user;
  drupal_save_session(TRUE);
}