Summary
In Drupal 7, render arrays generated by field formatters contained the field values as a plain array in $element['#items'] (received by theme_field() and hook_preprocess_field() as $variables['items']).
In Drupal 8, the $element['#items'] / $variables['items'] placed by FormatterBase::view() is the same data structure that is used throughout all the Entity / Field API runtime code : a FieldItemListInterafce object.
- That object implements ArrayAccess, and can thus be treated as an array of Item objects keyed by numeric delta.
- Values are accessed using the $items[$delta]->property
syntax instead of $items[$delta]['property']
.
The following hooks are impacted:
- hook_entity_view_display_build_alter() (D8 replacement for hook_field_attach_view_alter())
- hook_entity_view_alter()
- hook_preprocess_field()
- theme_field()
No syntax change is needed inside Twig templates, as the Twig syntax already handles the object/array syntax variants.
Additionally, formatters that delegate the rendering of each individual item to dedicated theme functions typically passed $element['#item] / $variables['item'] as an array. Similarly, they should preferably be updated to directly pass the FieldItemInterface objects.
The following core theme functions, used by image formatters, have been updated that way:
- theme_image_formatter()
- theme_picture_formatter()