You are here

function search_by_page_paths_search_by_page_query_modify in Search by Page 8

Implements Search by Page hook_search_by_page_query_modify().

Adds an access permission check to the search query.

File

search_by_page_paths/search_by_page_paths.module, line 60
Module file for Search by Page Paths, a sub-module for Search by Page.

Code

function search_by_page_paths_search_by_page_query_modify($environment, $query) {

  // Unfortunately, we don't have a wholesale way to check access permissions.
  // So we have to go through all the paths in our table, and check whether the
  // user has permission to see each one, by querying the menu system.
  // Hopefully, there aren't too many! We store them in a temporary table.
  // Create temporary table
  $table = \Drupal::database()
    ->queryTemporary('SELECT p.pid, 1 as perm FROM {sbpp_path} p WHERE p.environment=:env', array(
    ':env' => $environment,
  ));

  // Check permissions on each path, store in temporary table
  $res = \Drupal::database()
    ->query('SELECT p.pid, p.page_path FROM {sbpp_path} p WHERE p.environment=:env', array(
    ':env' => $environment,
  ))
    ->fetchAll();
  foreach ($res as $item) {
    $parts = search_by_page_path_parts($item->page_path);
    $mitem = menu_get_item($parts[0]);
    if (!$mitem['access']) {
      \Drupal::database()
        ->delete($table)
        ->condition('pid', $item->pid)
        ->execute();
    }
  }

  // Join to our temporary table
  $query
    ->leftJoin($table, 'sbpp_p', 'sbpp_p.pid = sp.modid');
  $cond = new \Drupal\Core\Database\Query\Condition('AND');
  $cond
    ->condition('sbpp_p.perm', 1);
  return $cond;
}