0

I have the following related Content types:

enter image description here

I am creating a view to get the following output

  • Resource Title
  • Resource Type. (If type is Quiz, then also show the following data)
    • Quiz Title
      • Option Title 1
      • Option Title 2

Currently, i am getting the following output

  • Resource Title
  • Resource Type
  • Quiz Title
  • Option Title 1

  • Resource Title

  • Resource Type
  • Quiz Title
  • Option Title 2

How can I group the data in the templates to get the desired result?

Thanks

0

An approach I have used at the moment is to use a preprocess hook in my template. Added the following preprocess template to a custom module

THEMENAME_preprocess_views_view_unformatted__PAGENAME(&$variables)

where variable is an array containing - view : the view object - rows : an array of row items and each row is an array of content.

I looped over the data and constructed my required object, which then I set as a new property resource (i.e. $variables['resource']) of variables.

Within the template (views-view-unformatted--PAGENAME.html.twig), I then have access to the resource variable to use in the twig template. e.g {{ resource.type }} , where type is a property added to the resource variable in the preprocess hook.

Not sure if this is the best way to approach the problem, but at least it is one way of approaching the problem.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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