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)
Comment | File | Size | Author |
---|---|---|---|
#7 | d8_field_single.patch | 39.83 KB | fago |
#7 | d8_field_single.interdiff.txt | 10.46 KB | fago |
#6 | d8_field_single.patch | 16.83 KB | fago |
Comments
Comment #1
fagotagging
Comment #1.0
fagoUpdated issue summary.
Comment #2
yched CreditAttribution: yched commentedOuch. 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
Comment #3
fagoad #2 - as said above:
Thus, we could support single-valued entity fields only for not-configurable fields, i.e. fields not handled by the field API?
Comment #4
yched CreditAttribution: yched commentedYeah, 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 :-).
Comment #5
fagoAs discussed during drupalcon, we want to do it.
Comment #6
fagoRolled a patch for this one. Without any field implementing it yet.
Comment #7
fagoSo this implements a single field for node.title. It factors Field::access() out into a helper class to ease code-reuse. Besides that naming of all the field data type classes need work (in general), e.g. it's type "string_field" with class StringItem - but that's best cleaned up as part of #2023563: Convert entity field types to the field_type plugin as there data types for field item's will become field_item:string anyway.
Let's see how that goes.
Comment #9
yched CreditAttribution: yched commentedSince this is apparently renaming Field to FieldItemList (which is a *very* good thing IMO - see my rant in #2019601-72: Rename config Field / FieldInstance structures ?), this completely changes the problem space around our naming issues.
I closed #2019601: Rename config Field / FieldInstance structures ?, and started a new proposal at #2051923: Rename \Drupal\Core\Entity\Field\Field, related subclasses and interfaces to *FieldItemList.
It's not clear when this patch here is going to be ready, though. Do you think it would be doable that we do the Field / FieldItemList rename separately ?
Comment #10
fagoAs discussed #2051923: Rename \Drupal\Core\Entity\Field\Field, related subclasses and interfaces to *FieldItemList goes with FieldItemList now, what means the we probably would have to implement the FieldItemList interface with a single field item here. So that would probably put as to not breaking the public API contract of content entities never breaking the "entity - itemlist -item" structure, but just saving the itemlist object instance if we know there will be only one anyway. I.e., this would boil down to a performance improvement patch without any API changes.
As an alternative, we could allow entities to consist of fieldItemLists or fieldItems directly, what should generally work out, but opens the question on how we'd deal with stuff like access() being part of FieldItemListInterface but not in FieldItemInterface.
Comment #10.0
fagoUpdated issue summary.
Comment #11
jibranThere is no EntityNG in core now.
Comment #12
catchComment #13
fagoThis won't happen for D8.
Comment #14
catchThere's nothing actually stopping it going into 8.x though except for time/interest, I'd like to keep the 9.x queue as short as possible with things that really can't possibly ever happen in 8.x under any circumstances.
Comment #15
tim.plunkettComment #16
xjmThis issue was marked as a beta target for the 8.0.x beta, but is not applicable as an 8.1.x beta target, so untagging.
I've moved the issue to 8.2.x since it could be possible to implement this with BC in a minor.
Comment #25
bradjones1I'm curious if the ship has long sailed on the idea of supporting single-ordinality fields, as Drupallers everywhere have now gotten used to
->first()
on fields?