Inside a view I have a block for which there is a field called Global Perspective.

Inside the block override , I have rewrite rules which overrides teh out of the field with custom text.

<div class="featured-media-tag">
{% if field_is_global_perspective__value %} 
    <a href="{{ path('view.global_memos.page_1', {'global_perspective' : 1}) }}" class="media-link">Global Perspectives</a>
{% else %}
    <a href="{{ path('view.global_memos.page_1') }}" class="media-link">Global Memos</a>
{% endif %}

This gives me the relative path, i.e /global-memos?global_perspective=1 for the a tag. When I click on my local the link it directs to mylocal.sj/global-memos?global_perspective=1 but the issue is on production i have my site homepage as example.com/mysite .

So when I click on the a tag on production, it is going here example.com/global-memos?global_perspective=1 , but expected is for it to go here example.com/mysite/global-memos?global_perspective=1.

I have tried to write a preprocess but failed at it. function subtheme_preprocess_views_view_fields(&$variables) {

  // Retrieve an array which contains the path pieces.
  $current_path = \Drupal::service('path.current')->getPath();

  // The hostname of the site
  $host = \Drupal::request()->getHost();

  if($host == "example.com") {
    $variables['pathname_id'] = "/councilofcouncils";


and then tried to add the pathname_id to the rewrite rules like this Global Perspectives

  • 2
    Have you tried <a href="{{ url('<current>') }}? – Danny Englander May 10 at 16:03
  • 1
    @DannyEnglander it worked. Thanks :) Please answer the question so I can mark it as correct – Stanley Fernandes Jun 15 at 14:57

You can try {{ url('<current>') }}. I could not find any documentation for this but it seems to work. For example, based on your code above, I can add the current URL like this in a Twig template:

<a href="{{ url('<current>') }}" class="media-link">
 {{ 'Global Memos'|t }}

If I am on a page with the url, example.com/councilofcouncils, the twig code above will render as:

<a href="http://example.com/councilofcouncils" class="media-link">
Global Memos

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.