3 namespace Drupal\Core\Theme;
5 use Drupal\Core\Access\CsrfTokenGenerator;
6 use Drupal\Core\Config\ConfigFactoryInterface;
7 use Drupal\Core\Routing\RouteMatchInterface;
8 use Symfony\Component\HttpFoundation\RequestStack;
11 * Defines a theme negotiator that deals with the active theme on ajax requests.
13 * Many different pages can invoke an Ajax request to a generic Ajax path. It is
14 * almost always desired for an Ajax response to be rendered using the same
15 * theme as the base page, because most themes are built with the assumption
16 * that they control the entire page, so if the CSS for two themes are both
17 * loaded for a given page, they may conflict with each other. For example,
18 * Bartik is Drupal's default theme, and Seven is Drupal's default
19 * administration theme. Depending on whether the "Use the administration theme
20 * when editing or creating content" checkbox is checked, the node edit form may
21 * be displayed in either theme, but the Ajax response to the Field module's
22 * "Add another item" button should be rendered using the same theme as the rest
25 class AjaxBasePageNegotiator implements ThemeNegotiatorInterface {
28 * The CSRF token generator.
30 * @var \Drupal\Core\Access\CsrfTokenGenerator
32 protected $csrfGenerator;
37 * @var \Drupal\Core\Config\ConfigFactoryInterface
39 protected $configFactory;
44 * @var \Symfony\Component\HttpFoundation\RequestStack
46 protected $requestStack;
49 * Constructs a new AjaxBasePageNegotiator.
51 * @param \Drupal\Core\Access\CsrfTokenGenerator $token_generator
52 * The CSRF token generator.
53 * @param \Drupal\Core\Config\ConfigFactoryInterface $config_factory
55 * @param \Symfony\Component\HttpFoundation\RequestStack $request_stack
56 * The request stack used to retrieve the current request.
58 public function __construct(CsrfTokenGenerator $token_generator, ConfigFactoryInterface $config_factory, RequestStack $request_stack) {
59 $this->csrfGenerator = $token_generator;
60 $this->configFactory = $config_factory;
61 $this->requestStack = $request_stack;
67 public function applies(RouteMatchInterface $route_match) {
68 $ajax_page_state = $this->requestStack->getCurrentRequest()->request->get('ajax_page_state');
69 return !empty($ajax_page_state['theme']) && isset($ajax_page_state['theme_token']);
75 public function determineActiveTheme(RouteMatchInterface $route_match) {
76 $ajax_page_state = $this->requestStack->getCurrentRequest()->request->get('ajax_page_state');
77 $theme = $ajax_page_state['theme'];
78 $token = $ajax_page_state['theme_token'];
80 // Prevent a request forgery from giving a person access to a theme they
81 // shouldn't be otherwise allowed to see. However, since everyone is
82 // allowed to see the default theme, token validation isn't required for
83 // that, and bypassing it allows most use-cases to work even when accessed
84 // from the page cache.
85 if ($theme === $this->configFactory->get('system.theme')->get('default') || $this->csrfGenerator->validate($token, $theme)) {