Drupal 8/9 — Automatic alias generation for custom routes paths.

Rodrigo Espinoza Brenes
MANATI | Agencia Web
3 min readApr 26, 2021
Photo by 丁亦然 on Unsplash

Recently I have been working over some challenging projects, one of those required something that I have never thought about: automatically generate a url alias for a custom route path.

It's important to mention that in our usage case the final user wanted to still be able to edit the path alias via the Drupal UI if they required it, so that's why went for the automatic alias creation instead of creating a Path processor plugin. The path processor basically allows you to manipulate the inbound and outbound paths of your system, so, if your usage case does not requires post alias creation modification, I would recommend you to use a Path processor plugin.

So, How did I solved it ?

Since most of the custom Drupal routes are required to have a Controller class and I already have one in place, I decided to write my "automatic alias generator" there, but depending on "your" usage case you could also subscribe to the KernelEvents::REQUEST Drupal event and throw your amazing magic there.

Step 1: Define a custom route and assign to it a custom controller.

This could be achieve by defining the desired route into the routing file of your custom module.

Step 2: Create your custom controller.

Here is where the magic happens, our first step here is to verify if the path that the user just requested already has an alias associated with it:

So as you can see in the IF definition, we verify if the requested path is following the original "Machine URL" pattern. Then we use Drupal's alias repository service to check if the requested url already has a path alias associated to it. All of this is done to avoid the creation of duplicated path aliases.
Now the only thing that we require to do is generate our alias for the current path, but how?

First, prepare your path alias literals:

As you can see in the snipped, I'm just replacing white spaces and setting all the characters to lower case, but of course you could look for special characters and replace them if your usage case requires it. For this example I'm keeping it simple.

Once you have your strings ready to rock! you just need to create your beautiful alias like this:

As you can see, we're taking advantage of the controller class using the Entity type manager service that it has already built-in, to create our path alias and sending the required values for the entity right away.

And as a final touch, once our alias is properly saved we redirect the current user to the same page route, but why? Easy: our custom route path already has an alias associated to it, so, "reloading" the page will show the new path alias to the user right away.

Hope this is helpful to you as it was for me. I'm leaving the complete route controller below.

Manatí is a web consultancy firm from Costa Rica, where we make websites strategically designed to help organization achieve goals.

Don’t miss out on projects, news and ideas from Manatí. Learn about what we do, follow us on medium, twitter and facebook.

--

--

Rodrigo Espinoza Brenes
MANATI | Agencia Web

Happy backend developer at @estudiomanati, gadgets lover, geek and 35% robot… or maybe a little bit more…