You are here

ThemeSwitcherNegotiator.php in Context 8

Same filename and directory in other branches
  1. 8.4 src/Theme/ThemeSwitcherNegotiator.php

File

src/Theme/ThemeSwitcherNegotiator.php
View source
<?php

namespace Drupal\context\Theme;

use Drupal\context\ContextManager;
use Drupal\context\Plugin\ContextReaction\Theme;
use Drupal\Core\Routing\RouteMatchInterface;
use Drupal\Core\Theme\ThemeNegotiatorInterface;

/**
 * Context Theme Switcher Negotiator.
 */
class ThemeSwitcherNegotiator implements ThemeNegotiatorInterface {

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

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

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

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

    // If there is no Theme reaction set, do not try to get active reactions,
    // since this causes infinite loop.
    $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) {
      foreach ($this->contextManager
        ->getActiveReactions('theme') as $theme_reaction) {
        $configuration = $theme_reaction
          ->getConfiguration();
        $this->theme = $configuration['theme'];
        return TRUE;
      }
    }
    return FALSE;
  }

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

}

Classes

Namesort descending Description
ThemeSwitcherNegotiator Context Theme Switcher Negotiator.