class ThemeSuggestionHooks

Same name and namespace in other branches
  1. main core/themes/admin/src/Hook/ThemeSuggestionHooks.php \Drupal\admin\Hook\ThemeSuggestionHooks

Provides theme suggestion hook implementations.

Hierarchy

Expanded class hierarchy of ThemeSuggestionHooks

File

core/themes/admin/src/Hook/ThemeSuggestionHooks.php, line 17

Namespace

Drupal\admin\Hook
View source
class ThemeSuggestionHooks {
  
  /**
   * Constructs the theme related hooks.
   */
  public function __construct(protected readonly RequestStack $requestStack, protected readonly PathMatcherInterface $pathMatcher, protected readonly RouteMatchInterface $routeMatch, protected ClassResolverInterface $classResolver) {
  }
  
  /**
   * Implements hook_theme_suggestions_HOOK_alter() for details.
   */
  public function details(array &$suggestions, array $variables) : void {
    if (!empty($variables['element']['#vertical_tab_item'])) {
      $suggestions[] = 'details__vertical_tabs';
    }
  }
  
  /**
   * Implements hook_theme_suggestions_HOOK_alter() for form.
   */
  public function form(array &$suggestions, array $variables) : void {
    $suggestions[] = 'form__' . str_replace('-', '_', $variables['element']['#id']);
  }
  
  /**
   * Implements hook_theme_suggestions_HOOK_alter() for form_element.
   */
  public function formElement(array &$suggestions, array $variables) : void {
    if (!empty($variables['element']['#type'])) {
      $suggestions[] = 'form_element__' . $variables['element']['#type'];
    }
  }
  
  /**
   * Implements hook_theme_suggestions_HOOK_alter() for maintenance_page.
   */
  public function maintenancePage(array &$suggestions) : void {
    try {
      $is_front = $this->pathMatcher
        ->isFrontPage();
    } catch (\Exception) {
      // An exception could mean that the database is offline. This scenario
      // should also be rendered using the frontpage template.
      $is_front = TRUE;
    }
    if ($is_front) {
      // Add theme suggestion for maintenance page rendered as front page. This
      // allows separating different applications such as update.php from the
      // actual maintenance page.
      $suggestions[] = 'maintenance_page__front';
    }
  }
  
  /**
   * Implements hook_theme_suggestions_HOOK_alter() for page.
   */
  public function page(array &$suggestions) : void {
    $path = $this->requestStack
      ->getCurrentRequest()?->getPathInfo();
    if ($path !== '/') {
      $path = trim($path, '/');
      $arg = str_replace([
        "/",
        '-',
      ], [
        '_',
        '_',
      ], $path);
      $suggestions[] = 'page__' . $arg;
    }
    // The node page template is required to use the node content form.
    if (!in_array('page__node', $suggestions, TRUE) && Helper::isContentForm()) {
      $suggestions[] = 'page__node';
    }
  }
  
  /**
   * Implements hook_theme_suggestions_HOOK_alter() for table.
   */
  public function table(array &$suggestions, array $variables) : void {
    if (empty($variables['attributes']['class'])) {
      return;
    }
    if (is_array($variables['attributes']['class']) && in_array('field-multiple-table', $variables['attributes']['class'], TRUE)) {
      $suggestions[] = 'table__simple';
    }
  }
  
  /**
   * Implements hook_theme_suggestions_HOOK_alter() for top_bar.
   */
  public function topBar(array &$suggestions) : void {
    $suggestions[] = 'top_bar__gin';
  }
  
  /**
   * Implements hook_theme_suggestions_HOOK_alter() for views_view_field.
   */
  public function viewsViewField(array &$suggestions, array $variables) : void {
    $field_name = $variables['field']->field;
    if ($field_name === 'status') {
      $suggestions[] = 'views_view_field__' . $field_name;
    }
  }

}

Members

Title Sort descending Modifiers Object type Summary
ThemeSuggestionHooks::details public function Implements hook_theme_suggestions_HOOK_alter() for details.
ThemeSuggestionHooks::form public function Implements hook_theme_suggestions_HOOK_alter() for form.
ThemeSuggestionHooks::formElement public function Implements hook_theme_suggestions_HOOK_alter() for form_element.
ThemeSuggestionHooks::maintenancePage public function Implements hook_theme_suggestions_HOOK_alter() for maintenance_page.
ThemeSuggestionHooks::page public function Implements hook_theme_suggestions_HOOK_alter() for page.
ThemeSuggestionHooks::table public function Implements hook_theme_suggestions_HOOK_alter() for table.
ThemeSuggestionHooks::topBar public function Implements hook_theme_suggestions_HOOK_alter() for top_bar.
ThemeSuggestionHooks::viewsViewField public function Implements hook_theme_suggestions_HOOK_alter() for views_view_field.
ThemeSuggestionHooks::__construct public function Constructs the theme related hooks.

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