Switching theme on a specific term

Created on 4 October 2022, over 2 years ago
Updated 20 March 2023, about 2 years ago

Problem/Motivation

The Theme Switcher Rules module comes with an opportunity to switch theme on a specific taxonomy.
Setting up a theme switching rule, you can make your way to "Taxonomy" and check one or more taxonomies.

I haven't been able to make this work in any way. Even if a node contains a term in a checked taxonomy, no theme switching is performed.

If a taxonomy called "Subject" has the term "Maths" will Theme Switcher Rules somehow be able to switch to "Maths Theme" on nodes tagged with "Maths"? The UI seem to say no and the documentation is rather quiet on this subject.

The documentation does, however, say

"Because of this, it can also be easily extended to support additional custom conditions exposed by other modules."

Is there an example anywhere on how to do this IRL?

I want to do this fairly simple thing: "If a given node is tagged with a specific term from a specific taxonomy I want to view it with a specific theme"

I've also got a question for the path-part.

Neither
/*switch_to_the_maths_theme_please*
nor
/node/*switch_to_the_maths_theme_please*
accomplishes a theme switch on paths containing the "switch_to_the_maths_theme_please" bit.

I.e. https://betterdeeperlearning.com/introduction_to_maths?switch_to_the_mat...
fails to switch to the Maths theme.

Not finished. Switching on domains is also rather interesting (https://betterdeeperlearning.com/ gets Better Theme, https://shoddyshallowlearning.com/ gets Shoddy theme). How would one use the Theme Switcher Module to accomplish such a feat?

Steps to reproduce

Look in the documentation. Find no answer to these interesting questions.

Proposed resolution

If anyone is in the know, I'll gladly write some better and more extensive documentation.

Remaining tasks

Tell me, and I'll tell everyone efficiently.

✨ Feature request
Status

Active

Version

2.0

Component

Documentation

Created by

πŸ‡©πŸ‡°Denmark Steven Snedker

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Comments & Activities

Not all content is available!

It's likely this issue predates Contrib.social: some issue and comment data are missing.

  • πŸ‡©πŸ‡ͺGermany Anybody Porta Westfalica
  • πŸ‡ΊπŸ‡ΈUnited States dave.gutierrez0@gmail.com Florida

    Thanks very much @steven-snedker. I found this useful for overriding Theme Switcher rules for a given content type only if the node is associated with a given taxonomy term. Here's what I ended up with as a variation of what you did that only switches the theme for 'View' mode for a given node:

    /modules/custom/custom_module/src/Theme/ThemeNegotiator.php

    /**
     * @file
     * Contains \Drupal\custom_module\Theme\ThemeNegotiator
     */
    
    namespace Drupal\custom_module\Theme;
    
    use Drupal\Core\Routing\RouteMatchInterface;
    use Drupal\Core\Theme\ThemeNegotiatorInterface;
    
    class ThemeNegotiator implements ThemeNegotiatorInterface
    {
    
        /**
         * @param RouteMatchInterface $route_match
         * @return bool
         */
        public function applies(RouteMatchInterface $route_match)
        {
            return $this->negotiateRoute($route_match) ? true : false;
        }
    
        /**
         * @param RouteMatchInterface $route_match
         * @return null|string
         */
        public function determineActiveTheme(RouteMatchInterface $route_match)
        {
            return $this->negotiateRoute($route_match) ?: null;
        }
    
        /**
         * Function that does all of the work in selecting a theme
         * @param RouteMatchInterface $route_match
         * @return bool|string
         */
        private function negotiateRoute(RouteMatchInterface $route_match)
        {
            $node = $route_match->getParameter('node');
    
            // Check if the current route is the node canonical view (i.e., viewing the node)
            if (!empty($node) && $route_match->getRouteName() === 'entity.node.canonical') {
    
                // Ensure we have a node and it's of the type 'content_type'
                if (!empty($node) && $node->getType() == 'content_type') {
    
                    // Check if the node has a 'field_template' field and it's not empty
                    if ($node->hasField('field_template') && !$node->get('field_template')->isEmpty()) {
    
                        // Apply the 'theme_to_switch_to' theme
                        return 'theme_to_switch_to';
                    }
                }
            }
    
            return false;
        }
    }
    
    
  • πŸ‡§πŸ‡ͺBelgium tim-diels Belgium πŸ‡§πŸ‡ͺ

    You should look at Entity field condition β†’ as that provides this condition out of the box for you for nodes. It would be fairly easy to look how this is done and do this yourself for taxonomy and provide a feature request there. Maybe this can be added as documentation and this can be closed as works as designed?

Production build 0.71.5 2024