0

This question is an exact duplicate of:

I need to show my website with a "theme" when in header's requests is present a particular key/value.

In Drupal 7 I do this via themekey, but this module does not exist in Drupal 8.

Are there alternatives, or other ways to do this?

marked as duplicate by Pierre.Vriens, Clive Jan 29 '18 at 14:59

This question was marked as an exact duplicate of an existing question.

0

You can easily recreate your required feature by implementing a custom theme negotiator. This requires a little custom programming.

First, create an own module (in the following example, we'll call it mymodule). Within that module, create a mymodule.services.yml file, that lets Drupal know about your theme negotiator service:

services:
  mymodule.theme_negotiator:
    class: Drupal\mymodule\Theme\MyModuleThemeNegotiator
    arguments: ['@request_stack']
    tags:
      - { name: theme_negotiator, priority: 1000 }

The important parts here are the service tag theme_negotiator and a high enough priority, that ensures the negotiator will be called before any other negotiators. If you want to just change the default theme dynamically but let route specific theme negotiators still apply, use a priority < 0.

As you can see, we also mentioned your custom theme negotiator service class MyModuleThemeNegotiator located in the namespace Drupal\mymodule\Theme, and as argument the current request stack (as we can use it to get the request headers).

So let's create a folder src/Theme in your module and add a MyModuleThemeNegotiator.php like this:

namespace Drupal\mymodule\Theme;

use Drupal\Core\Routing\RouteMatchInterface;
use Drupal\Core\Theme\ThemeNegotiatorInterface;
use Symfony\Component\HttpFoundation\RequestStack;

/**
 * My Custom Theme Negotiator.
 */
class MyModuleThemeNegotiator implements ThemeNegotiatorInterface {

  /**
   * The request stack.
   *
   * @var \Symfony\Component\HttpFoundation\RequestStack
   */
  protected $requestStack;

  /**
   * Constructs a MyModuleThemeNegotiator object.
   *
   * @param \Symfony\Component\HttpFoundation\RequestStack $requestStack
   *   The request stack.
   */
  public function __construct(RequestStack $request_stack) {
    $this->requestStack = $request_stack;
  }

  /**
   * {@inheritdoc}
   */
  public function applies(RouteMatchInterface $route_match) {
    // This method must return TRUE, if your headers
    // fulfill the desired requirements.
    if (
      $this->requestStack->getCurrentRequest()->headers->has('myheader')
      && $value = $this->requestStack->getCurrentRequest()->headers->get('myheader')
      && $value == 'myvalue'
    ) {
      return TRUE;
    }

    return FALSE;
  }

  /**
   * {@inheritdoc}
   */
  public function determineActiveTheme(RouteMatchInterface $route_match) {
    // This method returns the ID of the theme that shall be
    // used, if your theme negotiator applies.
    return 'mytheme';
  }
}

Alter the header/values to match your requirements, clear your caches, and your theme should be applied.

Not the answer you're looking for? Browse other questions tagged or ask your own question.