Boy Baukema

Drupal 8: display user login in admin theme

Boy Baukema Drupal

Do you want your websites users to log in? For certain types you would, but often there is no need. The Ibuildings website does not allow you to register an account and log in. Why would it?

However by default Drupal 8 (and earlier) considers logging in on /user part of the frontend theme. This means that either you have to theme it or accept a horribly broken login page.

Or you could convince Drupal to apply the admin theme to user pages.

In Drupal 7 you can use the example given on the Drupal.org page for hook_admin_paths_alter().

In Drupal 8 you'll need to implement a custom RouteSubscriber. Here is what we have done for our new Drupal 8 site:

<?php

/**
 * @file
 * Contains \Drupal\field_group\Routing\RouteSubscriber.
 */

namespace Drupal\ib_core\Routing;

use Drupal\Core\Routing\RouteSubscriberBase;
use Symfony\Component\Routing\Route;
use Symfony\Component\Routing\RouteCollection;

/**
 * Subscriber for Field group routes.
 */
class RouteSubscriber extends RouteSubscriberBase {
  /**
   * {@inheritdoc}
   */
  protected function alterRoutes(RouteCollection $collection) {
    /* @var Route $route */
    foreach ($collection as $route_name => $route) {
      if (strpos($route_name, 'user.') !== 0) {
        continue;
      }

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

}

 

Next you need to register it as a service and tag it with "event_subscriber" ​*in*​ your services.yml:

services:
  ib_core.subscriber:
    class: Drupal\ib_core\Routing\RouteSubscriber
    tags:
     - { name: event_subscriber }

 

Finally you'll need to allow anonymous users to view the administration theme on "/admin/people/permissions":

Hope this helps!