1

I 'm hitting a brick wall on this one, so trying to reach out for help. I did try to research this but could not find anything helpful.

What I have: I have a content type of 'courses', which have title, code, credits and semester (semester ranges from 01 to 10, and it is a vocabulary and each course is linked to a term from this vocabulary).

What I want: I want to create a view page, which lists all courses, i.e. their title, code and credits, and those courses should be grouped (and sorted) by their semester.

Ideally, I want all this to be presented with an accordion, so for example the final page should be something like:

Course Title     | Course Code     |     Course Credits   {titles}

-- Semester 01 -- [ collapsible accordion header]
Course #1 title  | Course #1 Code  |     Course #1 credits
Course #2 title  | Course #2 Code  |     Course #2 credits
Course #3 title  | Course #3 Code  |     Course #3 credits
-- Semester 02 -- [ collapsible accordion header]
Course #4 title  | Course #4 Code  |     Course #4 credits
Course #5 title  | Course #5 Code  |     Course #5 credits
-- Semester 03 -- [ collapsible accordion header]
Course #6 title  | Course #6 Code  |     Course #6 credits
Course #7 title  | Course #7 Code  |     Course #7 credits
Course #8 title  | Course #8 Code  |     Course #8 credits
Course #9 title  | Course #9 Code  |     Course #9 credits

I hope this above sketch shows what I 'm trying to achieve.

I am new to Drupal and I don't have a good grasp of Views yet, so this probably adds to my confusion. I feel quite confident with twig and templates, so I tried to do it from there, but hit a problem.

What I did:

Created a new View, where I show content of type courses.

In it, I create a page of unformatted list of teasers and using filename suggestions with the debug mode enabled, I created the file:

node--view--[course_content_type_name].html.twig

I can write the accordion markup in there and everything, the problem is that the 'semester' info obviously pops up in every row...

The best I can come up with, is something like:

-- Semester 01 -- [ collapsible accordion header]
Course #1 title  | Course #1 Code  |     Course #1 credits
-- Semester 01 -- [ collapsible accordion header]
Course #2 title  | Course #2 Code  |     Course #2 credits
-- Semester 01 -- [ collapsible accordion header]
Course #3 title  | Course #3 Code  |     Course #3 credits
-- Semester 02 -- [ collapsible accordion header]
Course #4 title  | Course #4 Code  |     Course #4 credits
-- Semester 02 -- [ collapsible accordion header]
Course #5 title  | Course #5 Code  |     Course #5 credits
-- Semester 03 -- [ collapsible accordion header]
Course #6 title  | Course #6 Code  |     Course #6 credits
-- Semester 03 -- [ collapsible accordion header]
Course #7 title  | Course #7 Code  |     Course #7 credits
-- Semester 03 -- [ collapsible accordion header]
Course #8 title  | Course #8 Code  |     Course #8 credits
-- Semester 03 -- [ collapsible accordion header]
Course #9 title  | Course #9 Code  |     Course #9 credits

I tried with 'fields' instead of 'content', added those that I want and 'excluded' semester from showing, but that didn't really help.

I understand that the teaser template runs once for every 'row' so there is no way to really 'group' results according to semesters, which is what I want.

Is there a way to get all data somewhere and format it in one template file, so I can use loops and ifs etc. to 'group' and present my data the way I want ?

Sorry for the ridiculous length of this message .. I wanted to be as specific as possible in order to be easily understood...


This question seems fairly similar, but the very useful and detailed answer mentions a solution if not using a view mode/template. I want to use a template because it helps me control exactly how the data is shown. I 've found other tips and info here and there, but none could be of any real help.

What should I try in order to achieve what I want? Any help would be greatly appreciated, I 'm really stuck on this one ...

Please shed some drupal light towards my way ... :)

0

Is there a way to get all data somewhere and format it in one template file, so I can use loops and ifs etc. to 'group' and present my data the way I want ?

Unless I'm misunderstanding something, you're sort of describing the views-view-unformatted.html.twig template:

{#
/**
 * @file
 * Default theme implementation to display a view of unformatted rows.
 *
 * 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.
 *
 * @see template_preprocess_views_view_unformatted()
 *
 * @ingroup themeable
 */
#}
{% if title %}
  <h3>{{ title }}</h3>
{% endif %}
{% for row in rows %}
  {%
    set row_classes = [
      default_row_class ? 'views-row',
    ]
  %}
  <div{{ row.attributes.addClass(row_classes) }}>
    {{ row.content }}
  </div>
{% endfor %}

As you can probably infer from the comments, the template is rendered for each group of rows in an aggregated View, and the title of the group is made available. That seems like the perfect place to add your accordion markup, so overriding that will probably do the job.

You might find the Views template files docs useful in deciding on the best file name to use for the template.

  • thanks for your prompt reply. I ended up in this exact template file before on my own, but couldn't really do what I wanted. I couldn't get useful stuff out of the 'content' variable to help with markup. But I can see why you bring it up. It's 1.20am where I am, and I am quite exhausted. I will get back to it tomorrow morning with a clear head and hope I can understand and use it better. Thanks again. Will need to put the exact name of the view in the filename so that it doesn't apply to all unformatted list views, I guess. – thomas Mar 11 '18 at 23:22
  • I can't find the way to proceed with that .. The problem is, I can't get any meaningful data from within row.content - I need to get the 'semester' data so that I group rows according to their semester. row.content.field_semester outputs null. The closest I get is with: kint(row.content['#node']) where I see a: protected values -> array but I cant get anything out of there. I need the semester data to group my rows somehow (accordion should have the semester as clickable, top row, as shown in my first 'sketch') ... Any ideas? – thomas Mar 12 '18 at 12:30

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.