Nick Greenup
Nick Greenup

Reputation: 81

Render a link field inside a paragraph template drupal 8

I have a paragraph called link. In this paragraph there is a single link field that allows multiple values. In the paragraph-link.html.twig file I want to render all of the links added to the paragraph. Instead I get the same link duplicated as many times as the number of link values. So if I add two links, it renders the first link twice. I also need it to work with external and internal links. Currently it only renders the external links properly (but only ever renders the first one).

        {% for item in paragraph.field_link %}
            <a href="{{ paragraph.field_link.uri }}">{{ paragraph.field_link.title }}</a>
        {% endfor %}

Thanks for the comments, the following is working for external links but not internal ones. Internal link URI renders as "internal:/"

{% for item in paragraph.field_link %}
    <a href="{{ item.uri }}">{{ item.title }}</a>
{% endfor %}

Upvotes: 2

Views: 3702

Answers (1)

Nick Greenup
Nick Greenup

Reputation: 81

With the help of the comments I was able to get this working as I wanted within the paragraph template. Below is the working code for a Link field that allows multiple values within a paragraph field.

{% for item in paragraph.field_link %} 
    <a href="{{ item.url }}">{{ item.title }}</a>
{% endfor %}

Upvotes: 4

Related Questions