Url parameter upcasting in drupal 8

In Drupal 8 there has been a significant change in the URL handling system. In contrary to Drupal 7 routing which was managed by hook_menu(), in Drupal 8 this has been replaced with the routing system.
Coming to URL parameters, sometimes the URL may contain one or more named parameters and we upcast them i.e we actually convert the parameters into an actual object or an array according to our need. Firstly let us have a look how would we implement this in Drupal 7.

In foo.module we will implement hook_menu as follows.

In the code above we have created a new route item which has a named parameter ‘%_bar’. Now if we want to access an object instead of the raw parameter inside the menu callback function, we have to write a function like below in our foo.module file.

Now whatever is returned from the load_bar function will be available to the menu callback. So as we can see this process is pretty simple in Drupal 7. Now to achieve the same in Drupal 8 we need to follow the below steps.

Step 1 – Adding a route

Let’s assume we have a custom module foo. So inside this module directory, I will create foo.routes.yml file and will add my route as follows.

So we have added one menu item ‘path/to/my/manu/{bar}’, where {bar} is the named parameter. In order to upcast it, we need to specify it inside “parameters” attribute of the route and have to assign a param converter service to it. Here we are using type: bar, which means we have to register a service named bar, which will carry out the necessary conversion.

Step 2 – Adding a param converter service

In our module, we will add

As you can see we have created one service named bar. The class FooParamConverter will take care of the parameter conversion. We have to make sure that the tag value contains the value paramconverter. This will help Drupal to build the service accordingly.

Step 3 – Implementing ParamConverterInterface

In the next step, we will have to implement Drupal\Core\ParamConverter\ParamConverterInterface interface to provide the logic the logic for the parameter upcasting.

We have implemented ParamConverterInterface in our class FooParamConverter and overridden the methods convert() and applies(). The former will contain the conversion logic and will return the converted value and the later one will contain the validation logic where the param conversion will be applicable. In our case we want to apply the conversion to the parameter named bar so we have written $definition[‘type’] == ‘bar’.

That’s it we will now get the converted value inside the callback class of our menu item.

One last point is if our URL parameter is a predefined entity e.g node or user,  we don’t need to follow the whole steps. For instance, if the parameter bar in our menu item is a node entity then foo.routes.yml will look something like below.

We just need to provide the parameter type as entity: node. And if the parameter is a user entity the type would be entity: user. And in the callback class, we will get the converted parameter.

About The Author

Malay Nayak

Acquia Certified Drupal8 Developer

Leave a Reply