Problem/Motivation
ERItem adds EntityType constraint on entity property in ERItem::propertyDefinitions because $settings['target_type'] exists in field storage of ER field but DERItem can't add EntityType constraint on entity property in DERItem::propertyDefinitions because DER field can reference multiple entity types. Also, ERItem::getConstraints() adds bundle constraint to entity property whereas there is no bundle constraint on DERItem entity.
Add EntityType and bundle constraint on entity property of DER.
Proposed resolution
DERItem can't add EntityType constraint on entity property because we don't have target_type value until DERItem entity property is created so add it in DynamicEntityReferenceFieldItemList like #2577963: Let entity_ref Selection handlers be in charge of the field validation.
Remaining tasks
ValidDynamicReferenceConstraintValidator.
- Validate new entities
- Validate target_ids
- Validate bundles
User interface changes
None.
API changes
None
Data model changes
Adds EntityType and bundle constraint on entity property of DER.
Original report by @jibran
Follow-up to #2365331-28: Update DynamicEntityReferenceItem according to EntityReferenceItem
+++ b/src/Plugin/Field/FieldType/DynamicEntityReferenceItem.php @@ -80,30 +87,18 @@ class DynamicEntityReferenceItem extends ConfigurableEntityReferenceItem { - // $properties['entity']->getTargetDefinition()->addConstraint('Bundle', $settings['target_bundle']);We can add this constraint in setValue(). Do you think it is worth it? If yes we can discuss this in follow up.
| Comment | File | Size | Author |
|---|---|---|---|
| #48 | add_entitytype_and-2405467-48.patch | 26.82 KB | jibran |
| #45 | add_entitytype_and-2405467-45.patch | 26.83 KB | thenchev |
Comments
Comment #1
larowlanComment #3
jibranWe also need validation tests for each class i.e.
DynamicEntityReference,DynamicEntityReferenceItemandDynamicEntityReferenceFieldItemList.There should be some better way to do this.
Comment #4
larowlanComment #6
larowlanComment #7
jibranWe should add some additional documents to explain this.
Can we please improve these messages? So that user can understand which value is not correct.
We still have this @todo left
perhaps we should have to add test for entities which don't have the bundles.
Comment #8
larowlanfixes
Comment #10
jibranThanks for the fixes. Some more nitpicks :P
We can use EntityTest::create here.
Can we please explain this if/else as well?
Can we please add /* @var \.. $field */ here for better understanding?
This is fine here. Let's use @grom358's script in a follow up issue and fix it for whole module.
Comment #13
fagoI've no idea why you are doing that? It propagates down on its own anyway?
Would it be possible to add that constraint as part of the property definitions already or does it depend on FieldConfig settings? If so, adding it here makes sense to me.
Again, no need for that?
Comment #14
larowlanCouldn't get the getConstraints method to be called. Then realized, we already have our own validator, so lets just use that.
Comment #16
jibranComment #17
larowlanComment #18
jibran@larowlan thank you so much for fixing this. Patch looks good. Let me update IS and ping @fago for opinion.
Isn't this the disclosure of information?
I think it is not suppose to be here.
@larowlan have you this seen from #2064191: Fix the 'target_bundle' field setting - 'target_bundles' are not validated when entity_reference is enabled.
Do you think we should accommodate/bypass this hunk in DER? I believe this is not same for DERItem.
Also #2577963: Let entity_ref Selection handlers be in charge of the field validation removes
ValidReferencefrom ERItem and moves it to ERItemList. Should we also consider that in follow up when #2577963: Let entity_ref Selection handlers be in charge of the field validation is fixed.FYI after #1978714: Entity reference doesn't update its field settings when referenced entity bundles are deleted NULL means all bundles and empty array means no bundle so following is the wrong assumption in the patch
DERItem::getConstraints(). I have updated IS to accommodate that case.Comment #20
jibran#2577963: Let entity_ref Selection handlers be in charge of the field validation is in so I pushed a fix for the failing tests 5df5af3. Here is a reroll and WIP patch. Please feel free to pick this up.
Comment #21
james.williamsHere's another re-rolled patch that should at least apply, since I found the previous one didn't any more. Still needs review, and not all of the points from comment 18 above have been addressed yet.
Comment #22
james.williamsFixed constraints not actually causing a form validation error when submitting an invalid reference in a field widget :-)
Comment #23
jibranComment #27
jibranThere are proper tests for each use case if you'd like to complete it then you just need to fix the fails and add new asserts about bundle and entity type constraints. Please provide the interdiff for reviewer. Thanks.
Comment #28
james.williamsYes, sorry, I'd forgotten the interdiffs. I don't believe all the points in comment 18 have been addressed yet either anyway, so I was fully aware that the patch still needed work. Remaining tasks updated in issue summary.
Comment #29
jibranComment #31
larowlanIts been so long, I'm not sure what we need to do here to finish this off, I'll talk with Jibran about it today.
Comment #32
jibranUpdated the issue summary.
Comment #33
jibranReroll and some clean up.
Comment #35
jibranComment #36
thenchev commentedStarted looking into it.
Here a fix to the fatal for generateSampleItems()
Comment #38
thenchev commentedAdded more validations, started fixing tests.
Comment #40
slashrsm commentedComments missing for those. Last one reads a bit strange.
Do we really need this two extra variables?
$item->target_idand$item->target_typeseem compact enough to me.Can this function call go into the condition to avoid an extra variable assignment?
Comment #41
thenchev commentedWorked only on tests. Will cover #40 in the next patch. Also tests for bundles are missing.
Comment #42
jibranThanks @Denchev for the patch and thanks @slashrsm for the review.
This $target_type can be NULL which we don't want.
Here is how I thought we should proceed.
nullMessageif any field property is NULL.$new_entitiesand validate them later cause we need to validate the entity type first.$wrongTypeMessage. Remove the new entities from new entities array.validateReferenceableNewEntities. Keep adding the violationsinvalidAutocreateMessage.validateReferenceableEntities. Keep adding the violations.See
ValidReferenceConstraintValidatorin core for more details.Comment #43
thenchev commentedSo i few things here:
1. Now we go over every element and and get first validation that we find.
2. $valid_type = empty($item->target_type) || (!empty($item->target_type) && in_array($item->target_type, $valid_target_types));
used this code because it was already there. So we are allowing empty target type?
3. Moved the check with validateReferenceableEntities at the end. If the bundle is not valid then the validateReferenceableEntities is not going to pass. Have to check first if ID has an existing entity so i can do the check for the bundle and have that specific message ('Referenced entity %label does not belong to one of the supported bundles (%bundles).')
4. There is the case if the 'target_bundles' is an empty array (which means no bundles allowed) this does not quite work with the massage we have now for the bundle (Referenced entity %label does not belong to one of the supported bundles (%bundles)). Should i add a new message like "No bundle allowed for %entity_type" or change the message to something else?
Comment #44
jibranWow, nice update. Thanks for the patch. Here is the answer to your questions:
Can you please upload this path for 8.x-2.x and test it againest that branch?
Comment #45
thenchev commentedThanks for the quick answers. Added new message for no bundle allowed.
Comment #47
jibranIt is green for 8.x-1.x.
Comment #48
jibranRe-roll for 8.x-2.x.
Comment #49
jibranThanks! @Denchev. I think this is ready. I asked @larowlan to review this so assigning it to him now. Meanwhile, if @Berdir would be able to have a look at it then it'd be great.
Comment #50
larowlanThis looks ready to me, thanks @Denchev!
Comment #53
jibranThank you. Committed and pushed to both branches.
Comment #54
jibranWe have a followup bug #2822849: Fatal error when using 8.x-1.x-dev version..
Comment #55
jibran