Skip to main content
Drupal 8

Main navigation

  • Home
    • Question And Answer
    • NEWS
    • Projects
  • Tutorials - Drupal
    • Android
    • Other
User account menu
  • Log in

Breadcrumb

  1. Home

Theme Negotiators - Switch theme dynamically

By editor , 30 October, 2016
Question

How to Switch theme dynamically on drupal 8 , using theme negotiators ?

Create a custom module (here : example)

For active themes by route you first have to define a service in your $module.services.yml file:


On example.services.yml

Add:

services:
  theme.negotiator.example:
    class: Drupal\example\Theme\ExampleNegotiator
    tags:
      - { name: theme_negotiator, priority: 0 }

and then use that new defined service class:
Create a class ExampleNegotiator on example/src/Theme With:

namespace Drupal\example\Theme;

use Drupal\Core\Theme\ThemeNegotiatorInterface;
use Drupal\Core\Routing\RouteMatchInterface;

class ExampleNegotiator implements ThemeNegotiatorInterface {
  /**
   * {@inheritdoc}
   */
  public function applies(RouteMatchInterface $route_match) {
    // Use this theme on a certain route.
    return $route_match->getRouteName() == 'example_route_name';
  }

  /**
   * {@inheritdoc}
   */
  public function determineActiveTheme(RouteMatchInterface $route_match) {
    // Here you return the actual theme name.
    return 'stark';//The theme name
  }
}

 

Tags

  • Drupal 8
  • Code
  • Theming
  • Add new comment

Comments1

Plain text

  • No HTML tags allowed.
  • Lines and paragraphs break automatically.
  • Web page addresses and email addresses turn into links automatically.

sankarprakash (not verified)

8 years 8 months ago

Drupal 8 Switching Theme Programmatically

I followed the below article here you can find how to switch your theme

Drupal 8 Switching Theme Programmatically - http://www.bestbloggercafe.com/switching-themes-programmatically-in-drupal-8/

  • Reply

Links

  • Drupal.org
  • Drupal core release cycle
  • Drupal events with ease
  • Unit test with Drupal using PHPUnit (Drupal 10)
  • Tokens for file upload url
  • Tips for Docker
  • CKEditor inline image files usage for custom forms.
  • Install Docker on Linux Mint
  • Install PHP 8.1
  • Drupal Security Best Practices
  • Order and sort items with tabledrag
  • Add a custom service YAML container
  • Apply a patch with composer