class SearchHelpHooks

Same name and namespace in other branches
  1. 11.x core/modules/search/modules/search_help/src/Hook/SearchHelpHooks.php \Drupal\search_help\Hook\SearchHelpHooks

Hook implementations for help.

Hierarchy

Expanded class hierarchy of SearchHelpHooks

File

core/modules/search/modules/search_help/src/Hook/SearchHelpHooks.php, line 20

Namespace

Drupal\search_help\Hook
View source
class SearchHelpHooks {
  use StringTranslationTrait;
  public function __construct(#[Autowire(service: 'plugin.manager.search')] protected readonly SearchPluginManager $searchManager, #[Autowire(service: 'plugin.cache_clearer')] protected readonly CachedDiscoveryClearerInterface $pluginCacheClearer, protected readonly HelpHooks $helpHooks) {
  }
  
  /**
   * Implements hook_help() on behalf of the help module.
   *
   * \Drupal\help\Controller\HelpController::helpPage() only invokes the
   * implementation of the module whose page is requested, so the section about
   * help search has to be part of the help module implementation. This method
   * replaces it: it returns the original output and appends the section.
   */
  public function help($route_name, RouteMatchInterface $route_match) : string|array|null {
    $output = $this->helpHooks
      ->help($route_name, $route_match);
    if ($route_name !== 'help.page.help') {
      return $output;
    }
    $search_help = Url::fromRoute('help.page', [
      'name' => 'search',
    ])->toString();
    // Remove that trailing close description list tag.
    $output['#markup'] = rtrim($output['#markup'], "</dl>");
    $section = '<dt>' . $this->t('Configuring help search') . '</dt>';
    $section .= '<dd>' . $this->t('To search help, configure a search page and add a search block to the Help page or another administrative page. (A search page is provided automatically, and if you use the core Administrative theme, a help search block is shown on the main Help page.) Then users with search permissions, and permission to view help, will be able to search help. See the <a href=":search_help">Search module help page</a> for more information.', [
      ':search_help' => $search_help,
    ]) . '</dd>';
    $section .= '</dl>';
    $output['#markup'] .= $section;
    return $output;
  }
  
  /**
   * Implements hook_modules_uninstalled().
   */
  public function modulesUninstalled(array $modules) : void {
    $this->searchUpdate($modules);
  }
  
  /**
   * Implements hook_modules_installed().
   */
  public function modulesInstalled(array $modules, $is_syncing) : void {
    $this->searchUpdate();
  }
  
  /**
   * Implements hook_themes_installed().
   *
   * Implements hook_themes_uninstalled().
   */
  public function themesInstallOrUninstall(array $themes) : void {
    $this->pluginCacheClearer
      ->clearCachedDefinitions();
    $this->searchUpdate();
  }
  
  /**
   * Implements hook_rebuild().
   */
  public function rebuild() : void {
    if ($this->searchManager
      ->hasDefinition('help_search')) {
      $help_search = $this->searchManager
        ->createInstance('help_search');
      $help_search->markForReindex();
    }
  }
  
  /**
   * Ensure that search is updated when extensions are installed or uninstalled.
   *
   * @param string[] $extensions
   *   (optional) If modules are being uninstalled, the names of the modules
   *   being uninstalled. For themes being installed/uninstalled, or modules
   *   being installed, omit this parameter.
   */
  protected function searchUpdate(array $extensions = []) : void {
    // Early return if we're uninstalling this module.
    if (in_array('search_help', $extensions)) {
      return;
    }
    // Ensure that topics for extensions that have been uninstalled are removed
    // and that the index state variable is updated.
    $help_search = $this->searchManager
      ->createInstance('help_search');
    $help_search->updateTopicList();
    $help_search->updateIndexState();
  }

}

Members

Title Sort descending Modifiers Object type Summary Overrides
SearchHelpHooks::help public function Implements hook_help() on behalf of the help module.
SearchHelpHooks::modulesInstalled public function Implements hook_modules_installed().
SearchHelpHooks::modulesUninstalled public function Implements hook_modules_uninstalled().
SearchHelpHooks::rebuild public function Implements hook_rebuild().
SearchHelpHooks::searchUpdate protected function Ensure that search is updated when extensions are installed or uninstalled.
SearchHelpHooks::themesInstallOrUninstall public function Implements hook_themes_installed().
SearchHelpHooks::__construct public function
StringTranslationTrait::$stringTranslation protected property The string translation service. 3
StringTranslationTrait::formatPlural protected function Formats a string containing a count of items.
StringTranslationTrait::getNumberOfPlurals protected function Returns the number of plurals supported by a given language.
StringTranslationTrait::getStringTranslation protected function Gets the string translation service.
StringTranslationTrait::setStringTranslation public function Sets the string translation service to use. 2
StringTranslationTrait::t protected function Translates a string to the current language or to a given language. 1

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