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
Comment #2
remotedots commentedi am noticing a similar issue, but the following output is generated:
checking the 'end_value' in the same way as 'value' is checked seems to solve the issue:
Comment #4
mandclu commentedAdded a check to make sure the end_value is a DrupalDateTime instance as well. Feel free to reopen if more work is needed.
Comment #6
lincoln-batsirayi commented@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\":[]},