function search_config_query_node_access_alter in Search configuration 8
Same name and namespace in other branches
- 7 search_config.module \search_config_query_node_access_alter()
Implements of hook_query_node_access_alter().
File
- ./
search_config.module, line 41 - The module that search form, including enforcing search restrictions by content type.
Code
function search_config_query_node_access_alter(SelectInterface $query) {
$user = \Drupal::currentUser();
if ($user
->id() == 1) {
return;
}
$search = FALSE;
$node = FALSE;
foreach ($query
->getTables() as $alias => $table) {
if ($table['table'] == 'search_index') {
$search = $alias;
}
elseif ($table['table'] == 'node') {
$node = $alias;
}
}
if ($node && $search) {
if ($user
->id() != 1) {
if (!\Drupal::currentUser()
->hasPermission('search all content')) {
$excluded_content_types = [];
foreach (search_config_content_types() as $type => $label) {
if (!\Drupal::currentUser()
->hasPermission("search {$type} content")) {
$excluded_content_types[] = $type;
}
}
// A core bug results in a DB error if we use the following: Ref: #1210072
// $query->condition($node . '.type', array($excluded_content_types), 'NOT IN');
if (!empty($excluded_content_types)) {
$db_and = new Condition('AND');
foreach ($excluded_content_types as $type) {
$db_and
->condition($node . '.type', $type, '!=');
}
$query
->condition($db_and);
}
}
if (!\Drupal::currentUser()
->hasPermission('search all excluded entities')) {
// Join into the {} table to see if it is excluded.
$query
->leftJoin('search_config_exclude', 'sc', $node . '.nid = sc.entity_id AND sc.entity_type = :type', [
':type' => 'node',
]);
$query
->isNull('sc.entity_id');
}
}
if (get_class($query) == 'PagerDefault' || is_subclass_of($query, 'PagerDefault')) {
$settings = search_config_node_settings();
if (!empty($settings['results']['limit'])) {
$query
->range($settings['results']['limit']);
}
}
}
}