class SearchThemeHooks

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

Theme hook implementations for search.

Hierarchy

Expanded class hierarchy of SearchThemeHooks

1 file declares its use of SearchThemeHooks
search.module in core/modules/search/search.module

File

core/modules/search/src/Hook/SearchThemeHooks.php, line 14

Namespace

Drupal\search\Hook
View source
class SearchThemeHooks {
  public function __construct(protected readonly LanguageManagerInterface $languageManager, protected readonly DateFormatterInterface $dateFormatter) {
  }
  
  /**
   * Implements hook_theme().
   */
  public function theme() : array {
    return [
      'search_result' => [
        'variables' => [
          'result' => NULL,
          'plugin_id' => NULL,
        ],
        'initial preprocess' => static::class . ':preprocessSearchResult',
      ],
    ];
  }
  
  /**
   * Prepares variables for individual search result templates.
   *
   * Default template: search-result.html.twig
   *
   * @param array $variables
   *   An array with the following elements:
   *   - result: Individual search result.
   *   - plugin_id: Plugin the search results came from.
   *   - title_prefix: Additional output populated by modules, intended to be
   *     displayed in front of the main title tag that appears in the template.
   *   - title_suffix: Additional output populated by modules, intended to be
   *     displayed after the main title tag that appears in the template.
   *   - title_attributes: HTML attributes for the title.
   *   - content_attributes: HTML attributes for the content.
   */
  public function preprocessSearchResult(array &$variables) : void {
    $language_interface = $this->languageManager
      ->getCurrentLanguage();
    $result = $variables['result'];
    $variables['url'] = UrlHelper::stripDangerousProtocols($result['link']);
    $variables['title'] = $result['title'];
    if (isset($result['langcode']) && $result['langcode'] != $language_interface->getId() && $result['langcode'] != LanguageInterface::LANGCODE_NOT_SPECIFIED) {
      $variables['title_attributes']['lang'] = $result['langcode'];
      $variables['content_attributes']['lang'] = $result['langcode'];
    }
    $info = [];
    if (!empty($result['plugin_id'])) {
      $info['plugin_id'] = $result['plugin_id'];
    }
    if (!empty($result['user'])) {
      $info['user'] = $result['user'];
    }
    if (!empty($result['date'])) {
      $info['date'] = $this->dateFormatter
        ->format($result['date'], 'short');
    }
    if (isset($result['extra']) && is_array($result['extra'])) {
      $info = array_merge($info, $result['extra']);
    }
    // Check for existence. User search does not include snippets.
    $variables['snippet'] = $result['snippet'] ?? '';
    // Provide separated and grouped meta information.
    $variables['info_split'] = $info;
    $variables['info'] = [
      '#type' => 'inline_template',
      '#template' => '{{ info|safe_join(" - ") }}',
      '#context' => [
        'info' => $info,
      ],
    ];
  }
  
  /**
   * Implements hook_theme_suggestions_HOOK().
   */
  public function themeSuggestionsSearchResult(array $variables) : array {
    return [
      'search_result__' . $variables['plugin_id'],
    ];
  }
  
  /**
   * Implements hook_preprocess_HOOK() for block templates.
   */
  public function preprocessBlock(&$variables) : void {
    if ($variables['plugin_id'] == 'search_form_block') {
      $variables['attributes']['role'] = 'search';
    }
  }

}

Members

Title Sort descending Modifiers Object type Summary
SearchThemeHooks::preprocessBlock public function Implements hook_preprocess_HOOK() for block templates.
SearchThemeHooks::preprocessSearchResult public function Prepares variables for individual search result templates.
SearchThemeHooks::theme public function Implements hook_theme().
SearchThemeHooks::themeSuggestionsSearchResult public function Implements hook_theme_suggestions_HOOK().
SearchThemeHooks::__construct public function

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