diff --git a/core/modules/help_topics/help_topics.module b/core/modules/help_topics/help_topics.module index e9c6c4674c..2af0cb2996 100644 --- a/core/modules/help_topics/help_topics.module +++ b/core/modules/help_topics/help_topics.module @@ -64,10 +64,57 @@ function help_topics_rebuild() { // when a module or theme is installed, uninstalled, or updated; and when // languages are added, translations are changed, or string overrides are // changed. These situations can cause an indexed help item to have different - // text, or for help items to be added or removed. So, we use a state - // variable to keep track of the need to reindex. + // text, or for help items to be added or removed. We cannot detect all of + // these cases, so instead at least trigger a reindex on cache rebuild. + help_topics_update_help_search_index('reindex'); +} + +/** + * Implements hook_modules_uninstalled(). + */ +function help_topics_modules_uninstalled(array $modules) { + help_topics_update_help_search_index('list'); +} + +/** + * Implements hook_themes_uninstalled(). + */ +function help_topics_themes_uninstalled(array $themes) { + help_topics_update_help_search_index('list'); +} + +/** + * Updates the full search index or the topic list for help search. + * + * If there is currently an active help search page on the site, either the help + * topics that have been indexed for searching are marked for reindexing, or + * the list of topics is updated. + * + * @param string $action + * 'reindex' to mark all topics for reindexing, or 'list' (default) to only + * update the topic list. + */ +function help_topics_update_help_search_index($action = 'list') { + if (!\Drupal::moduleHandler()->moduleExists('search')) { + return; + } + + $search_page_repository = \Drupal::service('search.search_page_repository'); + /** @var \Drupal\help_topics\Plugin\Search\HelpSearch $plugin */ + $plugin = NULL; + foreach ($search_page_repository->getIndexableSearchPages() as $entity) { + if ($entity->get('plugin') == 'help_search') { + $plugin = $entity->getPlugin(); + break; + } + } - if (\Drupal::moduleHandler()->moduleExists('search')) { - \Drupal::state()->set('help_search_reindex_needed', TRUE); + if ($plugin) { + if ($action === 'list') { + $plugin->updateTopicList(); + } + elseif ($action === 'reindex') { + $plugin->markForReindex(); + } } } diff --git a/core/modules/help_topics/help_topics/help_topics.help_topic_search.html.twig b/core/modules/help_topics/help_topics/help_topics.help_topic_search.html.twig index 394a74f75f..e18b5ad1d3 100644 --- a/core/modules/help_topics/help_topics/help_topics.help_topic_search.html.twig +++ b/core/modules/help_topics/help_topics/help_topics.help_topic_search.html.twig @@ -8,6 +8,7 @@
  • {% trans %}In the Manage administrative menu, navigate to Configuration > Search and metadata > Search pages (admin/config/search/pages).{% endtrans %}
  • {% trans %}Verify that a Help search page is listed in the Search pages section. If not, add a new page of type Help.{% endtrans %}
  • {% trans %}Check the indexing status of the Help search page. If it is not fully indexed, run Cron until indexing is complete.{% endtrans %}
  • +
  • {% trans %}In the future, you can click Rebuild search index on this page, or clear the site cache, in order to force help topic text to be reindexed for searching. This should be done whenever a module, theme, language, or string translation is updated.{% endtrans %}
  • {% trans %}In the Manage administrative menu, navigate to Structure > Block layout (admin/structure/block).{% endtrans %}
  • {% trans %}Click the link for your administrative theme (such as the core Seven theme), near the top of the page.{% endtrans %}
  • {% trans %}See if there is already a help search block placed in the Help region of your administrative theme.{% endtrans %}
  • diff --git a/core/modules/help_topics/src/Plugin/HelpSection/HelpTopicSection.php b/core/modules/help_topics/src/Plugin/HelpSection/HelpTopicSection.php index c604d650ae..8af8688b38 100644 --- a/core/modules/help_topics/src/Plugin/HelpSection/HelpTopicSection.php +++ b/core/modules/help_topics/src/Plugin/HelpSection/HelpTopicSection.php @@ -150,6 +150,14 @@ public function getCacheMaxAge() { return $this->getCacheMetadata()->getCacheMaxAge(); } + /** + * {@inheritdoc} + */ + public function clearCachedDefinitions() { + parent::clearCachedDefinitions(); + help_topics_update_help_search_index('reindex'); + } + /** * {@inheritdoc} */ diff --git a/core/modules/help_topics/src/Plugin/Search/HelpSearch.php b/core/modules/help_topics/src/Plugin/Search/HelpSearch.php index 29c4f726ce..fc5daa2e88 100644 --- a/core/modules/help_topics/src/Plugin/Search/HelpSearch.php +++ b/core/modules/help_topics/src/Plugin/Search/HelpSearch.php @@ -292,10 +292,8 @@ protected function prepareResults(StatementInterface $found) { * {@inheritdoc} */ public function updateIndex() { - // See if we need to update the list of items to be indexed. - if ($this->state->get('help_search_reindex_needed', TRUE)) { - $this->markForReindex(); - } + // Update the list of items to be indexed. + $this->updateTopicList(); // Find some items that need to be updated. Start with ones that have // never been indexed. @@ -361,11 +359,11 @@ public function indexClear() { } /** - * {@inheritdoc} + * Rebuilds the database table containing topics to be indexed. */ - public function markForReindex() { - // Update the list of help items. Start by fetching the existing list, - // so we can remove items not found at the end. + public function updateTopicList() { + // Start by fetching the existing list, so we can remove items not found + // at the end. $old_list = $this->database->select('help_search_items', 'hsi') ->fields('hsi', ['sid', 'topic_id', 'section_plugin_id', 'permission']) ->execute(); @@ -414,21 +412,21 @@ public function markForReindex() { // Remove remaining items from the index. $this->removeItemsFromIndex($sids_to_remove); + } - // Mark all items currently in the search index database as needing - // reindex. + /** + * {@inheritdoc} + */ + public function markForReindex() { + $this->updateTopicList(); search_mark_for_reindex($this->getType()); - $this->state->set('help_search_reindex_needed', FALSE); } /** * {@inheritdoc} */ public function indexStatus() { - if ($this->state->get('help_search_reindex_needed', TRUE)) { - $this->markForReindex(); - } - + $this->updateTopicList(); $total = $this->database->select('help_search_items', 'hsi') ->countQuery() ->execute() diff --git a/core/modules/help_topics/src/SearchableHelpInterface.php b/core/modules/help_topics/src/SearchableHelpInterface.php index 06fdb939c1..701c7fd102 100644 --- a/core/modules/help_topics/src/SearchableHelpInterface.php +++ b/core/modules/help_topics/src/SearchableHelpInterface.php @@ -7,6 +7,10 @@ /** * Provides an interface for a HelpSection plugin that also supports search. * + * Note that in addition to these methods, plugins that support search should + * also call help_topics_update_help_search_index('reindex') in their + * clearCachedDefinitions() method. + * * @see \Drupal\help\HelpSectionPluginInterface * * @internal diff --git a/core/modules/help_topics/tests/src/Functional/HelpTopicSearchTest.php b/core/modules/help_topics/tests/src/Functional/HelpTopicSearchTest.php index 5102ca3e17..1aeafad973 100644 --- a/core/modules/help_topics/tests/src/Functional/HelpTopicSearchTest.php +++ b/core/modules/help_topics/tests/src/Functional/HelpTopicSearchTest.php @@ -53,6 +53,10 @@ protected function setUp() { // here. $this->rebuildContainer(); + // Before running cron, verify that a search returns no results. + $this->drupalPostForm('search/help', ['keys' => 'notawordenglish'], 'Search'); + $this->assertSearchResultsCount(0); + // Run cron until the topics are fully indexed, with a limit of 100 runs // to avoid infinite loops. $num_runs = 100; @@ -191,6 +195,12 @@ public function testHelpSearch() { $this->drupalPostForm('search/help', ['keys' => 'notawordenglish'], 'Search'); $this->assertSearchResultsCount(0); $session->pageTextContains('no results'); + + // Uninstall the test module and verify its topics are immediately not + // searchable. + \Drupal::service('module_installer')->uninstall(['help_topics_test']); + $this->drupalPostForm('search/help', ['keys' => 'nonworditem'], 'Search'); + $this->assertSearchResultsCount(0); } /**