You are here

class PageThemeNegotiator in Switch Page Theme 8.2

Same name and namespace in other branches
  1. 8.3 src/Theme/PageThemeNegotiator.php \Drupal\switch_page_theme\Theme\PageThemeNegotiator
  2. 8 src/Theme/PageThemeNegotiator.php \Drupal\switch_page_theme\Theme\PageThemeNegotiator

Sets the selected theme on specified pages.

Hierarchy

Expanded class hierarchy of PageThemeNegotiator

1 string reference to 'PageThemeNegotiator'
switch_page_theme.services.yml in ./switch_page_theme.services.yml
switch_page_theme.services.yml
1 service uses PageThemeNegotiator
theme.negotiator.switch_page_theme in ./switch_page_theme.services.yml
Drupal\switch_page_theme\Theme\PageThemeNegotiator

File

src/Theme/PageThemeNegotiator.php, line 20

Namespace

Drupal\switch_page_theme\Theme
View source
class PageThemeNegotiator implements ThemeNegotiatorInterface {

  /**
   * Protected configFactory variable.
   *
   * @var Drupal\Core\Config\ConfigFactoryInterface
   */
  protected $configFactory;

  /**
   * Protected currentPath variable.
   *
   * @var Drupal\Core\Path\CurrentPathStack
   */
  protected $currentPath;

  /**
   * Protected pathAlias variable.
   *
   * @var Drupal\Core\Path\AliasManagerInterface
   */
  protected $pathAlias;

  /**
   * Protected pathMatcher variable.
   *
   * @var Drupal\Core\Path\PathMatcherInterface
   */
  protected $pathMatcher;

  /**
   * The proxy to the current user.
   *
   * @var \Drupal\Core\Session\AccountProxyInterface
   */
  protected $account;

  /**
   * The module handler.
   *
   * @var \Drupal\Core\Extension\ModuleHandlerInterface
   */
  protected $moduleHandler;

  /**
   * The Domain negotiator.
   *
   * @var \Drupal\domain\DomainNegotiatorInterface
   */
  protected $negotiator;

  /**
   * Request stack.
   *
   * @var \Symfony\Component\HttpFoundation\RequestStack
   */
  public $request;

  /**
   * The language manager.
   *
   * @var \Drupal\Core\Language\LanguageManagerInterface
   */
  protected $languageManager;

  /**
   * {@inheritdoc}
   */
  public function __construct(ConfigFactoryInterface $config_factory, CurrentPathStack $currentPath, AliasManagerInterface $pathAlias, PathMatcherInterface $pathMatcher, AccountProxyInterface $account, ModuleHandlerInterface $module_handler, RequestStack $request, DomainNegotiatorInterface $negotiator = NULL, LanguageManagerInterface $language_manager = NULL) {
    $this->configFactory = $config_factory;
    $this->currentPath = $currentPath;
    $this->pathAlias = $pathAlias;
    $this->pathMatcher = $pathMatcher;
    $this->account = $account;
    $this->moduleHandler = $module_handler;
    $this->request = $request;
    if ($negotiator) {
      $this->negotiator = $negotiator;
    }
    if ($language_manager) {
      $this->languageManager = $language_manager;
    }
  }

  /**
   * Select specified pages for specified role and apply theme.
   *
   * @param \Drupal\Core\Routing\RouteMatchInterface $route_match
   *   The current route match object.
   *
   * @return bool
   *   TRUE if this negotiator should be used or FALSE to let other negotiators
   *   decide.
   */
  public function applies(RouteMatchInterface $route_match) {
    global $theme;
    $applies = FALSE;

    // Get multiple configurations saved for different pages.
    $spt_table = $this->configFactory
      ->get('switch_page_theme.settings')
      ->get('spt_table');
    foreach ($spt_table as $value) {
      $condition = FALSE;

      // Check if rule is enabled.
      if ($value['status'] == 1) {

        // Check condition for basic rules.
        if ($this->request
          ->getCurrentRequest()->attributes
          ->get("_route") != "system.403" && ($this->pathMatcher
          ->matchPath($this->currentPath
          ->getPath(), $value["pages"]) || $this->pathMatcher
          ->matchPath($this->pathAlias
          ->getAliasByPath($this->currentPath
          ->getPath()), $value["pages"])) && !empty(array_intersect($value["roles"], $this->account
          ->getRoles()))) {
          $condition = TRUE;
        }

        // Check if domain module is enabled.
        if ($this->moduleHandler
          ->moduleExists('domain') && !empty($this->negotiator
          ->getActiveDomain())) {

          // Check condition for domain.
          if ($condition && !empty(array_intersect($value["domain"], [
            $this->negotiator
              ->getActiveDomain()
              ->id(),
          ]))) {
            $condition = TRUE;
          }
          else {
            $condition = FALSE;
          }
        }

        // Check if site is multilingual.
        if ($this->languageManager
          ->isMultilingual() || $this->moduleHandler
          ->moduleExists('language')) {

          // Check condition for language.
          if ($condition && !empty(array_intersect($value["language"], [
            $this->languageManager
              ->getCurrentLanguage()
              ->getId(),
          ]))) {
            $condition = TRUE;
          }
          else {
            $condition = FALSE;
          }
        }

        // Check if all conditions match.
        if ($condition) {
          $applies = TRUE;

          // Set the theme to apply on page into global variable.
          $theme = $value['theme'];
        }
      }
    }

    // Use the theme.
    return $applies;
  }

  /**
   * Determine the active theme for the request.
   *
   * @param \Drupal\Core\Routing\RouteMatchInterface $route_match
   *   The current route match object.
   *
   * @return string|null
   *   The name of the theme, or NULL if other negotiators, like the configured
   *   default one, should be used instead.
   */
  public function determineActiveTheme(RouteMatchInterface $route_match) {

    // Get theme to apply on page.
    global $theme;

    // Return the actual theme name.
    return $theme;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
PageThemeNegotiator::$account protected property The proxy to the current user.
PageThemeNegotiator::$configFactory protected property Protected configFactory variable.
PageThemeNegotiator::$currentPath protected property Protected currentPath variable.
PageThemeNegotiator::$languageManager protected property The language manager.
PageThemeNegotiator::$moduleHandler protected property The module handler.
PageThemeNegotiator::$negotiator protected property The Domain negotiator.
PageThemeNegotiator::$pathAlias protected property Protected pathAlias variable.
PageThemeNegotiator::$pathMatcher protected property Protected pathMatcher variable.
PageThemeNegotiator::$request public property Request stack.
PageThemeNegotiator::applies public function Select specified pages for specified role and apply theme. Overrides ThemeNegotiatorInterface::applies
PageThemeNegotiator::determineActiveTheme public function Determine the active theme for the request. Overrides ThemeNegotiatorInterface::determineActiveTheme
PageThemeNegotiator::__construct public function