Score:1

How can I display the NID in a views table template?

es flag

I want to take the NID field and display is as the row ID in a Views table. (I can then use that ID as an anchor to jump to the correct row in the table.)

However, I cannot for the life of me, find a way to 'print' the NID value in the views-view-table.html.twig template (I've changed the template name to match my view).

In a views-view-unformatted.html.twig template I've used the following (which works perfectly):

{{row.content['#row']._entity.nid[0].value}}

But that doesn't work in the table template.

Finding the correct syntax to print fields in different templates seems to be a mystery.

Thanks in advance for any help you can give.

Score:0
pw flag

I have tried template_preprocess_views_view_table function. You may also try in your theme's YOURTHEMENAME.theme file to override as follow.

function YOURTHEMENAME_preprocess_views_view_table(&$variables) {
  foreach ($variables['rows'] as $num => $row) {
    $row['attributes']['id'] = $variables['rows'][$num]['columns']['nid']['content'][0]['field_output']['#markup'];
  }
}

enter image description here

As in above the screen you can see NID field in view is added as id of row in table. Although its showing spaces around nid but id is added further you can refine it as per your requirement.

mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.