Drupal 8/9: Theme negotiation based on condition plugins

Case type:

Service area: 


Sooner, the European Commission Drupal project I'm working on, will transition to a new theme. During the transition, some pages will be still displayed using the the old theme. I've looked around for a Drupal module that knows to render a certain page using a specific theme and found Switch Page Theme. The big impediment with this module is that it mainly checks the page path. And this is more or less hardcoded. There's no way to add arbitrary conditions, such as a route condition, which is the one I'm more interesting in.

As proposing a change to the existing module would have been a too disruptive and hard to address in terms of backwards compatibility issues, I've built the Theme Negotiation by Rules module, which has and open architecture, allowing third-party modules to hook in by providing condition plugins. The Theme Negotiation by Rules module allows a site builder to show pages using different themes based on rules. Each rule contains the theme, and the conditions to be met in order to show the that theme. Conditions are condition plugins, such as page path pattern, current user role, current language or, even, the type of the node, when the page is a node canonical page.

As my theme selection is mainly based on routes, all I have to do is to install Route Condition and Theme Negotiation by Rules modules and to use the UI to define my theme rules.