Illegal string offset for no-access date fields in node preview

Created on 23 April 2018, about 7 years ago
Updated 6 February 2025, 4 months ago

I saw a warning like this in my watchdog, from a node form preview request:

Warning: Illegal string offset 'value' in some_custom_function_with_date_field()

.

Technical background

I did some debugging, and found this:

In a node preview, field items for display are built from form values.

If this happens with
- a date field protected by field permissions
- for a user who has no access to this field,

then the field items will contain one non-array item, where the value is just an empty string.
So, the result of field_get_items('node', $node, 'field_some_date') items might be sth like this:

array(
  'und' => array(
    0 => array('value' => .., 'value2' => .., 'timezone' => .., '..),
    1 => '',
  ),
)

The items are collected in field_default_extract_form_values(), and later filtered in _field_filter_items() via hook_field_is_empty().

To make the problem complete, date_field_is_empty() returns FALSE for non-array items, to support a weird case where the item is an object.

function date_field_is_empty($item, $field) {
  // Sometimes a $item is a date object.
  // Coming from repeating dates. Why??
  if (!is_array($item)) {
    return FALSE;
  }

If these field items are used anywhere during the preview via $item['value'], while $item === '', then you get "illegal string offset".

This is all a bit weird, but I suspect it is thanks to field_permissions that the form value for the field item is an empty string.

πŸ› Bug report
Status

Closed: outdated

Version

1.0

Component

Code

Created by

πŸ‡©πŸ‡ͺGermany donquixote

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Comments & Activities

Not all content is available!

It's likely this issue predates Contrib.social: some issue and comment data are missing.

Production build 0.71.5 2024