You are here

class ThemeNegotiator in Drupal 9

Same name and namespace in other branches
  1. 8 core/lib/Drupal/Core/Theme/ThemeNegotiator.php \Drupal\Core\Theme\ThemeNegotiator

Provides a class which determines the active theme of the page.

It therefore uses ThemeNegotiatorInterface objects which are passed in using the 'theme_negotiator' tag.

Hierarchy

Expanded class hierarchy of ThemeNegotiator

1 file declares its use of ThemeNegotiator
ThemeNegotiatorTest.php in core/tests/Drupal/Tests/Core/Theme/ThemeNegotiatorTest.php
1 string reference to 'ThemeNegotiator'
core.services.yml in core/core.services.yml
core/core.services.yml
1 service uses ThemeNegotiator
theme.negotiator in core/core.services.yml
Drupal\Core\Theme\ThemeNegotiator

File

core/lib/Drupal/Core/Theme/ThemeNegotiator.php, line 14

Namespace

Drupal\Core\Theme
View source
class ThemeNegotiator implements ThemeNegotiatorInterface {

  /**
   * Holds an array of theme negotiator IDs, sorted by priority.
   *
   * @var string[]
   */
  protected $negotiators = [];

  /**
   * The access checker for themes.
   *
   * @var \Drupal\Core\Theme\ThemeAccessCheck
   */
  protected $themeAccess;

  /**
   * The class resolver.
   *
   * @var \Drupal\Core\DependencyInjection\ClassResolverInterface
   */
  protected $classResolver;

  /**
   * Constructs a new ThemeNegotiator.
   *
   * @param \Drupal\Core\Theme\ThemeAccessCheck $theme_access
   *   The access checker for themes.
   * @param \Drupal\Core\DependencyInjection\ClassResolverInterface $class_resolver
   *   The class resolver.
   * @param string[] $negotiators
   *   An array of negotiator IDs.
   */
  public function __construct(ThemeAccessCheck $theme_access, ClassResolverInterface $class_resolver, array $negotiators) {
    $this->themeAccess = $theme_access;
    $this->negotiators = $negotiators;
    $this->classResolver = $class_resolver;
  }

  /**
   * {@inheritdoc}
   */
  public function applies(RouteMatchInterface $route_match) {
    return TRUE;
  }

  /**
   * {@inheritdoc}
   */
  public function determineActiveTheme(RouteMatchInterface $route_match) {
    foreach ($this->negotiators as $negotiator_id) {
      $negotiator = $this->classResolver
        ->getInstanceFromDefinition($negotiator_id);
      if ($negotiator
        ->applies($route_match)) {
        $theme = $negotiator
          ->determineActiveTheme($route_match);
        if ($theme !== NULL && $this->themeAccess
          ->checkAccess($theme)) {
          return $theme;
        }
      }
    }
  }

}

Members

Namesort descending Modifiers Type Description Overrides
ThemeNegotiator::$classResolver protected property The class resolver.
ThemeNegotiator::$negotiators protected property Holds an array of theme negotiator IDs, sorted by priority.
ThemeNegotiator::$themeAccess protected property The access checker for themes.
ThemeNegotiator::applies public function Whether this theme negotiator should be used to set the theme. Overrides ThemeNegotiatorInterface::applies
ThemeNegotiator::determineActiveTheme public function Determine the active theme for the request. Overrides ThemeNegotiatorInterface::determineActiveTheme
ThemeNegotiator::__construct public function Constructs a new ThemeNegotiator.