Change a core root in Drupal 8

 

In this article, I will show you how to change an existing root path in Drupal 8. The same exercise was much easier in Drupal 7, but it was also by no means elegant.

In Drupal 7 you were just supposed to use hook_menu_alter - and there - you are basically able to change the path or whatever you will in an existing menu item (also permissions, callback functions, titles).

In Drupal 8 we have to declare a service as an event subscriber and then declare a custom class that extends in the general case the RouteSubscriberBase class.

We may take a look at an example module that I created to illustrate this problem in which we are overwriting the login path. It is actually something that I ended up doing very often as a precaution against bot attacks. Usually, as they find in the web-page headers that it is generated on Drupal - the next thing they do is to go to the /user page and start trying to login to your Drupal backend. If your user 1 is also with the username admin - it was the case that in Drupal 7 it was quite common that in big traffic pages - your user 1 with the admin username could end up blocked because a bot tried to log in 3 times with a wrong password. Two quick ways to tackle this is first to name your user 1 with a not so trivial name and the second good idea is to make your login page under a non-trivial path.

Let's take a look at an example in which we are moving our login page under a different path. 
We will call our module drupalup_route_alter. In it, under /src/Routing/ I created a PHP file called DrupalupRouteSubscriber.php in which we are going to have our route subscriber class.

<?php

namespace Drupal\drupalup_route_alter\Routing;

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

/**
 * Listens to the dynamic route events.
 */
class DrupalupRouteSubscriber extends RouteSubscriberBase {

  /**
   * {@inheritdoc}
   */
  protected function alterRoutes(RouteCollection $collection) {

    $user_login_route = $collection->get('user.login');
    $user_login_route->setPath('/hell/login');

    $user_route = $collection->get('user.page');
    $user_route->setPath('/hell');

  }
}

It is quite a simple example we are making our changes in the alterRoutes method. There we have a RouteCollection object, which we can use to get some routes and change them as we wish to. The actual id of the route - one could find in the route file of the module that actually declared this route. In my case, I found the user.login route by searching the /core folder in my Drupal 8 codebase and it showed me the routes that I want to alter in the user.routing.yml file of the user module. The  next thing I did is to get the user login route and then set another path with setPath - and voila - we have our user login under /hell/login

special login

Here how it actually looks like in the end. You may log in to your Drupal backend from /hell.

The full module's source code, as always - you could find on our GitHub page here.

Hope you find these code examples interesting and if you have any questions - feel free to ask in the comment section below.

 

Meet the author

Nikolay is a Drupal PHP developer with more than 7 years of experience. Passion for teaching, passion for simplicity and effectiveness of the code are just some keywords that he cares about. See more ...
Drupal Up is a platform for learning Drupal ...

Drupal Dan promoting
The idea is though not just to be another platform with some boring videos but all the videos to be inspired by real-world problems. That means something that you will most probably need if you work with Drupal. We want you to be successful and we want to teach you everything we know about Drupal!
So watch the videos, try to follow and reproduce, ask questions and you will see you will be able to conquer this wonderful sea that Drupal 8 is.