You are here

class ThemeSwitcherNegotiator in Context 8.4

Same name and namespace in other branches
  1. 8 src/Theme/ThemeSwitcherNegotiator.php \Drupal\context\Theme\ThemeSwitcherNegotiator

Context Theme Switcher Negotiator.

Hierarchy

Expanded class hierarchy of ThemeSwitcherNegotiator

1 string reference to 'ThemeSwitcherNegotiator'
context.services.yml in ./context.services.yml
context.services.yml
1 service uses ThemeSwitcherNegotiator
theme.negotiator.context_themeswitcher in ./context.services.yml
Drupal\context\Theme\ThemeSwitcherNegotiator

File

src/Theme/ThemeSwitcherNegotiator.php, line 13

Namespace

Drupal\context\Theme
View source
class ThemeSwitcherNegotiator implements ThemeNegotiatorInterface {

  /**
   * ContextManager.
   *
   * @var \Drupal\context\ContextManager
   */
  private $contextManager;

  /**
   * Theme machine name.
   *
   * @var string
   */
  protected $theme;

  /**
   * A boolean indicating if the applies method has already been evaluated.
   *
   * @var bool
   */
  protected $evaluated;

  /**
   * Service constructor.
   *
   * @param \Drupal\context\ContextManager $contextManager
   *   ContextManager.
   */
  public function __construct(ContextManager $contextManager) {
    $this->contextManager = $contextManager;
    $this->evaluated = FALSE;
  }

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

    // If there is no Theme reaction set or this method has already been
    // executed, do not try to get active reactions, since this causes infinite
    // loop.
    if ($this->evaluated) {
      $this->evaluated = FALSE;
      return FALSE;
    }
    $theme_reaction = FALSE;
    foreach ($this->contextManager
      ->getContexts() as $context) {
      foreach ($context
        ->getReactions() as $reaction) {
        if ($reaction instanceof Theme) {
          $theme_reaction = TRUE;
          break;
        }
      }
    }
    if ($theme_reaction) {
      $this->evaluated = TRUE;
      foreach ($this->contextManager
        ->getActiveReactions('theme') as $theme_reaction) {
        $configuration = $theme_reaction
          ->getConfiguration();

        // Be sure the theme key really exists.
        if (isset($configuration['theme'])) {
          switch ($configuration['theme']) {
            case '_admin':
              $this->theme = \Drupal::config('system.theme')
                ->get('admin');
              return TRUE;
            case '_default':
              $this->theme = \Drupal::config('system.theme')
                ->get('default');
              return TRUE;
            default:
              $this->theme = $configuration['theme'];
              return TRUE;
          }
        }
      }
    }
    return FALSE;
  }

  /**
   * {@inheritdoc}
   */
  public function determineActiveTheme(RouteMatchInterface $route_match) {
    return $this->theme;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
ThemeSwitcherNegotiator::$contextManager private property ContextManager.
ThemeSwitcherNegotiator::$evaluated protected property A boolean indicating if the applies method has already been evaluated.
ThemeSwitcherNegotiator::$theme protected property Theme machine name.
ThemeSwitcherNegotiator::applies public function Whether this theme negotiator should be used to set the theme. Overrides ThemeNegotiatorInterface::applies
ThemeSwitcherNegotiator::determineActiveTheme public function Determine the active theme for the request. Overrides ThemeNegotiatorInterface::determineActiveTheme
ThemeSwitcherNegotiator::__construct public function Service constructor.