function HelpSearch::updateTopicList

Same name in other branches
  1. 9 core/modules/help_topics/src/Plugin/Search/HelpSearch.php \Drupal\help_topics\Plugin\Search\HelpSearch::updateTopicList()
  2. 8.9.x core/modules/help_topics/src/Plugin/Search/HelpSearch.php \Drupal\help_topics\Plugin\Search\HelpSearch::updateTopicList()
  3. 11.x core/modules/help/src/Plugin/Search/HelpSearch.php \Drupal\help\Plugin\Search\HelpSearch::updateTopicList()

Rebuilds the database table containing topics to be indexed.

3 calls to HelpSearch::updateTopicList()
HelpSearch::indexStatus in core/modules/help/src/Plugin/Search/HelpSearch.php
Reports the status of indexing.
HelpSearch::markForReindex in core/modules/help/src/Plugin/Search/HelpSearch.php
Marks the search index for reindexing for this plugin.
HelpSearch::updateIndex in core/modules/help/src/Plugin/Search/HelpSearch.php
Updates the search index for this plugin.

File

core/modules/help/src/Plugin/Search/HelpSearch.php, line 387

Class

HelpSearch
Handles searching for help using the Search module index.

Namespace

Drupal\help\Plugin\Search

Code

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();
    $old_list_ordered = [];
    $sids_to_remove = [];
    foreach ($old_list as $item) {
        $old_list_ordered[$item->section_plugin_id][$item->topic_id] = $item;
        $sids_to_remove[$item->sid] = $item->sid;
    }
    $section_plugins = $this->helpSectionManager
        ->getDefinitions();
    foreach ($section_plugins as $section_plugin_id => $section_plugin_definition) {
        $plugin = $this->getSectionPlugin($section_plugin_id);
        if (!$plugin) {
            continue;
        }
        $permission = $section_plugin_definition['permission'] ?? '';
        foreach ($plugin->listSearchableTopics() as $topic_id) {
            if (isset($old_list_ordered[$section_plugin_id][$topic_id])) {
                $old_item = $old_list_ordered[$section_plugin_id][$topic_id];
                if ($old_item->permission == $permission) {
                    // Record has not changed.
                    unset($sids_to_remove[$old_item->sid]);
                    continue;
                }
                // Permission has changed, update record.
                $this->database
                    ->update('help_search_items')
                    ->condition('sid', $old_item->sid)
                    ->fields([
                    'permission' => $permission,
                ])
                    ->execute();
                unset($sids_to_remove[$old_item->sid]);
                continue;
            }
            // New record, create it.
            $this->database
                ->insert('help_search_items')
                ->fields([
                'section_plugin_id' => $section_plugin_id,
                'permission' => $permission,
                'topic_id' => $topic_id,
            ])
                ->execute();
        }
    }
    // Remove remaining items from the index.
    $this->removeItemsFromIndex($sids_to_remove);
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.