0

I have a view that uses the "View content," "edit content," and "view comments" fields. I can't figure out how to print these in a custom twig template.

I've tried:

{{ fields.comments_link.content }}
{{ fields.comments_link }}
{{ comments_link }}

Similar attempts to print the view and edit links didn't work either. Other fields print fine. Can someone point me in the right direction, or do I just need to call the NID into the view and print that into a link?

2
  • What View? What template? Install Twig Debug and then put {{ dd() }} to see what's available. When you added these fields to the view they should already be there though.
    – leymannx
    Jan 4, 2022 at 8:58
  • My issue has resolved itself. I've updated the post.
    – cofihouse
    Jan 5, 2022 at 1:06

1 Answer 1

1

Add "view content" & "edit content" link in view as follow

enter image description here

Then put views-view-table--articles.html.twig file in your theme's template directory and get fields as follow.

<div class="example-template">

{% for row in rows %}
  {% set titletxt = '' %}
  {% set viewnod = '' %}
  {% set editnode = '' %}
    {% for key, column in row.columns %}
       
      {% for field in column.fields %}                                    
        
        {% for content in column.content %}
          
          {% if field == 'title' %}
             {% set titletxt = content.field_output %}
          {% endif %}

          {% if field == 'view-node' %}
             {% set viewnod = content.field_output %}
          {% endif %}              

          {% if field == 'edit-node' %}
             {% set editnode = content.field_output %}
          {% endif %}

        {% endfor %}
         
      {% endfor %}
      
    {% endfor %}
    
    <div>
      <h3>
        {{titletxt}}
      </h3>
      <div>
        {{viewnod}}
      </div>
      <div>
        {{editnode}}
      </div>
    </div>
  
{% endfor %}

Please have a look, this might help you. Thanks

1
  • Strangely enough, it now prints with {{ field.view_node.content }}. I'm not sure why it wasn't printing. But my problem seems to have resolved itself. Thanks for this answer. It may help someone else.
    – cofihouse
    Jan 5, 2022 at 1:03

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