Assign, specify/choose the theme by route/page in Drupal 8/9/10

Specify admin theme by route example code

Conundrum; you have created a webform that depends on your theme for content, your admin theme in this case is not your desired preference.

Solution: a theme_negotiator

add a class in

my_module/src/Theme/SwitcherNegotiator.php

a service in:

my_module/my_module.service.yml

Dans le fichier mon_module.service.yml ajoute le yml suivant:

service:
  theme.negotiator.my_module:
    class: Drupal\mon_module\Theme\SwitcherNegotiator
    tags:
      - { name: theme_negotiator, priority: -29 }

In the classe, add the following php code:

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

namespace Drupal\my_module\Theme;

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

class SwitcherNegotiator implements ThemeNegotiatorInterface
{

  /**
   * {@inheritdoc}
   */
  public function applies(RouteMatchInterface $route_match)
  {
    $current_route = $route_match->getRouteName();
    // Necessary and matches condition in determineActiveTheme.
    if ($current_route == 'entity.webform_submission.edit_form') {
      return TRUE;
    }
    // Apply default theme
    return FALSE;
  }

  /**
   * {@inheritdoc}
   */
  public function determineActiveTheme(RouteMatchInterface $route_match)
  {
    // This condition does what we need it to do for this case.
    if ($route_match->getRouteName() == 'entity.webform_submission.edit_form')
    {
      // Utilisent le thème 'bootstrap' pour cette route/ce chemin.
      return 'bootstrap';
    }
    // Otherwise, claro like usual.
    return 'claro';
  }

}