0

I'm trying to use the ThemeNegotiator to change the theme of the add and edit pages of one content type. I've followed the following very extensive posts/blogs.

Don't use admin theme for adding editing specific content type

Dynamic theme switching in Drupal 8

Soft launching your new drupal theme

Choose your theme dynamically Drupal 8

I have a user that is allowed to see the Admin theme seven, but for one content type documenten I want him to see the front-end theme svds. I'm able to get this to work for front-end pages but not for admin pages. What I think is that the applies part doesn't match on the add and edit pages. So it doesn't know it's on a documenten content type. Does anyone has an idea on how to solve this?

Module structure:

theme_switcher
--src
----Theme
------SwitcherNegotiator.php
--theme_switcher.info.yml
--theme_switcher.services.yml

theme_switcher.info.yml:

name: Theme Switcher
type: module
description: 'Switch theme for specific Content Type'
package: SVDS
core: 8.x

theme_switcher.services.yml:

services:
    theme.negotiator.theme_switcher:
        class: Drupal\theme_switcher\Theme\SwitcherNegotiator
        tags:
          - { name: theme_negotiator, priority: -50 }

SwitcherNegotiator.php:

<?php
/**
 * @file
 * Contains \Drupal\theme_switcher\Theme\SwitcherNegotiator
 */

namespace Drupal\theme_switcher\Theme;

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

class SwitcherNegotiator implements ThemeNegotiatorInterface
{
    /**
     * {@inheritdoc}
     */
    public function applies(RouteMatchInterface $route_match)
    {
        // Use this theme on a certain route.
        $node = $route_match->getParameter('node');
        if (!is_null($node) && $node instanceof \Drupal\node\Entity\Node) {
            return $node->getType() == 'documenten';
        }

        // apply default theme
        return false;
    }

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

1 Answer 1

2

The admin theme negotiator is tagged with a priority of -40:

user.services.yml

  theme.negotiator.admin_theme:
    class: Drupal\user\Theme\AdminNegotiator
    arguments: ['@current_user', '@config.factory', '@entity_type.manager', '@router.admin_context']
    tags:
      - { name: theme_negotiator, priority: -40 }

So you have to use at least -39, since the first negotiator returning a theme name wins and on an admin page this core service will return the admin theme.

4
  • Thanks for the quick reply, this solves the problem when editing a node. But not when adding one. So /node/add/documenten still shows the admin theme.
    – Joost
    Commented May 22, 2019 at 19:51
  • That's a different route /node/add/{node_type}, so you have to check for the node type as route parameter.
    – 4uk4
    Commented May 22, 2019 at 20:21
  • See drupal.stackexchange.com/a/249297/47547 for a code example which covers both cases.
    – 4uk4
    Commented May 23, 2019 at 8:56
  • Just what I needed! Thanks.
    – Joost
    Commented May 23, 2019 at 14:24

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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