Problem/Motivation

This is pretty obscure, but I ran into it, so I thought I'd report it. If the end date in a smart date range widget is left blank and an editor clicks on anything that triggers an ajax call (e.g. a media upload button), the page errors silently. The media button fails but there's no message to the user about why.

The failure is due to the following error being thrown:

Error: Call to a member function getTimestamp() on array in Drupal\smart_date\Plugin\Field\FieldWidget\SmartDateWidgetBase->massageFormValues() (line 284 of /var/www/html/web/modules/composer/smart_date/src/Plugin/Field/FieldWidget/SmartDateWidgetBase.php)

And I believe the issue is that while the code makes sure that the start date value is not empty and is a DrupalDateTime, it does not check the end date before calling getTimezone() on it.

   if (!empty(<strong>$item['value'])</strong> && $item['value'] instanceof DrupalDateTime) {
        if (!$timezone) {
          $value_tz = $item['value']->getTimezone();
          $value_tz_name = $value_tz->getName();
          if (SmartDateTrait::isAllDay(
            $item['value']->getTimestamp(),
            <strong>$item['end_value']</strong>->getTimestamp(),
            $value_tz_name
          ) && $value_tz_name != $site_tz_name) {
            // Make sure all day events explicitly save timezone if different
            // from the site.
            $timezone = $value_tz;
            $item['timezone'] = $value_tz_name;
          }
        }
        // Adjust the date for storage.
        $item['value'] = $this->smartGetTimestamp($item['value'], $timezone);
      }

Again, I know this obscure, but we may want to prevent the silent error.

Comments

jastraat created an issue. See original summary.

remotedots’s picture

i am noticing a similar issue, but the following output is generated:

The website encountered an unexpected error. Please try again later.
Error: Call to a member function getTimestamp() on array in Drupal\smart_date\Plugin\Field\FieldWidget\SmartDateWidgetBase->massageFormValues() (line 284 of modules/contrib/smart_date/src/Plugin/Field/FieldWidget/SmartDateWidgetBase.php).

Drupal\smart_date\Plugin\Field\FieldWidget\SmartDateWidgetBase->massageFormValues(Array, Array, Object) (Line: 390)
Drupal\Core\Field\WidgetBase->extractFormValues(Object, Array, Object) (Line: 232)
...

checking the 'end_value' in the same way as 'value' is checked seems to solve the issue:

      if (!empty($item['value']) && $item['value'] instanceof DrupalDateTime) {
        if (!$timezone) {
          $value_tz = $item['value']->getTimezone();
          $value_tz_name = $value_tz->getName();
          if (!empty($item['end_value']) && $item['end_value'] instanceof DrupalDateTime) {
            if (SmartDateTrait::isAllDay(
              $item['value']->getTimestamp(),
              $item['end_value']->getTimestamp(),
              $value_tz_name
            ) && $value_tz_name != $site_tz_name) {
              // Make sure all day events explicitly save timezone if different
              // from the site.
              $timezone = $value_tz;
              $item['timezone'] = $value_tz_name;
            }
          }
        }

  • mandclu committed c61e224 on 3.4.x
    Issue #3232423 by remotedots, mandclu: If end date left blank, smart...
mandclu’s picture

Status: Active » Fixed

Added a check to make sure the end_value is a DrupalDateTime instance as well. Feel free to reopen if more work is needed.

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.

lincoln-batsirayi’s picture

@mandclu so I'm getting this same error, when i try to add a media item before entering any values in the date range. Although the error I'm getting is similar to the one on this issue: https://www.drupal.org/project/smart_date/issues/3207261

I'm using version 3.4.3 of the module. The exact error I'm getting is:

"\nAn AJAX HTTP error occurred.\nHTTP Result Code: 500\nDebugging information follows.\nPath: /node/2/edit?ajax_form=1&_wrapper_format=drupal_ajax\nStatusText: Internal Server Error\nResponseText: {\"error\":{\"type\":\"InvalidArgumentException\",\"message\":\"The timestamp must be numeric.\",\"code\":0,\"file\":\"\\/home\\/drupalsites\\/default\\/web\\/core\\/lib\\/Drupal\\/Component\\/Datetime\\/DateTimePlus.php\",\"line\":201,\"trace\":[{\"file\":\"\\/home\\/drupalsites\\/default\\/web\\/core\\/lib\\/Drupal\\/Component\\/Datetime\\/DateTimePlus.php\",\"line\":201,\"function\":null,\"class\":\"InvalidArgumentException\",\"args\":[\"The timestamp must be numeric.\"]},{\"file\":\"\\/home\\/drupalsites\\/default\\/web\\/modules\\/contrib\\/smart_date\\/src\\/Plugin\\/Field\\/FieldWidget\\/SmartDateWidgetBase.php\",\"line\":117,\"function\":\"createFromTimestamp\",\"class\":\"Drupal\\\\Component\\\\Datetime\\\\DateTimePlus\",\"args\":[]},{\"file\":\"\\/home\\/drupalsites\\/default\\/web\\/modules\\/contrib\\/smart_date\\/src\\/Plugin\\/Field\\/FieldWidget\\/SmartDateDefaultWidget.php\",\"line\":108,\"function\":\"formElement\",\"class\":\"Drupal\\\\smart_date\\\\Plugin\\\\Field\\\\FieldWidget\\\\SmartDateWidgetBase\",\"args\":[]},