0

E.g. administration themes are used only on paths that start with /admin/, what if I want to use administration theme on paths like /user/, /users/* or user/login?

There is a module for that where you can set paths where to load your admin theme https://www.drupal.org/project/admin_theme, but my question is for Drupal 8.

1

You need a route subscriber, where you search for routes, which have a path you want to load with the admin theme, and add the option:

$route->setOption('_admin_route', TRUE);

More info Altering existing routes and adding new routes based on dynamic ones.

Example

There is an example in core. You can configure the admin theme for the node edit form in UI and this route subscriber is applying this configuration:

namespace Drupal\node\EventSubscriber;

use Drupal\Core\Config\ConfigFactoryInterface;
use Drupal\Core\Routing\RouteSubscriberBase;
use Symfony\Component\Routing\RouteCollection;

/**
 * Sets the _admin_route for specific node-related routes.
 */
class NodeAdminRouteSubscriber extends RouteSubscriberBase {

  /**
   * The config factory.
   *
   * @var \Drupal\Core\Config\ConfigFactoryInterface
   */
  protected $configFactory;

  /**
   * Constructs a new NodeAdminRouteSubscriber.
   *
   * @param \Drupal\Core\Config\ConfigFactoryInterface $config_factory
   *   The config factory.
   */
  public function __construct(ConfigFactoryInterface $config_factory) {
    $this->configFactory = $config_factory;
  }

  /**
   * {@inheritdoc}
   */
  protected function alterRoutes(RouteCollection $collection) {
    if ($this->configFactory->get('node.settings')->get('use_admin_theme')) {
      foreach ($collection->all() as $route) {
        if ($route->hasOption('_node_operation_route')) {
          $route->setOption('_admin_route', TRUE);
        }
      }
    }
  }

}
  • I wish I knew how to code. – user3108268 Oct 30 '17 at 12:02

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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