Mark content in a view that is older than 30 days

Created on 25 October 2023, 8 months ago
Updated 30 October 2023, 8 months ago

I have a view showing events. The core date field is used to set the date of the events. Now i want to create a css class for content that is older than 30 days to style it differently. Is there a simple Drupal way to achieve this within a view?
Thanks in advance for any hint.

💬 Support request
Status

Active

Version

9.5

Component
Datetime  →

Last updated about 6 hours ago

Created by

🇩🇪Germany gaia

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

Comments & Activities

  • Issue created by @gaia
  • 🇮🇳India sidharth_soman Bangalore

    Not sure if this is the best way to go about doing this, but you can achieve this like so:

    1) Exclude all the fields in your view from display.
    2) Edit the event date field in your view to use a plain formatter. We need the date string only, not the html element.
    3) Add a 'Custom Text' field into your view. Here, we will render the event fields manually and add logic to differentiate the classes using twig templating.

    Here's the code I used to introduce a new class of old-event into the div which renders the individual events.

    {% set currentDate = "now"|date("U") %}
    {% set dateField =  field_event_date|date("U") %}
    {% set timeDifference = currentDate - dateField %}
    {% set daysDifference = timeDifference/86400 %}
    {% if daysDifference > 30 %}
    
    <div class="event-card old-event">
    {% else %}
    <div class="event-card">
    {% endif %}
    
    {{ title }}
    {{ field_event_date }}
    
    </div>
    
  • 🇩🇪Germany gaia

    Hey sidharth_soman! Thank you very much, this is exactly, what i needed.

Production build 0.69.0 2024