You are here

public function AdvancedHelpSearch::updateIndex in Advanced Help 8

Updates the search index for this plugin.

This method is called every cron run if the plugin has been set as an active search module on the Search settings page (admin/config/search/pages). It allows your module to add items to the built-in search index by calling the index() method on the search.index service class, or to add them to your module's own indexing mechanism.

When implementing this method, your module should index content items that were modified or added since the last run. There is a time limit for cron, so it is advisable to limit how many items you index per run using config('search.settings')->get('index.cron_limit') or with your own setting. And since the cron run could time out and abort in the middle of your run, you should update any needed internal bookkeeping on when items have last been indexed as you go rather than waiting to the end of indexing.

Overrides SearchIndexingInterface::updateIndex

File

src/Plugin/Search/AdvancedHelpSearch.php, line 188

Class

AdvancedHelpSearch
Executes a keyword search for Advanced Help against the {advanced_help} topic pages.

Namespace

Drupal\advanced_help\Plugin\Search

Code

public function updateIndex() {

  // Interpret the cron limit setting as the maximum number of nodes to index
  // per cron run.
  $limit = (int) $this->searchSettings
    ->get('index.cron_limit');
  $language = \Drupal::languageManager()
    ->getCurrentLanguage()
    ->getId();
  $topics = $this
    ->getSids($this->advancedHelp
    ->getTopics());

  // If we got interrupted by limit, this will contain the last module
  // and topic we looked at.
  $last = \Drupal::state()
    ->get($this
    ->getPluginId() . '.last_cron', [
    'time' => 0,
  ]);
  $count = 0;
  foreach ($topics as $module => $module_topics) {

    // Fast forward if necessary.
    if (!empty($last['module']) && $last['module'] != $module) {
      continue;
    }
    foreach ($module_topics as $topic => $info) {

      // Fast forward if necessary.
      if (!empty($last['topic']) && $last['topic'] != $topic) {
        continue;
      }

      // If we've been looking to catch up, and we have, reset so we
      // stop fast forwarding.
      if (!empty($last['module'])) {
        unset($last['topic']);
        unset($last['module']);
      }
      $file = $this->advancedHelp
        ->getTopicFileName($module, $topic);
      if ($file && (empty($info['sid']) || filemtime($file) > $last['time'])) {
        if (empty($info['sid'])) {
          $info['sid'] = $this->database
            ->insert('advanced_help_index')
            ->fields([
            'module' => $module,
            'topic' => $topic,
            'langcode' => $language,
          ])
            ->execute();
        }
      }

      // Update index, using search index "type" equal to the plugin ID.
      if (floatval(\Drupal::VERSION) >= 8.800000000000001) {
        $this->searchIndex
          ->index($this
          ->getPluginId(), $info['sid'], $language, file_get_contents($file));
      }
      else {
        search_index($this
          ->getPluginId(), $info['sid'], $language, file_get_contents($file));
      }
      $count++;
      if ($count >= $limit) {
        $last['module'] = $module;
        $last['topic'] = $topic;
        \Drupal::state()
          ->set($this
          ->getPluginId() . '.last_cron', $last);
        return;
      }
    }
  }
  \Drupal::state()
    ->set($this
    ->getPluginId() . '.last_cron', [
    'time' => time(),
  ]);
}