3

Drupal links start showing internal:/ and entity: at the beginning of the link URI value. Those are useless. I need to remove internal:/ and entity: in order to make links reachable.

How to fix that that?

I get the URI value with:

paragraph.field_stage_element_hp_link.uri.value
Zatigem
  • 174
  • 1
  • 7

1 Answers1

-1

Edit: Use this method as a last resort. There are a lot of better ways to do this.

To get rid of those two annoying prefixes, you first must know where they come from. There are 3 types of link :

  • An internal link as /my-awesome-alias adds the prefix internal: to your URI,
  • A node link inserted with autocomplete is converted to a node URI and is like entity:/node/3 and thus adds the prefix entity: to your URI,
  • An external link as https://www.openstreetmap.org lets your URI as it is.

Treat those 3 cases with 3 conditions:

{% set uri = paragraph.field_stage_element_hp_link.uri.value %}

{% if uri matches '{^https?://}' %}
  {% set url = uri %}
{% elseif uri matches '{^internal:}' %}
  {% set url = uri|split(':')[1] %}
{% elseif uri matches '{^entity:}' %}
  {% set url = path('entity.node.canonical', {'node':  uri | split('/')[1]}) %}
{% endif %}

<a href="{{ url }}"> My awesome link </a>
Zatigem
  • 174
  • 1
  • 7