You are here

function search_by_page_search_execute in Search by Page 8

Same name and namespace in other branches
  1. 7 search_by_page.module \search_by_page_search_execute()

Implements hook_search_execute().

File

./search_by_page.module, line 448
Main module file for Drupal module Search by Page.

Code

function search_by_page_search_execute($keys = NULL, $conditions = NULL) {
  global $pager_page_array, $pager_total, $pager_total_items;
  global $language;
  $lang = $language->language;

  // Extract environment and clear from keys.
  $environment = search_expression_extract($keys, 'environment');
  $keys = search_expression_insert($keys, 'environment');
  if (!$environment) {
    $environment = \Drupal::state()
      ->get('search_by_page_default_environment', 1);
  }

  // Set up query for Search module.
  $query = \Drupal::database()
    ->select('search_index', 'i', [
    'target' => 'slave',
  ])
    ->extend('SearchQuery')
    ->extend('PagerDefault');
  $query
    ->searchExpression($keys, 'search_by_page')
    ->join('search_by_page_path', 'sp', 'i.sid = sp.pid');
  $query
    ->condition('sp.environment', $environment)
    ->condition('sp.language', $lang);

  // Set number of results per page.
  $num = intval(search_by_page_setting_get('results_per_page', $environment, 10));
  if ($num > 0) {
    $query
      ->limit($num);
  }

  // Get sub-modules' search modifications.
  $or = new \Drupal\Core\Database\Query\Condition('OR');
  $or
    ->fields('0', '1');
  foreach (module_implements('search_by_page_query_modify') as $module) {
    $cond = module_invoke($module, 'search_by_page_query_modify', $environment, $query);
    $cond
      ->condition('sp.from_module', $module);
    $or
      ->condition($cond);
  }
  $query
    ->condition($or);

  // Perform the search.
  if (!$query
    ->executeFirstPass()) {
    return [];
  }
  $stuff = $query
    ->execute();

  // Create array of formatted results for Search
  $results = [];
  foreach ($stuff as $item) {
    $info = _search_by_page_lookup($item->sid);

    // Figure out the URL to this page.
    $parts = \Drupal::service('search_by_page.settings')
      ->explodePathParts($info->page_path);
    $args = [
      'absolute' => TRUE,
    ];
    if (isset($parts[1])) {
      $args['query'] = [];
      parse_str($parts[1], $args['query']);
    }

    // Special case: if this is a file, we need to use the File API to get
    // the URL. Otherwise, use the normal Drupal URL function.
    if (file_uri_scheme($parts[0])) {

      // This is a file with scheme://filename.
      $link = file_create_url($parts[0]);
    }
    else {
      $link = url($parts[0], $args);
    }
    $res = [
      'link' => $link,
    ];

    // Merge URL with information provided by module from hook_search_by_page_details()
    $res2 = module_invoke($info->from_module, 'search_by_page_details', $info->modid, $environment, $keys);
    if (is_array($res2)) {
      $res = array_merge($res, $res2);
    }

    // Make sure we have a title
    if (!$res['title']) {
      $res['title'] = $link;
    }
    $results[] = $res;
  }
  return $results;
}