'Convert rows to columns using Twig - D8

I have a content type "Awards" that has data structured as below:

enter image description here

I want to group this data as below:

Want to group them on a "State" basis. So when I click on mysite.com/TXawards, it should show data grouped as below:

enter image description here

Similarly "/FLaward" page should show data grouped as above. I don't have a view created yet but the content type with data is ready. What would be the best way to achieve this? I was thinking to use a contextual filter but I click the link "/TXawards" which is hardcoded on a page. Any help on how to group this data? Thanks


UPDATE: I created a"Table" styled view and grouped the rows by Type and State. The view output looks as below. enter image description here

Can someone help with twigging this output to 2 column format pls?



Solution 1:[1]

so this answer that I have doesn't go about transposing your table using the twig templates. What this does is just give you the data in the direct format from Views and then you can style and format it however you'd like.

Since I don't have your fields or project I can't really give you a view export, however I can walk you through a few steps to set it up.

EDIT: I figured that I might as well toss in the export of the mock-up view I made to make these screenshots. Also, this concept should be applicable for Drupal 7-9, but the export linked may only work for 9.x

Export of Drupal v9.3.12 View that was built as concept for answer.

Mark all fields hidden, except one

Drupal views hidden fields

  • All the fields you want to show, make them hidden.
  • Whatever formatting you use will still be output for the each field (i.e. link, date format, etc.)
  • I typically keep all the fields I plan on outputting hidden, and for the last field I add like an ID content field or something.

Set the one last non-hidden field to re-write with tokens

Drupal views rewrite fields with replacement tokens

  • You can style the overall format however you want it to rewrite.
  • Your example is looking for a 2x4 table, so thats what I put in for mine.
  • All the variables that are wrapped in double brackets are taken from the Replacement Patterns accordion below.
  • Any field you included and set as hidden should be shown and be available as a token for rewrite.
  • For my example I did just basic fields, title, user, date, etc.

Set the Format > Format to be Unformatted list instead of Table

  • Now for this, if you really want, can make it any of the formats.
  • I think Unformatted list will give you the easiest markup to work with

Preview the results

Drupal views preview rewritten fields in an unformatted list format

Lastly, configure the view and everything else as necessary. Hope this helps!

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1