1 node.module _node_search_query_alter(QueryAlterableInterface $query)

Exclude nodes from the search query in the special situations.

  • The node type is disabled in the search settings.
  • The node type has hidden paths and the current user doesn't have permission to bypass the restriction.

Related topics

File

core/modules/node/node.module, line 3151
The core module that allows content to be submitted to the site.

Code

function _node_search_query_alter(QueryAlterableInterface $query) {
  $search_table = FALSE;
  $node_type = FALSE;

  foreach ($query->getTables() as $alias => $table) {
    if ($table['table'] == 'search_index') {
      $search_table = $alias;
    }
    elseif ($table['table'] == 'node') {
      $node_type = $alias;
    }
  }

  if ($node_type && $search_table) {
    $enabled_types = _node_search_get_types();
    $all_types = node_type_get_types();
    $access_hidden_paths = user_access('view hidden paths');

    $excluded_content_types = array();
    foreach ($all_types as $type => $detail) {
      if (!$access_hidden_paths && $detail->settings['hidden_path']) {
        $excluded_content_types[] = $type;
      }
      elseif (!in_array($type, $enabled_types)) {
        $excluded_content_types[] = $type;
      }
    }

    if (!empty($excluded_content_types)) {
      $query->condition($node_type . '.type', array($excluded_content_types), 'NOT IN');
    }
  }
}