You are here

class ThemeSwitcherNegotiator in Context 8

Same name and namespace in other branches
  1. 8.4 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;

  /**
   * 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;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
ThemeSwitcherNegotiator::$contextManager private property ContextManager.
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.