Problem/Motivation

\Drupal\Core\Field\FieldConfigInterface::addConstraint()'s docs say:

   * Note that constraints added via this method are not stored in configuration
   * and as such need to be added at runtime using
   * hook_entity_bundle_field_info_alter().

This means it's required to have a module to add a constraint (because you need a module to implement that hook). It's also rather misleading.

Similar problems have been fixed in #2247085: Constraints cannot be added to configurable fields and #2541228: FieldConfigBase::setPropertyConstraints() and addPropertyConstraints() are broken.

Proposed resolution

It should be possible to add constraints via FieldConfig config entities and have those constraints be stored.

Remaining tasks

TBD

User interface changes

None.

API changes

None.

Data model changes

FieldConfig config entities now store constraints.

Comments

Wim Leers created an issue. See original summary.

amateescu’s picture

#2247085-6: Constraints cannot be added to configurable fields is why constraints are not stored in the field config objects. And, as far as I know, nothing changed in the land of constraints since then to invalidate that point.

wim leers’s picture

To quote @Berdir from #2247085-7: Constraints cannot be added to configurable fields:

Ouch, schema. Ok, you win.

So clearly this can't be solved today. But … isn't this something we need to do eventually?

dawehner’s picture

I think its actually a fair behaviour right now. By default the behaviour of a field is basically defined by the field type + configuration for this field type. Once you are able to add validations on configuration time, we basically integrate another dimension into this behaviour by default, aka. the plugin system around validation.

On top of that you want to do something special when you add validations, and as such I think custom code is not as bad as people think it is.

amateescu’s picture

@dawehner is correct, as always :) I vote for "Closed (works as designed)".

Version: 8.4.x-dev » 8.5.x-dev

Drupal 8.4.4 was released on January 3, 2018 and is the final full bugfix release for the Drupal 8.4.x series. Drupal 8.4.x will not receive any further development aside from critical and security fixes. Sites should prepare to update to 8.5.0 on March 7, 2018. (Drupal 8.5.0-alpha1 is available for testing.)

Bug reports should be targeted against the 8.5.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.6.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.5.x-dev » 8.6.x-dev

Drupal 8.5.6 was released on August 1, 2018 and is the final bugfix release for the Drupal 8.5.x series. Drupal 8.5.x will not receive any further development aside from security fixes. Sites should prepare to update to 8.6.0 on September 5, 2018. (Drupal 8.6.0-rc1 is available for testing.)

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

ghost of drupal past’s picture

> Ouch, schema

That ouch is band-aid'd by #2264179: Clarify use of property/undefined and add an ignore type in configuration schema. We can do this. ignore for the win.

wim leers’s picture

#9: Does that mean you're in favor of doing what this issue proposes? What are your thoughts about @dawehner's concerns in #5 and @amateescu's confirmation of those concerns in #6?

ghost of drupal past’s picture

My use case is restricting the contents of some link fields. I guess I can use hook_entity_bundle_field_info_alter ... I guess the point of making addConstraint store would be to allow the UI add these constraints ... and that might not be that import to us at least. I guess I am also fine with just closing this then. Although , as with everything else, it's utterly not clear whether changes hook_entity_bundle_field_info_alter need an update hook or not :( I am completely lost ever since entup was removed.

amateescu’s picture

Status: Active » Closed (works as designed)

Constraints don't affect the storage in any way, so using hook_entity_bundle_field_info_alter() for that doesn't need an update hook :)