Download & Extend

Consider supporting single valued EntityNG fields

Project:Drupal core
Version:8.x-dev
Component:entity system
Category:task
Priority:normal
Assigned:Unassigned
Status:active
Issue tags:Entity Field API, Performance, typed data

Issue Summary

Right now each field is multi-valued by default as we enforce this structure on all of our fields. This creates one intermediate, unused layer for all single-valued fields what has a performance impact memory and speed wise.

Considering we usually have quite some single-valued fields I think we should consider dropping the multi-valued enforcement for performance.
Related issue: #1869562: Avoid instantiating EntityNG field value objects by default

Impact:
You cannot rely on being able to do $entity->field[0] any more to get the first field item. You might have to do $entity->field instead. Still, we could support $entity->field->value to always work such that code won't break if you change a single valued field to become multiple valued. The other way round it breaks though.
Then, another question is whether we want to allow single valued fields for configurable fields (=field API fields) also. We could leave this possibility API-only and only allow developers to create them such that field API could continue to work on top of multiple-valued fields only.
Having single-valued fields would make some things easier also though. E.g. when serializing to JSON-LD (not drupal json-ld) I assume we could use the information which field is supposed to be single valued. Also, when I think of e.g. token replacements implemented on top of that, we'd naturally want single valued fields to appear single valued in a token browser... (related: #1869582: Leverage the Typed Data API for token replacements)

Comments

#1

tagging

#2

Ouch. I'm really not looking forward to changing the internal format of Field API fields depending on whether they are mono or single valued :-/.
The whole code base and storage relies on the fact that mono is treated as a degenerate case of multi. I don't see us adding "it depends" code all around the place

#3

ad #2 - as said above:

We could leave this possibility API-only and only allow developers to create them such that field API could continue to work on top of multiple-valued fields only.

Thus, we could support single-valued entity fields only for not-configurable fields, i.e. fields not handled by the field API?

#4

Yeah, I was reacting to "Then, another question is whether we want to allow single valued fields for configurable fields (=field API fields) also".
Of course, if configurable fields are left untouched, I have no objection :-).