I am creating a complex workflow, where a date must be added in phase 2. When the Workflow is transitioned to this phase, the user receives an error.

Notice: Uninitialized string offset: 0 in date_entity_metadata_struct_getter() (line 587 of ../sites/all/modules/date/date.module).

It doesn't appear to affect the ability to add a date. There is no default date for the field, and a date isn't required. The date is set to -3, +3.

This is the function at line 586.

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

If nothing else, I would like to stop this particular warning message.

Comments

nels’s picture

Issue summary: View changes

I got this error when evaluating a Date field in a Rules Component.

I added a condition to check that the value was not empty.

Example Rule order:

  • Entity has field (ex: Parameter: Entity: [node], Field: field_publish_on )
  • NOT Data value is empty (ex: Parameter: Data to check: [node:field-publish-on] This is the condition that prevents the warning)
  • Data comparison (ex. Data to compare: [node:field-publish-on], Operator: is less than, Data value: +15 minutes
johnlaine’s picture

I am also getting this error when using a rules component to check if the data value is empty.

I changed this line
$value = trim($item[$name]);

To
$value = (isset($item[$name])) ? trim($item[$name]) : '';

This prevented the error message for me.
I don't know how to test it or submit a patch yet, will do so asap, unless someone else would like to.

rovo89’s picture

StatusFileSize
new719 bytes

I have noticed the same warning on two of my sites using version 2.8. In both cases, the fields are empty by default, they're set to #access = FALSE and an entity_metadata_wrapper is created in hook_node_update.

Please find my patch attached. I think the value for a date field can't be a string, it should be an array. Using NULL as default value also removes the warnings - not sure what's better here.

crstnkal’s picture

I currently have Drupal version: 7.50 and I get 2 messages:

Warning: Illegal string offset 'value' in date_entity_metadata_struct_getter() (line 649 of ../sites/all/modules/contrib/date/date.module).

Notice: Uninitialized string offset: 0 in date_entity_metadata_struct_getter() (line 649 of ../sites/all/modules/contrib/date/date.module).

@rovo89 the patch worked successfully!!!

akolahi’s picture

Status: Active » Reviewed & tested by the community

Patch works for me as well

damienmckenna’s picture

Rerunning the tests, lets see how this works.

damienmckenna’s picture

Version: 7.x-2.6 » 7.x-2.x-dev
anrikun’s picture

Title: error message - Function date_entity_metadata_struct_getter » Error messages from date_entity_metadata_struct_getter()
Category: Support request » Bug report
Status: Reviewed & tested by the community » Needs work

I get the same errors as @crstnkal at #4.
Unfortunately, even with patch at #3, I still get:
Warning : Illegal string offset 'value' in date_entity_metadata_struct_getter()

anrikun’s picture

Status: Needs work » Needs review
StatusFileSize
new513 bytes

Here's a new patch.

yazzbe’s picture

#9 fixes the problem in Date 7.x-2.10

mdolnik’s picture

FYI the changes in #3's date-default-value-2074457-3.patch are also present in the patch in Issue: String Offset errors on edit page using PHP 7.1.0

tame4tex’s picture

Even with https://www.drupal.org/project/date/issues/2843367#comment-12929035 patch applied I am still getting the PHP Notices & Warnings mentioned by #4 on version 7.x-2.10.

Warning: Illegal string offset 'value' in date_entity_metadata_struct_getter() (line 649 of ../sites/all/modules/contrib/date/date.module).

Notice: Uninitialized string offset: 0 in date_entity_metadata_struct_getter() (line 649 of ../sites/all/modules/contrib/date/date.module).

Patch supplied in #9 stops the PHP notices and warnings but I wonder if it is just fixing the symptom and not the root cause.

tame4tex’s picture

Applying patch #3 stops the PHP notices and warnings with Date version 7.x-2.10, Drupal 7.67 and PHP 7.2.

I am thinking this is a better patch than #9 because it is fixing the root cause.

@anrikun I would be interested in knowing more details of when you were getting the php warnings.

anrikun’s picture

@tame4tex
It's been a while ago so I can't remember precisely.
I think I had found cases when value was simply not set yet, so checking isset() seemed to be the best protection.

nedjo’s picture

On PHP 7.4 the same bug may produce a different error message:

Notice: Trying to access array offset on value of type null in date_entity_metadata_struct_getter()

+++ b/date.module
@@ -651,7 +651,9 @@ function date_entity_metadata_field_getter($entity, array $options, $name, $enti
+  if (isset($item[$name])) {
+    $value = trim($item[$name]);
+  }

Setting $value within the if control structure means it will be undefined in the FALSE case.

Attaching a new patch that combines the new isset() test with the existing one for an empty value and returns early.

damienmckenna’s picture

StatusFileSize
new554 bytes

You shouldn't use empty() on the return from another function, it is only meant to be used on variables.

How about this?

damienmckenna’s picture

I think we should add this to 2.12.

solideogloria’s picture

  • DamienMcKenna committed 9bcb606 on 7.x-2.x
    Issue #2074457 by nedjo, DamienMcKenna, anrikun, rovo89, tame4tex,...
damienmckenna’s picture

Status: Needs review » Fixed

Committed. Thank you.

Status: Fixed » Closed (fixed)

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

zevarix’s picture

I tried the patch on comment #16 on and the Undefined index PHP notice still persisted on every save of a content type with event dates and times.

Notice: Undefined index: field in date_entity_metadata_struct_getter() (line 669 of /code/docroot/sites/all/modules/contrib/date/date.module).

Drupal 7.80
Date 7.x-2.11
PHP 7.3.27

damienmckenna’s picture

@zevarix: Please open a new issue, that's something else.