Is it possible to get only the next event in views?

Created on 8 December 2022, over 2 years ago
Updated 12 January 2023, about 2 years ago

Problem/Motivation

Our Event entity with a smart date fields outputs itself multiple times in views for every configured date. While this is probably and normaly a very desirable behaviour our customer does not want it.

Is there a way to dissable this behaviour?

If not, how to know, which tile is referencing which event? Currently every tile shows the same start date for us. (output via {{ content.field_event_date.0 }} )

Steps to reproduce

Create a view of event content types.
Put multiple dates to an event.
The event is outputted multiple times in the view (once for every date.)

Proposed resolution

Make ths behaviour onfigurable?

Remaining tasks

User interface changes

API changes

Data model changes

💬 Support request
Status

Active

Version

3.6

Component

Code

Created by

🇩🇪Germany Schoenef Unna

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Comments & Activities

Not all content is available!

It's likely this issue predates Contrib.social: some issue and comment data are missing.

  • 🇨🇦Canada plousia

    It's a bit late, but I had to do this recently and wrote it up on my blog .

    For all the details, you can read the blog post, but this is the basic technique to get the next date from a Smart Date field and display it in your view. You will put this code in your views-view--fields.html.twig template (renaming it for your particular view as needed, e.g. views-view--fields--view-machine-name--display-machine-name.html.twig). You should also be able to use this in the View UI rewritten field output:

    // Create new empty array to push future values to
    {% set future_dates = [] %}
    // Loop over all date field values; if they are greater than now, push them to the future_dates array. Note that Twig does not have a "push" filter; it's called "merge":
    {% for key in row._entity.field_event_dates|keys %}
        {% if row._entity.field_event_dates[key].value > now|date('U') %}
            {% set future_dates = future_dates|merge([row._entity.field_event_dates[key].value]) %}
        {% endif %}
    {% endfor %}
    
    // Create variables from the first item of the future_dates array for display. I had to display the date and time separately, hence two variables:
    {% set next_date = future_dates[0]|date('F d, Y') %}
    {% set next_time = future_dates[0]|date('h:i A T') %}
    

    You can then put the {{ next_date }} and {{ next_time }} variables wherever you need them in your code. Or just create one variable if you don't need to print the date and time separately.

    Note that I'm not sure how this will work with Twig caching; your mileage may vary.

Production build 0.71.5 2024