In limited_field_widgets, enforcement of the custom cardinality setting is only done through #element_validate on the widget form. As described in #1029298-99, this excludes entity updates through, say, the REST API. This patch implements a custom validation constraint to limit the cardinality of field instances. The validation constraint ensures that all entity updates are validated against the limit_values setting. The patch does not replace the current form validation, which takes precedence as it is performed earlier in the update operation.
Possible sticking points with this patch are:
- The added functionality has only been tested with Paragraph bundles. limited_field_widgets_limit_validation guards against certain edge cases such as the _none item, which haven't been implemented in the new validation constraint. I've been unable to find exactly which behaviors necessitate these additions, so I've been unable to test whether they work with the constraint.
- The pre-patch third party setting was held in the field widget, and the patch includes some code to synchronize that value to another third party setting in the field config, which is needed to access the setting from hook_entity_bundle_field_info_alter(). This code works fine from my testing, but it might be worth taking a second look at.
Any feedback and additional testing is appreciated.
| Comment | File | Size | Author |
|---|---|---|---|
| #5 | limited_field_widgets-validation-constraint-3201950-5.patch | 3.86 KB | jacobbell84 |
| limited_field_widgets-validation-constraint.patch | 3.89 KB | koztunc |
Issue fork limited_field_widgets-3201950
Show commands
Start within a Git clone of the project using the version control instructions.
Or, if you do not have SSH keys set up on git.drupalcode.org:
Comments
Comment #2
koztunc commentedComment #3
stomusicPatch outdated, please re-roll it
Comment #4
stomusicComment #5
jacobbell84 commentedHere's a re-roll for the patch
Comment #6
smustgrave commentedShould be in an MR
Comment #7
smustgrave commentedActually since I added configuration validation in 3.0.x branch is this still needed?
Comment #9
smustgrave commentedTake my previous statement back. I've updated slightly for 10.3
Will need a review from someone else besides myself.
Comment #11
pavel.bulatThe patch had an issue:
Error: Call to undefined method Drupal\Core\Field\BaseFieldDefinition::id() in limited_field_widgets_field_widget_third_party_settings_form() when you try to open the `Manage Form Display` tab with fields that were created on a code level.
For example: a Commerce Order has an Unlimited Order Items field. The field definition for the Order Items doesn't have id().
Steps to reproduce
1) Install Drupal Commerce and Commerce Order
2) Create a new order type
3) Open Manage Form Display tab
In the last commit, I added a check
if ($field_definition instanceof FieldConfigInterface). It fixed the issue, but I am not sure that it was the right way to fix it, so feel free to review it.Comment #13
smustgrave commented