ThemeNegotiator.php 2.82 KB
Newer Older
1 2 3 4 5 6 7 8 9
<?php

/**
 * @file
 * Contains \Drupal\Core\Theme\ThemeNegotiator.
 */

namespace Drupal\Core\Theme;

10
use Drupal\Core\Routing\RouteMatchInterface;
11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51

/**
 * 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.
 *
 * @see \Drupal\Core\Theme\ThemeNegotiatorPass
 * @see \Drupal\Core\Theme\ThemeNegotiatorInterface
 */
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.
   */
52
  public function __construct(ThemeAccessCheck $theme_access) {
53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90
    $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;
  }

  /**
91
   * {@inheritdoc}
92
   */
93
  public function applies(RouteMatchInterface $route_match) {
94 95 96 97 98 99
    return TRUE;
  }

  /**
   * {@inheritdoc}
   */
100
  public function determineActiveTheme(RouteMatchInterface $route_match) {
101
    foreach ($this->getSortedNegotiators() as $negotiator) {
102 103
      if ($negotiator->applies($route_match)) {
        $theme = $negotiator->determineActiveTheme($route_match);
104
        if ($theme !== NULL && $this->themeAccess->checkAccess($theme)) {
105
          return $theme;
106 107 108 109 110 111
        }
      }
    }
  }

}