Mark content in a view that is older than 30 days

Created on 25 October 2023, about 1 year ago
Updated 11 August 2024, 3 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

Fixed

Version

11.0 🔥

Component
Datetime 

Last updated about 4 hours ago

Created by

🇩🇪Germany gaia

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.

  • 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.

  • Status changed to Fixed 4 months ago
  • Automatically closed - issue fixed for 2 weeks with no activity.

Production build 0.71.5 2024