The module throws following PHP notice messages when upgraded to PHP 7.4.8:

Notice: Trying to access array offset on value of type null in theme_date_display_combination() (line 132 of /var/www/html/sites/all/modules/contrib/date/date.theme).

Comments

preethy211 created an issue. See original summary.

damienmckenna’s picture

Version: 7.x-2.10 » 7.x-2.x-dev
Priority: Critical » Normal
Issue tags: +PHP 7.4

Does the field have a value2, i.e. a "to" date? Looking at the code in question it's expecting there to be one defined.

preethy211’s picture

No there is no 'to' date to it. And the errors are thrown on the lines of the default switch case where 'value' and 'value2' is retrieved and stored in date1 and date2 respectively. The same error is being thrown in 5 places wherever 'value' and 'value2' is being used as mentioned below,


  switch ($options['fromto']) {
    case 'value':
      $date1 = $dates['value']['formatted'];
      $date2 = $date1;
      break;

    case 'value2':
      $date2 = $dates['value2']['formatted'];
      $date1 = $date2;
      break;

    default:
      $date1 = $dates['value']['formatted'];  \\Error
      $date2 = $dates['value2']['formatted'];  \\Error
      break;
  }

  // Pull the timezone, if any, out of the formatted result and tack it back on
  // at the end, if it is in the current formatted date.
  $timezone = $dates['value']['formatted_timezone'];  \\Error
  if ($timezone) {
    $timezone = ' ' . $timezone;
  }
  $date1 = str_replace($timezone, '', $date1);
  $date2 = str_replace($timezone, '', $date2);
  $time1 = preg_replace('`^([\(\[])`', '', $dates['value']['formatted_time']);  \\Error
  $time1 = preg_replace('([\)\]]$)', '', $time1);
  $time2 = preg_replace('`^([\(\[])`', '', $dates['value2']['formatted_time']);  \\Error
  $time2 = preg_replace('([\)\]]$)', '', $time2);

preethy211’s picture

StatusFileSize
new60.96 KB
derekw’s picture

Had the same error. In my case I had a bunch of NULL values in the field_data_[date field] table. Maybe from an old import as the newer nodes with the same field didn't have that problem (if the value wasn't entered there wasn't a table row for it).

damienmckenna’s picture

Title: PHP 7.4 Notices » Notices in theme_date_display_combination() after upgrading to PHP 7.4
Issue tags: +Needs tests

@derekw: I suspect NULL issues on data that was imported would indicate a problem with the imported data, that you could fix with an update script.

@preethy211: Was your data imported, or was it created via the admin

preethy211’s picture

@damienmckenna:
date_field/content type was imported by admin but with "Collect End date" disabled
DATA to this field/content type are manually added by site users

lavamind’s picture

@damienmckenna:
I'm also hitting this bug. The content was not imported. The date field value is being set to NULL by a core Rules "Set a data value" action. Under the hood, this is done using a $field->set(NULL); call where $field is an instance of EntityMetadataWrapper. See https://git.drupalcode.org/project/rules/-/blob/7.x-2.x/modules/data.eva...

franckylfs’s picture

StatusFileSize
new776 bytes

I had the exact same errors and after some testing I made this solution. In fact, normally, the function returns HTML. So a little bit of validation added to each case returns NULL in case of empty values. What do you think of my solution?

damienmckenna’s picture

Status: Active » Needs review
hughworm’s picture

StatusFileSize
new2.34 KB

I came across more instances of the same error. I've tried a different approach testing every value for null before using. Patch attached.

joelpittet’s picture

Status: Needs review » Reviewed & tested by the community

Thanks @hughworm I've been seeing these in the logs today too after a recent upgrade to PHP 7.4.

@DamienMcKenna does this really need tests? And if so, would passing an empty array to theme_date_display_combination() suffice?

damienmckenna’s picture

This is fine, thank you.

joelpittet’s picture

Bumping, because I ran into it again on a different app. I'll apply the patch now to all our apps.

leendertdb’s picture

We also had an issue with these notices appearing after upgrading to PHP 7.4. I was first looking into applying a patch, but thanks to the comment of @derekw I found out we also had quite a few records in field_data_[date field] tables with NULL values. These records with NULL values shouldn't be there and caused these errors. Probably caused by a previous import.

After deleting all the records with NULL values and clearing the cache the problem goes away.

This has nothing to do with the patch, but just dropping it in here for other people: check your "field_data_[date field]" tables in your database to see if there are any records with a "NULL" value. If so, delete them, clear the cache and check if the errors go away. You might not need any patch.

damienmckenna’s picture

StatusFileSize
new3.21 KB
new1.88 KB

Testing to see if this alternative solution works, as it leads to slightly cleaner code imho.

  • DamienMcKenna committed d86b067 on 7.x-2.x
    Issue #3164305 by DamienMcKenna, FranckyLFS, hughworm, preethy211,...
damienmckenna’s picture

Status: Reviewed & tested by the community » Fixed

Committed. Thanks everyone.

ennorehling’s picture

@damienmckenna : You accidentally wrote 'value1' there, instead of 'value', for $dates['value1']['formatted_timezone'].

solideogloria’s picture

Status: Fixed » Needs review
StatusFileSize
new605 bytes

Please review this typo fix that applies on dev. Thanks.

damienmckenna’s picture

Status: Needs review » Fixed
Related issues: +#3201850: Plan for Date 7.x-2.12-rc1

.... dangit.

Thanks.

Status: Fixed » Closed (fixed)

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