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

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

yched’s picture

Agreed.

effulgentsia’s picture

plach’s picture

Issue tags: +entity storage
yched’s picture

amateescu’s picture

Since #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?

Version: 8.0.x-dev » 8.1.x-dev

Drupal 8.0.6 was released on April 6 and is the final bugfix release for the Drupal 8.0.x series. Drupal 8.0.x will not receive any further development aside from security fixes. Drupal 8.1.0-rc1 is now available and sites should prepare to update to 8.1.0.

Bug reports should be targeted against the 8.1.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.2.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.1.x-dev » 8.2.x-dev

Drupal 8.1.9 was released on September 7 and is the final bugfix release for the Drupal 8.1.x series. Drupal 8.1.x will not receive any further development aside from security fixes. Drupal 8.2.0-rc1 is now available and sites should prepare to upgrade to 8.2.0.

Bug reports should be targeted against the 8.2.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.3.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

amateescu’s picture

Version: 8.2.x-dev » 8.3.x-dev
Status: Active » Needs review
FileSize
2.14 KB

Answering 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.

Manuel Garcia’s picture

#8 Looks good to me

timmillwood’s picture

Status: Needs review » Reviewed & tested by the community

+1

Berdir’s picture

Status: Reviewed & tested by the community » Needs review

We 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)

dixon_’s picture

Issue tags: +Workflow Initiative
amateescu’s picture

@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 :)

Status: Needs review » Needs work

The last submitted patch, 13: 2390495-13.patch, failed testing.

amateescu’s picture

Status: Needs work » Needs review
FileSize
3.24 KB
693 bytes

Oops, that's an IDE autocompletion fail :)

Status: Needs review » Needs work

The last submitted patch, 15: 2390495-15.patch, failed testing.

Berdir’s picture

+++ b/core/lib/Drupal/Core/Field/RequiredFieldStorageDefinitionInterface.php
@@ -0,0 +1,32 @@
+
+  /**
+   * Sets whether the field storage is required.
+   *
+   * @param bool $required
+   *   Whether the field storage is required.
+   *
+   * @return static
+   *   The object itself for chaining.
+   */
+  public function setStorageRequired($required);

FieldStorageDefinitionInterface 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?

amateescu’s picture

Status: Needs work » Needs review
FileSize
2.98 KB
676 bytes

That's perfectly true and reasonable :)

The last submitted patch, 15: 2390495-15.patch, failed testing.

amateescu’s picture

Late night coding...

The last submitted patch, 18: 2390495-18.patch, failed testing.

Status: Needs review » Needs work

The last submitted patch, 20: 2390495-20.patch, failed testing.

amateescu’s picture

Status: Needs work » Needs review

The testbot has finally decided to grant us a good run here :)

Berdir’s picture

Status: Needs review » Reviewed & tested by the community

Yeah, 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... :(

Status: Reviewed & tested by the community » Needs work

The last submitted patch, 20: 2390495-20.patch, failed testing.

amateescu’s picture

Status: Needs work » Reviewed & tested by the community
alexpott’s picture

Status: Reviewed & tested by the community » Needs work
Issue tags: +Needs change record

We need a CR for this addition.

timmillwood’s picture

Status: Needs work » Reviewed & tested by the community

I've added a change record for this https://www.drupal.org/node/2841350

naveenvalecha’s picture

Issue tags: -Needs change record

Removing Needs change record tag

alexpott’s picture

alexpott’s picture

Status: Reviewed & tested by the community » Fixed

Committed cc30d65 and pushed to 8.3.x. Thanks!

  • alexpott committed cc30d65 on 8.3.x
    Issue #2390495 by amateescu, Berdir: Support marking field storage...

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.