Field formatter is incompatible with smart dates provided by smart date module

Created on 23 August 2024, 3 months ago
Updated 20 September 2024, about 2 months ago

Problem/Motivation

I have a site where I use smart dates to provide date functionality for events. I had a request for ical links to be added to these events. I realised that the date ical module was not compatible with the smart date fields even though it seemed to be on the display mode. After looking at the viewElements method in the field formatter, I noticed that there was no logic to handle smart dates.

Proposed resolution

Add logic to determine method used to create array of elements depending on whether it is a smart date, date range or date. Add additional method to handle creation of array elements for smart dates.

I've written a small patch to handle this, I haven't tested this on a variety of use cases but it covers mine and could be used for a more comprehensive solution.

Feature request
Status

Fixed

Version

4.0

Component

Other

Created by

🇬🇧United Kingdom kristiantosney

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

Comments & Activities

  • Issue created by @kristiantosney
  • Status changed to Needs review 3 months ago
  • 🇫🇷France lazzyvn paris

    This patch needs a lot of work
    $this->smartDateElements($items, $langcode);
    but methode smartDateElements has only $items without $langcode;
    and errors with phpcs
    395 | ERROR | [ ] Missing parameter comment
    395 | ERROR | [x] Data types in @param tags need to be fully namespaced
    397 | ERROR | [ ] Description for the @return value is missing
    I think smartDate must have a service to get the date and end date, it must use the service if it exists

    • lazzyvn committed 965224e6 on 4.x
      Issue #3469868: Field formatter is incompatible with smart dates...
  • Status changed to Fixed 2 months ago
  • Automatically closed - issue fixed for 2 weeks with no activity.

  • 🇬🇧United Kingdom kristiantosney

    It doesn't work to just have smart dates using the existing date range method as the way days are stored is different so I'd advise either using what I've added in the patch or adding something of your own rather than just trying to use the existing date range method. I've added an updated patch to work with the change you made.

  • 🇫🇷France lazzyvn paris

    why in your patch don't you check if value and end_date are not null?
    i think it's better if you can apply the patch by checking $item->get('value') and $item->get('start_date')
    something like this

    {
          /** @var \Drupal\Core\Datetime\DrupalDateTime $start_date */
          $start_date = $item->start_date ?? $item->value;
          /** @var \Drupal\Core\Datetime\DrupalDateTime $end_date */
          $end_date = $item->end_date;
    
Production build 0.71.5 2024