You are here

class ThemeNegotiator in Zircon Profile 8

Same name and namespace in other branches
  1. 8.0 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
Contains \Drupal\Tests\Core\Theme\ThemeNegotiatorTest.
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 18
Contains \Drupal\Core\Theme\ThemeNegotiator.

Namespace

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

  /**
   * Holds arrays of theme negotiators, keyed by priority.
   *
   * @var array
   */
  protected $negotiators = array();

  /**
   * Holds the array of theme negotiators sorted by priority.
   *
   * Set to NULL if the array needs to be re-calculated.
   *
   * @var array|NULL
   */
  protected $sortedNegotiators;

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

  /**
   * Constructs a new ThemeNegotiator.
   *
   * @param \Drupal\Core\Theme\ThemeAccessCheck $theme_access
   *   The access checker for themes.
   */
  public function __construct(ThemeAccessCheck $theme_access) {
    $this->themeAccess = $theme_access;
  }

  /**
   * Adds a active theme negotiation service.
   *
   * @param \Drupal\Core\Theme\ThemeNegotiatorInterface $negotiator
   *   The theme negotiator to add.
   * @param int $priority
   *   Priority of the theme negotiator.
   */
  public function addNegotiator(ThemeNegotiatorInterface $negotiator, $priority) {
    $this->negotiators[$priority][] = $negotiator;

    // Force the negotiators to be re-sorted.
    $this->sortedNegotiators = NULL;
  }

  /**
   * Returns the sorted array of theme negotiators.
   *
   * @return array|\Drupal\Core\Theme\ThemeNegotiatorInterface[]
   *   An array of theme negotiator objects.
   */
  protected function getSortedNegotiators() {
    if (!isset($this->sortedNegotiators)) {

      // Sort the negotiators according to priority.
      krsort($this->negotiators);

      // Merge nested negotiators from $this->negotiators into
      // $this->sortedNegotiators.
      $this->sortedNegotiators = array();
      foreach ($this->negotiators as $builders) {
        $this->sortedNegotiators = array_merge($this->sortedNegotiators, $builders);
      }
    }
    return $this->sortedNegotiators;
  }

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

  /**
   * {@inheritdoc}
   */
  public function determineActiveTheme(RouteMatchInterface $route_match) {
    foreach ($this
      ->getSortedNegotiators() as $negotiator) {
      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::$negotiators protected property Holds arrays of theme negotiators, keyed by priority.
ThemeNegotiator::$sortedNegotiators protected property Holds the array of theme negotiators sorted by priority.
ThemeNegotiator::$themeAccess protected property The access checker for themes.
ThemeNegotiator::addNegotiator public function Adds a active theme negotiation service.
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::getSortedNegotiators protected function Returns the sorted array of theme negotiators.
ThemeNegotiator::__construct public function Constructs a new ThemeNegotiator.