You are here

public function HelpSearch::updateTopicList in Drupal 10

Same name and namespace in other branches
  1. 8 core/modules/help_topics/src/Plugin/Search/HelpSearch.php \Drupal\help_topics\Plugin\Search\HelpSearch::updateTopicList()
  2. 9 core/modules/help_topics/src/Plugin/Search/HelpSearch.php \Drupal\help_topics\Plugin\Search\HelpSearch::updateTopicList()

Rebuilds the database table containing topics to be indexed.

File

core/modules/help_topics/src/Plugin/Search/HelpSearch.php, line 388

Class

HelpSearch
Handles searching for help using the Search module index.

Namespace

Drupal\help_topics\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);
}