I am unable to fetch the content author name inside the twig template thorough views.

Below is what i did so far : - Created a relationship under the advanced tab in views and added the Authored by field.

I can see the author in the preview section of the view but since i have overridden the template with views-view-filed i cant fetch it in twig with {{ fields.author.user.name}}

enter image description here

Kint is not of much help as its freezing the browser.

  • which type of twig template you are creating? – Vikram fz16 Oct 10 at 13:44
  • @Vikramfz16 I am not sure whether i get your question Are you looking for : views-view-fields--VIEWNAME.html.twig – Jignesh Rawal Oct 10 at 13:46
  • 1
    Print this twig variable {{ fields.name.content }} in your views-view-fields--VIEWNAME.html.twig – Vikram fz16 Oct 10 at 13:56
  • @Vikramfz16 the ouput for your above request is null. Is there a way to print all the fields ? – Jignesh Rawal Oct 10 at 14:59
  • 1
    Could you make this a proper answer and accept it to help future readers to find the solution to that problem? – leymannx Oct 10 at 19:55

You can custom views, as you can see in views-view-unformatted.html.twig

Available variables:

  • title: The title of this group of rows. May be empty.
  • rows: A list of the view's row items.
    • attributes: The row's HTML attributes.
    • content: The row's content.
  • view: The view object.
  • default_row_class: A flag indicating whether default classes should be used on rows.

If you want to use fields in a view template, use views-view-fields--[view-name]--[machine-name].html.twig. There, you can print your fields like this:

<span>{{ fields.title.content }},</span><span>{{ fields.body.content }}</span><span>{{ fields.author.content }}</span>

Better way to answer this is through debugging the views fields by enabling the page_array check under the devel module settings.We get each and every fields that are available for the twig template.

There are two steps to get this working

Follow the steps mentioned in the comment:

  1. https://www.drupal.org/forum/support/post-installation/2011-06-26/drupal-72-views-adding-authors-name-of-a-node#comment-4919120

  2. You have to be careful while selecting the field .Please select the field marked in the attached image. enter image description here

  3. Then in the twig template views-view-fields--VIEWNAME.html.twig the variable to be used is {{fields.nane.content}}

In addition to the above while researching for the variable name i found that most of the field names commonly used are available under admin/reports/fields in the backend of Drupal 8

Your Answer

 
discard

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

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