Greetings! I am running into an issue porting a module to Drupal 8 that uses the Field API. The issue is that the value my field provides is stored as a string, but at validation time, it is composed of several values that will be coerced into a string during preSave. Since it looks like TypedDataManager places the PrimitiveType constraint on all fields which extend FieldItemBase, it causes PrimitiveTypeConstraintValidator to run against your field, even if you specify a custom constraint in the @FieldType annotation.

I'm attaching a patch that will help to describe the problem by proposing a simplistic solution. I don't know if this is robust enough, but it should at least illustrate the problem a little better. The suggestion is for TypedDataManager::getDefaultConstraints() to not add the PrimitiveType constraint to all primitive types. Rather, let the fields define the constraint themselves.

Thanks!

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

Kevin Hankens’s picture

dawehner’s picture

Status: Active » Needs review

Let's have a look what the testbot thinks. It seems odd that you don't do that kind of work on a widget level, but yeah maybe you have a proper usecase.

Status: Needs review » Needs work

The last submitted patch, 1: drupal-allow-constraint-overrides-2333733-1.patch, failed testing.

vitalie’s picture

I think one could apply some massage to the form values as an alternative solution, to make sure that returned value is of the necessary PrimitiveType.

See: https://api.drupal.org/api/drupal/core!lib!Drupal!Core!Field!WidgetBase....

Version: 8.0.0-alpha14 » 8.0.x-dev

Core issues are now filed against the dev versions where changes will be made. Document the specific release you are using in your issue comment. More information about choosing a version.

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

Drupal 8.8.7 was released on June 3, 2020 and is the final full bugfix release for the Drupal 8.8.x series. Branches prior to 8.8.x are not supported, and Drupal 8.8.x will not receive any further development aside from security fixes. Sites should prepare to update to Drupal 8.9.0 or Drupal 9.0.0 for ongoing support.

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

Version: 8.9.x-dev » 9.2.x-dev

Drupal 8 is end-of-life as of November 17, 2021. There will not be further changes made to Drupal 8. Bugfixes are now made to the 9.3.x and higher branches only. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 9.2.x-dev » 9.3.x-dev
longwave’s picture

Issue tags: +Bug Smash Initiative

This came up as random bug of the day in the Bug Smash Initiative.

I'm not sure if this is actually a bug; if your value isn't strictly a primitive then should you be implementing PrimitiveInterface?

PrimitiveTypeConstraintValidator then does checks based on type, so if you're implementing StringInterface but your value isn't a string, I think it is right for it to fail. You could still implement PrimitiveInterface but none of the core child interfaces, and PrimitiveTypeConstraintValidator would be a no-op in that case as it fails safe if it doesn't know the type.

quietone’s picture

Status: Needs work » Postponed (maintainer needs more info)

Since we need more information to move forward with this issue, I am setting the status to Postponed (maintainer needs more info). See the question asked in #9. If we don't receive additional information to help with the issue, it may be closed after three months.

Thanks!

Version: 9.3.x-dev » 9.4.x-dev

Drupal 9.3.15 was released on June 1st, 2022 and is the final full bugfix release for the Drupal 9.3.x series. Drupal 9.3.x will not receive any further development aside from security fixes. Drupal 9 bug reports should be targeted for the 9.4.x-dev branch from now on, and new development or disruptive changes should be targeted for the 9.5.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

smustgrave’s picture

Status: Postponed (maintainer needs more info) » Closed (outdated)

Reading the explanation in #9 seems correct. Think this is good to close. If still an issue please reopen.