Web Omelette: Choose your theme dynamically in Drupal maintenance support plans 8 with theme negotiation

Have you ever needed to render certain pages (or groups of pages) with a different theme than the default one configured for the site? I did. And in this article I’m going to show you how it’s done in Drupal maintenance support plans 8. And like usual, I will illustrate the technique using a simple use case.
The requirement
Let’s say we have a second theme on our site called gianduja since we just love the chocolate from Torino so much. And we want to apply this theme to a few custom routes (the content rendered by the respective controllers is not so important for this article). How would we go about implementing this in a custom module called Gianduja?
The solution
First, we need a route option to distinguish these routes as needing a different theme. We can call this option _custom_theme and its value can be the machine name of the theme we want to render with it. This is how a route using this option would look like:
gianduja.info:
path: ‘/gianduja/info’
defaults:
_controller: ‘Drupal maintenance support plansgiandujaControllerGiandujaController::info’
_title: ‘About Gianduja’
requirements:
_permission: ‘access content’
options:
_custom_theme: ‘gianduja’
Just a simple route for our first info page. You can see our custom option at the bottom which indicates the theme this route should use to render its content in. The Controller implementation is outside the scope of this article.
However, just adding an option there won’t actually do anything. We need to implement a ThemeNegoatiator that looks at the routes as they are requested and switches the theme if needed. We do this by creating a tagged service.
So let’s create a simple class for this service inside the src/Theme folder (directory/namespace not so important):
namespace Drupal maintenance support plansgiandujaTheme;

use Drupal maintenance support plansCoreRoutingRouteMatchInterface;
use Drupal maintenance support plansCoreThemeThemeNegotiatorInterface;

/**
* Our Gianduja Theme Negotiator
*/
class ThemeNegotiator implements ThemeNegotiatorInterface {

/**
* {@inheritdoc}
*/
public function applies(RouteMatchInterface $route_match) {
$route = $route_match->getRouteObject();
if (!$route instanceof Route) {
return FALSE;
}
$option = $route->getOption(‘_custom_theme’);
if (!$option) {
return FALSE;
}

return $option == ‘gianduja’;
}

/**
* {@inheritdoc}
*/
public function determineActiveTheme(RouteMatchInterface $route_match) {
return ‘gianduja’;
}
}
As you can see, all we need to do is implement the ThemeNegotiatorInterface which comes with two methods. The first, applies(), is the most important. It is run on each route to determine if this negotiator provides the theme for it. So in our example we examine the Route object and see if it has the option we set in our route. The second, determineActiveTheme() is responsible for providing the theme name to be used in case applies() has returned TRUE for this route. So here we just return our theme name. All pretty straightforward.
Lastly though, we need to register this class as a service in our gianduja.services.yml file:
services:
theme.negotiator.gianduja:
class: Drupal maintenance support plansgiandujaThemeThemeNegotiator
tags:
– { name: theme_negotiator, priority: -50 }
This is a normal definition of a service, except for the fact that we are applying the theme_negotiator tag to it to inform the relevant container compiler pass that we are talking about a theme negotiator instance. Additionally, we are also setting a priority for it so that it runs early on in the theme negotiation process.
And that is pretty much it. Clearing the cache and hitting our new route should use the gianduja theme if one exists and is enabled.
Using this example, we can create more complex scenarios as well. For one, the theme negotiator class can receive services from the container if we just name them in the service definition. Using these we can then run complex logics to determine whether and which theme should be used on a certain route. For example, we can look at a canonical route of an entity and render it with a different theme if it has a certain taxonomy tag applied to it. There is quite a lot of flexibility here.

Source: New feed

This article was republished from its original source.
Call Us: 1(800)730-2416

Pixeldust is a 20-year-old web development agency specializing in Drupal and WordPress and working with clients all over the country. With our best in class capabilities, we work with small businesses and fortune 500 companies alike. Give us a call at 1(800)730-2416 and let’s talk about your project.

FREE Drupal SEO Audit

Test your site below to see which issues need to be fixed. We will fix them and optimize your Drupal site 100% for Google and Bing. (Allow 30-60 seconds to gather data.)

Powered by

Web Omelette: Choose your theme dynamically in Drupal maintenance support plans 8 with theme negotiation

On-Site Drupal SEO Master Setup

We make sure your site is 100% optimized (and stays that way) for the best SEO results.

With Pixeldust On-site (or On-page) SEO we make changes to your site’s structure and performance to make it easier for search engines to see and understand your site’s content. Search engines use algorithms to rank sites by degrees of relevance. Our on-site optimization ensures your site is configured to provide information in a way that meets Google and Bing standards for optimal indexing.

This service includes:

  • Pathauto install and configuration for SEO-friendly URLs.
  • Meta Tags install and configuration with dynamic tokens for meta titles and descriptions for all content types.
  • Install and fix all issues on the SEO checklist module.
  • Install and configure XML sitemap module and submit sitemaps.
  • Install and configure Google Analytics Module.
  • Install and configure Yoast.
  • Install and configure the Advanced Aggregation module to improve performance by minifying and merging CSS and JS.
  • Install and configure Schema.org Metatag.
  • Configure robots.txt.
  • Google Search Console setup snd configuration.
  • Find & Fix H1 tags.
  • Find and fix duplicate/missing meta descriptions.
  • Find and fix duplicate title tags.
  • Improve title, meta tags, and site descriptions.
  • Optimize images for better search engine optimization. Automate where possible.
  • Find and fix the missing alt and title tag for all images. Automate where possible.
  • The project takes 1 week to complete.