You are here

class WebformThemeNegotiator in Webform 8.5

Same name and namespace in other branches
  1. 6.x src/Theme/WebformThemeNegotiator.php \Drupal\webform\Theme\WebformThemeNegotiator

Sets the admin theme on a webform that does not have a public canonical URL.

Hierarchy

Expanded class hierarchy of WebformThemeNegotiator

1 string reference to 'WebformThemeNegotiator'
webform.services.yml in ./webform.services.yml
webform.services.yml
1 service uses WebformThemeNegotiator
webform.theme_negotiator in ./webform.services.yml
Drupal\webform\Theme\WebformThemeNegotiator

File

src/Theme/WebformThemeNegotiator.php, line 14

Namespace

Drupal\webform\Theme
View source
class WebformThemeNegotiator implements ThemeNegotiatorInterface {

  /**
   * The current user.
   *
   * @var \Drupal\Core\Session\AccountInterface
   */
  protected $user;

  /**
   * The config factory.
   *
   * @var \Drupal\Core\Config\ConfigFactoryInterface
   */
  protected $configFactory;

  /**
   * The webform request handler.
   *
   * @var \Drupal\webform\WebformRequestInterface
   */
  protected $requestHandler;

  /**
   * Creates a new WebformThemeNegotiator instance.
   *
   * @param \Drupal\Core\Session\AccountInterface $user
   *   The current user.
   * @param \Drupal\Core\Config\ConfigFactoryInterface $config_factory
   *   The config factory.
   * @param \Drupal\webform\WebformRequestInterface $request_handler
   *   The webform request handler.
   */
  public function __construct(AccountInterface $user, ConfigFactoryInterface $config_factory, WebformRequestInterface $request_handler = NULL) {
    $this->user = $user;
    $this->configFactory = $config_factory;

    // @todo Webform 8.x-6.x: Require request handler.
    $this->requestHandler = $request_handler ?: \Drupal::service('webform.request');
  }

  /**
   * {@inheritdoc}
   */
  public function applies(RouteMatchInterface $route_match) {
    return $this
      ->getActiveTheme($route_match) ? TRUE : FALSE;
  }

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

  /**
   * Determine the active theme for the current route.
   *
   * @param \Drupal\Core\Routing\RouteMatchInterface $route_match
   *   The current route match.
   *
   * @return string
   *   The active theme or an empty string.
   */
  protected function getActiveTheme(RouteMatchInterface $route_match) {
    $route_name = $route_match
      ->getRouteName();
    if (strpos($route_name, 'webform') === FALSE) {
      return '';
    }
    $webform = $this->requestHandler
      ->getCurrentWebform();
    if (empty($webform)) {
      return '';
    }
    $is_webform_route = in_array($route_name, [
      'entity.webform.canonical',
      'entity.webform.test_form',
      'entity.webform.confirmation',
      'entity.node.webform.test_form',
    ]);
    $is_user_submission_route = strpos($route_name, 'entity.webform.user.') === 0;

    // If webform route and page is disabled, apply admin theme to
    // the webform routes.
    if ($is_webform_route && !$webform
      ->getSetting('page')) {
      return $this->user
        ->hasPermission('view the administration theme') ? $this->configFactory
        ->get('system.theme')
        ->get('admin') : '';
    }

    // If webform and user submission routes apply custom page theme to
    // the webform routes.
    if (($is_webform_route || $is_user_submission_route) && $webform
      ->getSetting('page_theme_name')) {
      return $webform
        ->getSetting('page_theme_name');
    }
    return '';
  }

}

Members

Namesort descending Modifiers Type Description Overrides
WebformThemeNegotiator::$configFactory protected property The config factory.
WebformThemeNegotiator::$requestHandler protected property The webform request handler.
WebformThemeNegotiator::$user protected property The current user.
WebformThemeNegotiator::applies public function Whether this theme negotiator should be used to set the theme. Overrides ThemeNegotiatorInterface::applies
WebformThemeNegotiator::determineActiveTheme public function Determine the active theme for the request. Overrides ThemeNegotiatorInterface::determineActiveTheme
WebformThemeNegotiator::getActiveTheme protected function Determine the active theme for the current route.
WebformThemeNegotiator::__construct public function Creates a new WebformThemeNegotiator instance.