3 namespace Drupal\node\Plugin\Search;
5 use Drupal\Core\Access\AccessResult;
6 use Drupal\Core\Cache\CacheableMetadata;
7 use Drupal\Core\Config\Config;
8 use Drupal\Core\Database\Connection;
9 use Drupal\Core\Database\Query\SelectExtender;
10 use Drupal\Core\Database\StatementInterface;
11 use Drupal\Core\Entity\EntityManagerInterface;
12 use Drupal\Core\Extension\ModuleHandlerInterface;
13 use Drupal\Core\Form\FormStateInterface;
14 use Drupal\Core\Language\LanguageInterface;
15 use Drupal\Core\Language\LanguageManagerInterface;
16 use Drupal\Core\Session\AccountInterface;
17 use Drupal\Core\Access\AccessibleInterface;
18 use Drupal\Core\Database\Query\Condition;
19 use Drupal\Core\Render\RendererInterface;
20 use Drupal\node\NodeInterface;
21 use Drupal\search\Plugin\ConfigurableSearchPluginBase;
22 use Drupal\search\Plugin\SearchIndexingInterface;
23 use Drupal\Search\SearchQuery;
24 use Symfony\Component\DependencyInjection\ContainerInterface;
27 * Handles searching for node entities using the Search module index.
31 * title = @Translation("Content")
34 class NodeSearch extends ConfigurableSearchPluginBase implements AccessibleInterface, SearchIndexingInterface {
37 * A database connection object.
39 * @var \Drupal\Core\Database\Connection
44 * An entity manager object.
46 * @var \Drupal\Core\Entity\EntityManagerInterface
48 protected $entityManager;
51 * A module manager object.
53 * @var \Drupal\Core\Extension\ModuleHandlerInterface
55 protected $moduleHandler;
58 * A config object for 'search.settings'.
60 * @var \Drupal\Core\Config\Config
62 protected $searchSettings;
65 * The language manager.
67 * @var \Drupal\Core\Language\LanguageManagerInterface
69 protected $languageManager;
72 * The Drupal account to use for checking for access to advanced search.
74 * @var \Drupal\Core\Session\AccountInterface
79 * The Renderer service to format the username and node.
81 * @var \Drupal\Core\Render\RendererInterface
86 * An array of additional rankings from hook_ranking().
93 * The list of options and info for advanced search filters.
95 * Each entry in the array has the option as the key and for its value, an
96 * array that determines how the value is matched in the database query. The
97 * possible keys in that array are:
98 * - column: (required) Name of the database column to match against.
99 * - join: (optional) Information on a table to join. By default the data is
100 * matched against the {node_field_data} table.
101 * - operator: (optional) OR or AND, defaults to OR.
105 protected $advanced = [
106 'type' => ['column' => 'n.type'],
107 'language' => ['column' => 'i.langcode'],
108 'author' => ['column' => 'n.uid'],
109 'term' => ['column' => 'ti.tid', 'join' => ['table' => 'taxonomy_index', 'alias' => 'ti', 'condition' => 'n.nid = ti.nid']],
113 * A constant for setting and checking the query string.
115 const ADVANCED_FORM = 'advanced-form';
120 public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
125 $container->get('database'),
126 $container->get('entity.manager'),
127 $container->get('module_handler'),
128 $container->get('config.factory')->get('search.settings'),
129 $container->get('language_manager'),
130 $container->get('renderer'),
131 $container->get('current_user')
136 * Constructs a \Drupal\node\Plugin\Search\NodeSearch object.
138 * @param array $configuration
139 * A configuration array containing information about the plugin instance.
140 * @param string $plugin_id
141 * The plugin_id for the plugin instance.
142 * @param mixed $plugin_definition
143 * The plugin implementation definition.
144 * @param \Drupal\Core\Database\Connection $database
145 * A database connection object.
146 * @param \Drupal\Core\Entity\EntityManagerInterface $entity_manager
147 * An entity manager object.
148 * @param \Drupal\Core\Extension\ModuleHandlerInterface $module_handler
149 * A module manager object.
150 * @param \Drupal\Core\Config\Config $search_settings
151 * A config object for 'search.settings'.
152 * @param \Drupal\Core\Language\LanguageManagerInterface $language_manager
153 * The language manager.
154 * @param \Drupal\Core\Render\RendererInterface $renderer
156 * @param \Drupal\Core\Session\AccountInterface $account
157 * The $account object to use for checking for access to advanced search.
159 public function __construct(array $configuration, $plugin_id, $plugin_definition, Connection $database, EntityManagerInterface $entity_manager, ModuleHandlerInterface $module_handler, Config $search_settings, LanguageManagerInterface $language_manager, RendererInterface $renderer, AccountInterface $account = NULL) {
160 $this->database = $database;
161 $this->entityManager = $entity_manager;
162 $this->moduleHandler = $module_handler;
163 $this->searchSettings = $search_settings;
164 $this->languageManager = $language_manager;
165 $this->renderer = $renderer;
166 $this->account = $account;
167 parent::__construct($configuration, $plugin_id, $plugin_definition);
169 $this->addCacheTags(['node_list']);
175 public function access($operation = 'view', AccountInterface $account = NULL, $return_as_object = FALSE) {
176 $result = AccessResult::allowedIfHasPermission($account, 'access content');
177 return $return_as_object ? $result : $result->isAllowed();
183 public function isSearchExecutable() {
184 // Node search is executable if we have keywords or an advanced parameter.
185 // At least, we should parse out the parameters and see if there are any
186 // keyword matches in that case, rather than just printing out the
187 // "Please enter keywords" message.
188 return !empty($this->keywords) || (isset($this->searchParameters['f']) && count($this->searchParameters['f']));
194 public function getType() {
195 return $this->getPluginId();
201 public function execute() {
202 if ($this->isSearchExecutable()) {
203 $results = $this->findResults();
206 return $this->prepareResults($results);
214 * Queries to find search results, and sets status messages.
216 * This method can assume that $this->isSearchExecutable() has already been
217 * checked and returned TRUE.
219 * @return \Drupal\Core\Database\StatementInterface|null
220 * Results from search query execute() method, or NULL if the search
223 protected function findResults() {
224 $keys = $this->keywords;
226 // Build matching conditions.
227 $query = $this->database
228 ->select('search_index', 'i', ['target' => 'replica'])
229 ->extend('Drupal\search\SearchQuery')
230 ->extend('Drupal\Core\Database\Query\PagerSelectExtender');
231 $query->join('node_field_data', 'n', 'n.nid = i.sid AND n.langcode = i.langcode');
232 $query->condition('n.status', 1)
233 ->addTag('node_access')
234 ->searchExpression($keys, $this->getPluginId());
236 // Handle advanced search filters in the f query string.
237 // \Drupal::request()->query->get('f') is an array that looks like this in
238 // the URL: ?f[]=type:page&f[]=term:27&f[]=term:13&f[]=langcode:en
239 // So $parameters['f'] looks like:
240 // array('type:page', 'term:27', 'term:13', 'langcode:en');
241 // We need to parse this out into query conditions, some of which go into
242 // the keywords string, and some of which are separate conditions.
243 $parameters = $this->getParameters();
244 if (!empty($parameters['f']) && is_array($parameters['f'])) {
246 // Match any query value that is an expected option and a value
247 // separated by ':' like 'term:27'.
248 $pattern = '/^(' . implode('|', array_keys($this->advanced)) . '):([^ ]*)/i';
249 foreach ($parameters['f'] as $item) {
250 if (preg_match($pattern, $item, $m)) {
251 // Use the matched value as the array key to eliminate duplicates.
252 $filters[$m[1]][$m[2]] = $m[2];
256 // Now turn these into query conditions. This assumes that everything in
257 // $filters is a known type of advanced search.
258 foreach ($filters as $option => $matched) {
259 $info = $this->advanced[$option];
260 // Insert additional conditions. By default, all use the OR operator.
261 $operator = empty($info['operator']) ? 'OR' : $info['operator'];
262 $where = new Condition($operator);
263 foreach ($matched as $value) {
264 $where->condition($info['column'], $value);
266 $query->condition($where);
267 if (!empty($info['join'])) {
268 $query->join($info['join']['table'], $info['join']['alias'], $info['join']['condition']);
273 // Add the ranking expressions.
274 $this->addNodeRankings($query);
278 // Add the language code of the indexed item to the result of the query,
279 // since the node will be rendered using the respective language.
280 ->fields('i', ['langcode'])
281 // And since SearchQuery makes these into GROUP BY queries, if we add
282 // a field, for PostgreSQL we also need to make it an aggregate or a
283 // GROUP BY. In this case, we want GROUP BY.
284 ->groupBy('i.langcode')
288 // Check query status and set messages if needed.
289 $status = $query->getStatus();
291 if ($status & SearchQuery::EXPRESSIONS_IGNORED) {
292 drupal_set_message($this->t('Your search used too many AND/OR expressions. Only the first @count terms were included in this search.', ['@count' => $this->searchSettings->get('and_or_limit')]), 'warning');
295 if ($status & SearchQuery::LOWER_CASE_OR) {
296 drupal_set_message($this->t('Search for either of the two terms with uppercase <strong>OR</strong>. For example, <strong>cats OR dogs</strong>.'), 'warning');
299 if ($status & SearchQuery::NO_POSITIVE_KEYWORDS) {
300 drupal_set_message($this->formatPlural($this->searchSettings->get('index.minimum_word_size'), 'You must include at least one keyword to match in the content, and punctuation is ignored.', 'You must include at least one keyword to match in the content. Keywords must be at least @count characters, and punctuation is ignored.'), 'warning');
307 * Prepares search results for rendering.
309 * @param \Drupal\Core\Database\StatementInterface $found
310 * Results found from a successful search query execute() method.
313 * Array of search result item render arrays (empty array if no results).
315 protected function prepareResults(StatementInterface $found) {
318 $node_storage = $this->entityManager->getStorage('node');
319 $node_render = $this->entityManager->getViewBuilder('node');
320 $keys = $this->keywords;
322 foreach ($found as $item) {
324 /** @var \Drupal\node\NodeInterface $node */
325 $node = $node_storage->load($item->sid)->getTranslation($item->langcode);
326 $build = $node_render->view($node, 'search_result', $item->langcode);
328 /** @var \Drupal\node\NodeTypeInterface $type*/
329 $type = $this->entityManager->getStorage('node_type')->load($node->bundle());
331 unset($build['#theme']);
332 $build['#pre_render'][] = [$this, 'removeSubmittedInfo'];
334 // Fetch comments for snippet.
335 $rendered = $this->renderer->renderPlain($build);
336 $this->addCacheableDependency(CacheableMetadata::createFromRenderArray($build));
337 $rendered .= ' ' . $this->moduleHandler->invoke('comment', 'node_update_index', [$node]);
339 $extra = $this->moduleHandler->invokeAll('node_search_result', [$node]);
341 $language = $this->languageManager->getLanguage($item->langcode);
343 '#theme' => 'username',
344 '#account' => $node->getOwner(),
348 'link' => $node->url('canonical', ['absolute' => TRUE, 'language' => $language]),
349 'type' => $type->label(),
350 'title' => $node->label(),
353 'score' => $item->calculated_score,
354 'snippet' => search_excerpt($keys, $rendered, $item->langcode),
355 'langcode' => $node->language()->getId(),
358 $this->addCacheableDependency($node);
360 // We have to separately add the node owner's cache tags because search
361 // module doesn't use the rendering system, it does its own rendering
362 // without taking cacheability metadata into account. So we have to do it
364 $this->addCacheableDependency($node->getOwner());
366 if ($type->displaySubmitted()) {
368 'user' => $this->renderer->renderPlain($username),
369 'date' => $node->getChangedTime(),
373 $results[] = $result;
380 * Removes the submitted by information from the build array.
382 * This information is being removed from the rendered node that is used to
383 * build the search result snippet. It just doesn't make sense to have it
384 * displayed in the snippet.
386 * @param array $build
390 * The modified build array.
392 public function removeSubmittedInfo(array $build) {
393 unset($build['created']);
394 unset($build['uid']);
399 * Adds the configured rankings to the search query.
402 * A query object that has been extended with the Search DB Extender.
404 protected function addNodeRankings(SelectExtender $query) {
405 if ($ranking = $this->getRankings()) {
406 $tables = &$query->getTables();
407 foreach ($ranking as $rank => $values) {
408 if (isset($this->configuration['rankings'][$rank]) && !empty($this->configuration['rankings'][$rank])) {
409 $node_rank = $this->configuration['rankings'][$rank];
410 // If the table defined in the ranking isn't already joined, then add it.
411 if (isset($values['join']) && !isset($tables[$values['join']['alias']])) {
412 $query->addJoin($values['join']['type'], $values['join']['table'], $values['join']['alias'], $values['join']['on']);
414 $arguments = isset($values['arguments']) ? $values['arguments'] : [];
415 $query->addScore($values['score'], $arguments, $node_rank);
424 public function updateIndex() {
425 // Interpret the cron limit setting as the maximum number of nodes to index
427 $limit = (int) $this->searchSettings->get('index.cron_limit');
429 $query = db_select('node', 'n', ['target' => 'replica']);
430 $query->addField('n', 'nid');
431 $query->leftJoin('search_dataset', 'sd', 'sd.sid = n.nid AND sd.type = :type', [':type' => $this->getPluginId()]);
432 $query->addExpression('CASE MAX(sd.reindex) WHEN NULL THEN 0 ELSE 1 END', 'ex');
433 $query->addExpression('MAX(sd.reindex)', 'ex2');
435 $query->orConditionGroup()
436 ->where('sd.sid IS NULL')
437 ->condition('sd.reindex', 0, '<>')
439 $query->orderBy('ex', 'DESC')
445 $nids = $query->execute()->fetchCol();
450 $node_storage = $this->entityManager->getStorage('node');
451 foreach ($node_storage->loadMultiple($nids) as $node) {
452 $this->indexNode($node);
457 * Indexes a single node.
459 * @param \Drupal\node\NodeInterface $node
462 protected function indexNode(NodeInterface $node) {
463 $languages = $node->getTranslationLanguages();
464 $node_render = $this->entityManager->getViewBuilder('node');
466 foreach ($languages as $language) {
467 $node = $node->getTranslation($language->getId());
469 $build = $node_render->view($node, 'search_index', $language->getId());
471 unset($build['#theme']);
473 // Add the title to text so it is searchable.
474 $build['search_title'] = [
476 '#plain_text' => $node->label(),
477 '#suffix' => '</h1>',
480 $text = $this->renderer->renderPlain($build);
482 // Fetch extra data normally not visible.
483 $extra = $this->moduleHandler->invokeAll('node_update_index', [$node]);
484 foreach ($extra as $t) {
488 // Update index, using search index "type" equal to the plugin ID.
489 search_index($this->getPluginId(), $node->id(), $language->getId(), $text);
496 public function indexClear() {
497 // All NodeSearch pages share a common search index "type" equal to
499 search_index_clear($this->getPluginId());
505 public function markForReindex() {
506 // All NodeSearch pages share a common search index "type" equal to
508 search_mark_for_reindex($this->getPluginId());
514 public function indexStatus() {
515 $total = $this->database->query('SELECT COUNT(*) FROM {node}')->fetchField();
516 $remaining = $this->database->query("SELECT COUNT(DISTINCT n.nid) FROM {node} n LEFT JOIN {search_dataset} sd ON sd.sid = n.nid AND sd.type = :type WHERE sd.sid IS NULL OR sd.reindex <> 0", [':type' => $this->getPluginId()])->fetchField();
518 return ['remaining' => $remaining, 'total' => $total];
524 public function searchFormAlter(array &$form, FormStateInterface $form_state) {
525 $parameters = $this->getParameters();
526 $keys = $this->getKeywords();
527 $used_advanced = !empty($parameters[self::ADVANCED_FORM]);
528 if ($used_advanced) {
529 $f = isset($parameters['f']) ? (array) $parameters['f'] : [];
530 $defaults = $this->parseAdvancedDefaults($f, $keys);
533 $defaults = ['keys' => $keys];
536 $form['basic']['keys']['#default_value'] = $defaults['keys'];
538 // Add advanced search keyword-related boxes.
539 $form['advanced'] = [
540 '#type' => 'details',
541 '#title' => t('Advanced search'),
542 '#attributes' => ['class' => ['search-advanced']],
543 '#access' => $this->account && $this->account->hasPermission('use advanced search'),
544 '#open' => $used_advanced,
546 $form['advanced']['keywords-fieldset'] = [
547 '#type' => 'fieldset',
548 '#title' => t('Keywords'),
551 $form['advanced']['keywords'] = [
552 '#prefix' => '<div class="criterion">',
553 '#suffix' => '</div>',
556 $form['advanced']['keywords-fieldset']['keywords']['or'] = [
557 '#type' => 'textfield',
558 '#title' => t('Containing any of the words'),
561 '#default_value' => isset($defaults['or']) ? $defaults['or'] : '',
564 $form['advanced']['keywords-fieldset']['keywords']['phrase'] = [
565 '#type' => 'textfield',
566 '#title' => t('Containing the phrase'),
569 '#default_value' => isset($defaults['phrase']) ? $defaults['phrase'] : '',
572 $form['advanced']['keywords-fieldset']['keywords']['negative'] = [
573 '#type' => 'textfield',
574 '#title' => t('Containing none of the words'),
577 '#default_value' => isset($defaults['negative']) ? $defaults['negative'] : '',
581 $types = array_map(['\Drupal\Component\Utility\Html', 'escape'], node_type_get_names());
582 $form['advanced']['types-fieldset'] = [
583 '#type' => 'fieldset',
584 '#title' => t('Types'),
586 $form['advanced']['types-fieldset']['type'] = [
587 '#type' => 'checkboxes',
588 '#title' => t('Only of the type(s)'),
589 '#prefix' => '<div class="criterion">',
590 '#suffix' => '</div>',
591 '#options' => $types,
592 '#default_value' => isset($defaults['type']) ? $defaults['type'] : [],
595 $form['advanced']['submit'] = [
597 '#value' => t('Advanced search'),
598 '#prefix' => '<div class="action">',
599 '#suffix' => '</div>',
604 $language_options = [];
605 $language_list = $this->languageManager->getLanguages(LanguageInterface::STATE_ALL);
606 foreach ($language_list as $langcode => $language) {
607 // Make locked languages appear special in the list.
608 $language_options[$langcode] = $language->isLocked() ? t('- @name -', ['@name' => $language->getName()]) : $language->getName();
610 if (count($language_options) > 1) {
611 $form['advanced']['lang-fieldset'] = [
612 '#type' => 'fieldset',
613 '#title' => t('Languages'),
615 $form['advanced']['lang-fieldset']['language'] = [
616 '#type' => 'checkboxes',
617 '#title' => t('Languages'),
618 '#prefix' => '<div class="criterion">',
619 '#suffix' => '</div>',
620 '#options' => $language_options,
621 '#default_value' => isset($defaults['language']) ? $defaults['language'] : [],
629 public function buildSearchUrlQuery(FormStateInterface $form_state) {
630 // Read keyword and advanced search information from the form values,
631 // and put these into the GET parameters.
632 $keys = trim($form_state->getValue('keys'));
635 // Collect extra filters.
637 if ($form_state->hasValue('type') && is_array($form_state->getValue('type'))) {
638 // Retrieve selected types - Form API sets the value of unselected
640 foreach ($form_state->getValue('type') as $type) {
643 $filters[] = 'type:' . $type;
648 if ($form_state->hasValue('term') && is_array($form_state->getValue('term'))) {
649 foreach ($form_state->getValue('term') as $term) {
650 $filters[] = 'term:' . $term;
654 if ($form_state->hasValue('language') && is_array($form_state->getValue('language'))) {
655 foreach ($form_state->getValue('language') as $language) {
658 $filters[] = 'language:' . $language;
662 if ($form_state->getValue('or') != '') {
663 if (preg_match_all('/ ("[^"]+"|[^" ]+)/i', ' ' . $form_state->getValue('or'), $matches)) {
664 $keys .= ' ' . implode(' OR ', $matches[1]);
668 if ($form_state->getValue('negative') != '') {
669 if (preg_match_all('/ ("[^"]+"|[^" ]+)/i', ' ' . $form_state->getValue('negative'), $matches)) {
670 $keys .= ' -' . implode(' -', $matches[1]);
674 if ($form_state->getValue('phrase') != '') {
675 $keys .= ' "' . str_replace('"', ' ', $form_state->getValue('phrase')) . '"';
680 // Put the keywords and advanced parameters into GET parameters. Make sure
681 // to put keywords into the query even if it is empty, because the page
682 // controller uses that to decide it's time to check for search results.
683 $query = ['keys' => $keys];
685 $query['f'] = $filters;
687 // Record that the person used the advanced search form, if they did.
689 $query[self::ADVANCED_FORM] = '1';
696 * Parses the advanced search form default values.
699 * The 'f' query parameter set up in self::buildUrlSearchQuery(), which
700 * contains the advanced query values.
701 * @param string $keys
702 * The search keywords string, which contains some information from the
703 * advanced search form.
706 * Array of default form values for the advanced search form, including
707 * a modified 'keys' element for the bare search keywords.
709 protected function parseAdvancedDefaults($f, $keys) {
712 // Split out the advanced search parameters.
713 foreach ($f as $advanced) {
714 list($key, $value) = explode(':', $advanced, 2);
715 if (!isset($defaults[$key])) {
716 $defaults[$key] = [];
718 $defaults[$key][] = $value;
721 // Split out the negative, phrase, and OR parts of keywords.
723 // For phrases, the form only supports one phrase.
725 $keys = ' ' . $keys . ' ';
726 if (preg_match('/ "([^"]+)" /', $keys, $matches)) {
727 $keys = str_replace($matches[0], ' ', $keys);
728 $defaults['phrase'] = $matches[1];
731 // Negative keywords: pull all of them out.
732 if (preg_match_all('/ -([^ ]+)/', $keys, $matches)) {
733 $keys = str_replace($matches[0], ' ', $keys);
734 $defaults['negative'] = implode(' ', $matches[1]);
737 // OR keywords: pull up to one set of them out of the query.
738 if (preg_match('/ [^ ]+( OR [^ ]+)+ /', $keys, $matches)) {
739 $keys = str_replace($matches[0], ' ', $keys);
740 $words = explode(' OR ', trim($matches[0]));
741 $defaults['or'] = implode(' ', $words);
744 // Put remaining keywords string back into keywords.
745 $defaults['keys'] = trim($keys);
751 * Gathers ranking definitions from hook_ranking().
754 * An array of ranking definitions.
756 protected function getRankings() {
757 if (!$this->rankings) {
758 $this->rankings = $this->moduleHandler->invokeAll('ranking');
760 return $this->rankings;
766 public function defaultConfiguration() {
770 return $configuration;
776 public function buildConfigurationForm(array $form, FormStateInterface $form_state) {
777 // Output form for defining rank factor weights.
778 $form['content_ranking'] = [
779 '#type' => 'details',
780 '#title' => t('Content ranking'),
783 $form['content_ranking']['info'] = [
784 '#markup' => '<p><em>' . $this->t('Influence is a numeric multiplier used in ordering search results. A higher number means the corresponding factor has more influence on search results; zero means the factor is ignored. Changing these numbers does not require the search index to be rebuilt. Changes take effect immediately.') . '</em></p>'
787 $header = [$this->t('Factor'), $this->t('Influence')];
788 $form['content_ranking']['rankings'] = [
790 '#header' => $header,
793 // Note: reversed to reflect that higher number = higher ranking.
794 $range = range(0, 10);
795 $options = array_combine($range, $range);
796 foreach ($this->getRankings() as $var => $values) {
797 $form['content_ranking']['rankings'][$var]['name'] = [
798 '#markup' => $values['title'],
800 $form['content_ranking']['rankings'][$var]['value'] = [
802 '#options' => $options,
803 '#attributes' => ['aria-label' => $this->t("Influence of '@title'", ['@title' => $values['title']])],
804 '#default_value' => isset($this->configuration['rankings'][$var]) ? $this->configuration['rankings'][$var] : 0,
813 public function submitConfigurationForm(array &$form, FormStateInterface $form_state) {
814 foreach ($this->getRankings() as $var => $values) {
815 if (!$form_state->isValueEmpty(['rankings', $var, 'value'])) {
816 $this->configuration['rankings'][$var] = $form_state->getValue(['rankings', $var, 'value']);
819 unset($this->configuration['rankings'][$var]);