A Major Step Forward for Smart Date

By mandclu, Tue, 05/02/2023 - 06:10
A cluttered clock-maker's bench

The planning for a 4.0 release of Smart Date began towards the end of 2021, but there were still lots of opportunities to fix bugs and add features without going to a major release. In the end, the need for a major release was triggered by Drupal 10, though in actuality by its updated PHP dependency, to use a minimum version of 8.1.

Smart Date had made some methods available to be used statically, that is, for the method to be used without instantiating a full object. This was a useful way to make capabilities available broadly, and drew inspiration from Drupal core static methods like Entity::create() or Url::fromRoute(). Newer versions of PHP, and in particular 8.1, are much more stringent about mixing static and non-static code. Calling non-static methods within a static one can generate errors.

In particular, an issue was identified using some Date Augmenters with Smart Date fields in Drupal 10. I was tempted to make more and more methods available statically, but ultimately there are practical limits. The best fix proved to be moving away from static methods, towards making the same methods available in a service. The result is a new service, smart_date.manager.

The most useful methods will likely be formatSmartDate(), which applies intelligent formatting to a date range based on the provided settings, and isAllDay(), which will test if a date range is all day, according to the convention used by Smart Date.

This new service recently came in handy as I was working on the Event Platform, to provide a new drag-and-drop interface for scheduling sessions:

Grid of available session slots, with a list of available sessions

For the date and time listings on the left, I wanted leverage the date_only and time_only formats that Smart Date provides by default. Using the new service made this very easy:

// Use the Smart Date service to format dates and times.
$format_settings = $this->smartDateManager->loadSmartDateFormat('date_only');
$row['date'] = $this->smartDateManager->formatSmartDate($value, $end_value, $format_settings, NULL, 'string');

// Use Smart Date's time only format to provide a concise time column.
$format_settings = $this->smartDateManager->loadSmartDateFormat('time_only');
$row['time'] = $this->smartDateManager->formatSmartDate($value, $end_value, $format_settings, NULL, 'string');

UPDATE: Thanks to an enhancement recently released in Smart Date 4.0.2, the above can now be achieved even more easily:

// Use the Smart Date service to format dates and times.
$row['date'] = $this->smartDateManager->formatSmartDate($value, $end_value, 'date_only', NULL, 'string');

// Use Smart Date's time only format to provide a concise time column.
$row['time'] = $this->smartDateManager->formatSmartDate($value, $end_value, 'time_only', NULL, 'string');

More Configurable Widgets

There was a long-standing request to make the "All day" checkbox something that can be removed, and the newest version of Smart Date delivers on that. For use cases where the ability to designate events as all day don't make sense, removing the option is as simple as unchecking a box in the configuration.

Screen capture of the widget configuration form, highlighting the new option

More Configurable Formatters

Many versions ago there was a request for a formatter that would show the start and duration instead of the formatted and deduplicated range. I realized that it wouldn't take a lot of work to extend the standard formatter to provide something new that would provide the duration instead.

More recently, an issue requested a start-only formatter. Again it wouldn't have been a lot of work to create this as another modified formatter, but I was wary of maintaining a growing list of formatters to address niche use cases. It occurred to me that which elements should be output could be part of the formatter's configuration, allowing a single class to provide tremendous flexibility in what would be output.

Screen capture of the formatter configuration highlighting the new Time Parts options

As a result of this new capability in the default formatter, the Duration formatter is now deprecated, and will be removed in a future 5.0.0 release.

A Grab Bag of Smaller Changes

Of course, the changes listed above only scratch the surface of the improvements you'll see if you upgrade to the 4.0.0 release from the 3.7.x branch. According to the reporting on drupal.org, however, more than half of the sites using Smart Date are using a version even older than that. I hope some of the new features above will inspire you to try out this latest version. And if you have any ideas on what would make a 5.0.0 release even more compelling, please open an issue and let me know.



Restricted HTML

  • Allowed HTML tags: <a href hreflang> <em> <strong> <cite> <blockquote cite> <code> <ul type> <ol start type> <li> <dl> <dt> <dd> <h2 id> <h3 id> <h4 id> <h5 id> <h6 id>
  • Lines and paragraphs break automatically.
  • Web page addresses and email addresses turn into links automatically.