backdrop-contrib / entity_plus

This module wraps in a variety of additional entity-related functionality from various sources. Partial port of D7 Entity API.
https://backdropcms.org/project/entity_plus
GNU General Public License v2.0
3 stars 11 forks source link

Trying to access array offset on value of type null in entity_plus_metadata_date_struct_getter() #155

Closed herbdool closed 1 year ago

herbdool commented 1 year ago

Warning: Trying to access array offset on value of type null in entity_plus_metadata_date_struct_getter() (line 1108 of /app/httpdocs/modules/contrib/entity_plus/modules/callbacks.inc).

I get this when running a Search API. I don't think this is a bug per se in entity_plus (this only happens with one of the fields I've added to the index - just not sure which one), but might be good to add a check like this:

function entity_plus_metadata_date_struct_getter($item, array $options, $name, $type, $info) {
  if (empty($item) || !isset($item[$name])) {
    return NULL;
  }
  $value = trim($item[$name]);
  if (empty($value)) {
    return NULL;
  }

  $timezone_db = !empty($item['timezone_db']) ? $item['timezone_db'] : 'UTC';
  $date = new BackdropDateTime($value, $timezone_db);
  return !empty($date) ? date_format_date($date, 'custom', 'U') : NULL;
}
herbdool commented 1 year ago

@argiepiano I've created a PR. Figured it can't hurt to add this check.

argiepiano commented 1 year ago

Thanks, @herbdool. In fact this was similarly fixed in Date API in D7 not too long ago. I'll merge.