Skip to main content

Having access to a site’s URL in your templates can come in handy for many different use cases, such as determining if your on the admin/people page as apposed to admin/reports/dblog or admin/content.  These three pages might seem to bear on common relationship, however there is at least one.  Users.

I edited the user in the admin/people page so rather than showing just username, know shows:

  • Firstname
  • Lastname
  • Email
  • Username

However, in making this change I discovered that when on admin/content or admin/reports/dblog pages it would also so all the details.  So I needed to add an if statement to catch which page I was on.  Subsequently, you can get the current URL and parse it for a substring like this:

{% set site_url = url("<current>") %}
{% if 'people' in site_url['#markup']|render|render %}
  <div class="tw-pt-2">
    <div class="tw-inline-block tw-w-52 tw-text-gray-400">{{ name }}</div>
    <div class="tw-inline-block tw-pl-10 tw-text-gray-500">{{ mail }}</div>
  </div>
{% endif %}

Note, this doesn't show the full example of how to change the details, I wanted to focus on determining the people.

 

Other options

Absolute URL to the front page:

<a href="{{ url('<front>') }}">{{ 'Home'|t }}</a>

An absolute URL to a specific node:

<a href="{{ url('entity.node.canonical', { 'node': 123 }) }}">{{ 'Visit node 123'|t }}</a>

What about if you are working in a node template and have a node variable available, you can generate an absolute URL to the current node:

<a href="{{ url('entity.node.canonical', { 'node': node.id() }) }}">{{ 'Visit this page'|t }}</a>

 

Extra notes

If you are using url('<front>'), note this is an array.  I was using the 'front' is the following way

{% set frontUrl = url('<front>') %}

{% set path = 'related-projects' %}

{% set hrefPathTag = 'f[0]'|url_encode ~ '=' ~ 'tags:'|url_encode %}

{% for tag in items|slice(startPos, length) %}

  {% set tag = drupal_field('name', 'taxonomy_term', tag.content['#options'].entity.tid.value) %}

  {% set tagTitle = tag|render|striptags|trim %}

  {% set href = '?' ~ hrefPathTag ~ tagTitle|url_encode %}

  <a href="{{ frontUrl|render ~ path ~ href }}" alt="{{ tagTitle|title }}" class="tw-no-underline">

    <div class="dark-blue-tag">{{ tagTitle|title }}</div>

  </a>

{% endfor %}

So the 'front' path renders correctly, add |render

frontUrl|render

 

Related articles

Andrew Fletcher04 Apr 2025
Managing .gitignore changes
When working with Git, the .gitignore file plays a critical role in controlling which files and folders are tracked by version control. Yet, many developers are unsure when changes to .gitignore take effect and how to manage files that are already being tracked. This uncertainty can lead to...
Andrew Fletcher26 Mar 2025
How to fix the ‘Undefined function t’ error in Drupal 10 or 11 code
Upgrading to Drupal 10.4+ you might have noticed a warning in their code editor stating “Undefined function ‘t’”. While Drupal’s `t()` function remains valid in procedural code, some language analysis tools — such as Intelephense — do not automatically recognise Drupal’s global functions. This...
Andrew Fletcher17 Mar 2025
Upgrading to PHP 8.4 challenges with Drupal contrib modules
The upgrade from PHP 8.3.14 to PHP 8.4.4 presents challenges for Drupal 10.4 websites, particularly when dealing with contributed modules. While Drupal core operates seamlessly, various contrib modules have not yet been updated to accommodate changes introduced in PHP 8.4.x. This has resulted in...