#! code: Drupal 9: Programmatically Creating And Using URLs And Links

Drupal CMS

This is probably not relevant to some people, but I find that I’m always searching for this information when I need to print out a URL or find the current page path.

The difficulty is that finding or printing out a URL is very contextual and there is more than one way to get or use this information in Drupal. You might have a node object that you need to convert into a fully qualified path, or you want to print out the path of a route, each of which have different approaches. I think that’s why there are so many questions asking variations of this topic on sites like Stackoverflow.

What is surprising to me is that there is very little documentation on drupal.org about this. Creating URLs and printing out links is perhaps the most common thing that needs to be done by developers creating themes, outside of changing the classes or markup of a block of HTML.

In this article I will go through some of the main things you should be aware of when using paths and URLs in Drupal and then look at examples of using each.

The main objects I will address here are SymfonyComponentHttpFoundationRequest, DrupalCoreUrl and DrupalCoreLink, all of which have their own uses. After that I will look at other ways in which links and URLs can be created.

The Request Object

When you respond to a page request in Drupal you will have access to a Request object. This is an instance of SymfonyComponentHttpFoundationRequest and can be used to tell you all about the current request. As you might see from the name, this is actually a Symfony component and therefore has no knowledge about Drupal.

This object is an important starting point when looking for URL information as it’s a good way of getting the current path, host, or other parameters being sent to the page.

Getting the Request object is a case of just asking Drupal for it. You can do this statically, like the following.

Read more.