Support for Drupal 7 is ending on 5 January 2025—it’s time to migrate to Drupal 10! Learn about the many benefits of Drupal 10 and find migration tools in our resource center.
Motivation
For generating NOT NULL constraints for base field schema columns, we need to know whether a storage field is required. Right now, that's one of the usual things that entity schema handlers have to customize. It would be need for later improvements like automating field index generation as well.
Proposed resolution
Add isRequired
to FieldStorageDefinitionInterface
and make it default to FALSE for FieldConfig
. Configurable fields are never required at storage level as the instance decides whether something is required by bundle.
Remaining tasks
- Agree
- Do
User interface changes
-
API changes
- Add isRequired
to FieldStorageDefinitionInterface
Beta phase evaluation
Comment | File | Size | Author |
---|---|---|---|
#20 | interdiff.txt | 666 bytes | amateescu |
#20 | 2390495-20.patch | 3.16 KB | amateescu |
#18 | interdiff.txt | 676 bytes | amateescu |
#18 | 2390495-18.patch | 2.98 KB | amateescu |
| |||
#15 | interdiff.txt | 693 bytes | amateescu |
Comments
Comment #1
yched CreditAttribution: yched commentedAgreed.
Comment #2
effulgentsia CreditAttribution: effulgentsia commentedReparenting this issue since we moved the scope of needing this from the original parent to the new one.
Comment #3
plachComment #4
yched CreditAttribution: yched commentedRelated : #2411323: FieldStorageConfig::isRequired should not exist
Comment #5
amateescu CreditAttribution: amateescu for Pfizer, Inc. commentedSince #2232477: Fatal when adding new fields with NOT NULL constraints in a base table that contains existing entities, not null constraints are added to all required properties of a field type. Is there anything else we need to do here?
Comment #8
amateescu CreditAttribution: amateescu for Pfizer, Inc. commentedAnswering to myself in #5, the
isRequired()
method is actually needed for #2346019: Handle initial values when creating a new field storage definition, so here's a patch for that.Note that the documentation that I added to
\Drupal\Core\Field\FieldStorageDefinitionInterface::isRequired()
is lying a bit, NOT NULL constraints will only be added in #2346019: Handle initial values when creating a new field storage definition.Comment #9
Manuel Garcia CreditAttribution: Manuel Garcia as a volunteer and at Appnovation commented#8 Looks good to me
Comment #10
timmillwood+1
Comment #11
BerdirWe have isRequired() now on FieldDefinitionInterface and on FieldStorageDefinitionInterface. Are we sure that we won't get a conflict on that, for example for base fields?
For configurable fields, it is now normal for the storage to not be required but the field definition/field config can be required.
What if a base field wants to provide a similar behavior? Make it optionally required per-bundle but not do that on the storage level?
Would it be better to have a separate method?
Also, the fact that we have this method already on base fields makes me wonder if this isn't a hidden BC break. We have somewhat relaxed our rules that we can add new methods e.g. to entity (type) interfaces as we have base classes there which can provide sane defaults that we can expect people to implement. That's currently not the case for FieldStorageDefinitionInterface as we do not have a base class that could provide sane defaults, beside BaseFieldDefinition (#2280639: Add the FieldStorageDefinition class to define field storage definitions in hook_entity_field_storage_info() as well as @todo on \Drupal\entity_test\FieldStorageDefinition).
What if we make this a new optional interface, with a different method name (isSToragRequired()?). We can implement this in BaseFieldDefinition and actually call isRequired() by default, then someone who needs to do something weird can override it.
And we can also simply not implement it with FieldStorageConfig, which I think would technically also be more correct (It's not that we know that it is not required, we actually don't know if it is or not)
Comment #12
dixon_Comment #13
amateescu CreditAttribution: amateescu for Pfizer, Inc. commented@Berdir is right, the clash in
BaseFieldDefinition
with the existing 'required' property is unfortunate and I like the suggestion from #11, let's do that :)Comment #15
amateescu CreditAttribution: amateescu for Pfizer, Inc. commentedOops, that's an IDE autocompletion fail :)
Comment #17
BerdirFieldStorageDefinitionInterface on purpose only has get*, has*, is*. It doesn't specify how those are set as you are usually not supposed to change those values when working against the interface, e.g. changing a base field definition isn't allowed outside of the places where you may define/alter it.
So I think we should only have the is method on the interface?
Comment #18
amateescu CreditAttribution: amateescu for Pfizer, Inc. commentedThat's perfectly true and reasonable :)
Comment #20
amateescu CreditAttribution: amateescu for Pfizer, Inc. commentedLate night coding...
Comment #23
amateescu CreditAttribution: amateescu for Pfizer, Inc. commentedThe testbot has finally decided to grant us a good run here :)
Comment #24
BerdirYeah, that makes sense to me.
This doesn't do anything yet on its own, but #2346019: Handle initial values when creating a new field storage definition will depend on it.
I guess setting it to RTBC will start to fail again on testbot... :(
Comment #26
amateescu CreditAttribution: amateescu for Pfizer, Inc. commentedComment #27
alexpottWe need a CR for this addition.
Comment #28
timmillwoodI've added a change record for this https://www.drupal.org/node/2841350
Comment #29
naveenvalechaRemoving Needs change record tag
Comment #30
alexpottComment #31
alexpottCommitted cc30d65 and pushed to 8.3.x. Thanks!