You are here

function search_api_page_view in Search API Pages 7

Displays a search page.

Parameters

string $id: The search page's machine name.

string|null $keys: The keys to search for.

array $overridden_options: An associative array of page options that should be overridden for this search only.

1 call to search_api_page_view()
search_api_page_result_content_type_render in plugins/content_types/search_api_page_result.inc
Renders the custom content type.
3 string references to 'search_api_page_view'
search_api_page_menu in ./search_api_page.module
Implements hook_menu().
search_api_page_search_api_page_menu_alter in plugins/tasks/search_api_page.inc
Alters available menu items based on a defined task.
search_api_page_search_api_page_page in plugins/tasks/search_api_page.inc
Page callback: Displays a search page as a page manager task.

File

./search_api_page.pages.inc, line 18
User page callbacks for the Search pages module.

Code

function search_api_page_view($id, $keys = NULL, $overridden_options = array()) {
  $page = search_api_page_load($id);
  if (!$page) {
    return MENU_NOT_FOUND;
  }

  // Merge in overridden options.
  if ($overridden_options && is_array($overridden_options)) {
    $page->original_options = $page->options;
    $page->options = $overridden_options + $page->options;
  }

  // Pre-process keys (unescape \ and /).
  if (isset($keys) && $keys !== '') {
    $max_length = 128;
    if (isset($page->options['max_length'])) {
      $max_length = $page->options['max_length'] > 0 ? $page->options['max_length'] : NULL;
    }
    if ($max_length && drupal_strlen($keys) > $max_length) {
      $keys = drupal_substr($keys, 0, $max_length);
    }
    $keys = explode("\\\\", $keys);
    $keys = str_replace("\\", "/", $keys);
    $keys = implode("\\", $keys);
  }
  else {
    $keys = NULL;
  }
  $ret = array(
    '#theme' => "search_api_page_full_page__{$page->machine_name}",
  );
  $ret['#contextual_links']['search_api_page'] = array(
    'admin/config/search/search_api/page',
    array(
      $page->machine_name,
    ),
  );
  if (!isset($page->options['result_page_search_form']) || $page->options['result_page_search_form']) {
    $ret['#form'] = drupal_get_form('search_api_page_search_form', $page, $keys);
  }

  // Do a search if we have keys, or our empty behavior and facets dictate.
  if ($keys || !empty($page->options['empty_behavior'])) {

    // Override per_page setting with GET parameter.
    $limit = $page->options['per_page'];
    if (!empty($page->options['get_per_page']) && !empty($_GET['per_page']) && (int) $_GET['per_page'] > 0) {
      $limit = (int) $_GET['per_page'];
    }
    try {
      $results = search_api_page_search_execute($page, $keys, $limit);
    } catch (SearchApiException $e) {
      drupal_set_message(t('An error occurred while executing the search. Please try again, or contact the site administrator if the problem persists.'), 'error');
      $link = l(t('search page'), $_GET['q'], array(
        'query' => drupal_get_query_parameters(),
      ));
      watchdog_exception('search_api_page', $e, '%type while executing a search: !message in %function (line %line of %file).', array(), WATCHDOG_ERROR, $link);
      return $ret;
    }
    if (empty($results)) {
      return $ret;
    }
    $ret['#results']['#theme'] = "search_api_page_results__{$page->machine_name}";
    $ret['#results']['#index'] = search_api_index_load($page->index_id);
    $ret['#results']['#view_mode'] = isset($page->options['view_mode']) ? $page->options['view_mode'] : 'search_api_page_result';
    $ret['#results']['#page'] = $page;

    // If spellcheck results were returned then add them to the render array.
    if (isset($results['search_api_spellcheck'])) {
      $ret['#results']['#spellcheck'] = array(
        '#theme' => 'search_api_spellcheck',
        '#spellcheck' => $results['search_api_spellcheck'],
        // Let the theme function know where the key is stored by passing its arg
        // number. We can work this out from the number of args in the page path.
        '#options' => array(
          'arg' => array(
            count(arg(NULL, $page->path)),
          ),
        ),
        '#prefix' => '<p class="search-api-spellcheck-suggestion">',
        '#suffix' => '</p>',
      );
    }
    $ret['#results']['#results'] = $results;
    $ret['#results']['#keys'] = $keys;

    // Add a clean-up function to reset page options to their original values.
    if ($overridden_options) {
      $ret['#results']['#post_render'] = array(
        'search_api_page_reset_original_options',
      );
    }

    // Load pager.
    if ($results['result count'] > $limit) {
      pager_default_initialize($results['result count'], $limit);
      $ret['#results']['#pager']['#theme'] = 'pager';
    }
    if (!empty($results['ignored'])) {
      drupal_set_message(t('The following search keys are too short or too common and were therefore ignored: "@list".', array(
        '@list' => implode(t('", "'), $results['ignored']),
      )), 'warning');
    }
    if (!empty($results['warnings'])) {
      foreach ($results['warnings'] as $warning) {
        drupal_set_message(check_plain($warning), 'warning');
      }
    }
  }
  return $ret;
}