I have an entity reference field which is dependent on another entity reference field (using "Business rules - make field dependent using views" widget).

After the node is created, I disable both fields on node edit form for certain user roles. So, when the form is submitted the validation fails with error: This entity (node: 45) cannot be referenced..

This happens because the parent field is no longer present within $_POST (because it is disabled) and the getValidIds method of BusinessRulesViewsSelection returns an empty array.

Comments

beluoctavian created an issue. See original summary.

tavib47’s picture

StatusFileSize
new1.72 KB

I suggest trying to get the value of the parent field from the entity if the parent field value is empty, but I don't think this is the best idea. I attach the patch which fixes my issue, but it may break something else.

yseki’s picture

I guess the best approach would be to check if the field was rendered on the form before performing the validation test. I will check it on the next opportunity.

tavib47’s picture

Version: 8.x-1.x-dev » 8.x-1.0-beta1
StatusFileSize
new1.67 KB

Updated the patch to work with beta1 version.

tavib47’s picture

Version: 8.x-1.0-beta1 » 8.x-1.0-beta4
StatusFileSize
new1.72 KB
tavib47’s picture

StatusFileSize
new1.8 KB

And now the patch without undefined variable error.

littlepixiez’s picture

#6 worked for me, but the reason my parent entity value is empty is because I'm trying to modify this module to work with Paragraphs. :) Continuing my hacking... thanks for the patch! :D

tavib47’s picture

Version: 8.x-1.0-beta4 » 8.x-1.x-dev
Assigned: Unassigned » tavib47
Issue tags: +DevDaysTransylvania, +DevDaysCluj
StatusFileSize
new1.7 KB

Updated the patch to work with the latest dev version

rosinegrean’s picture

Issue tags: -DevDaysCluj
colan’s picture

Component: Code » Dependent fields
pcambra’s picture

Assigned: tavib47 » Unassigned
Status: Active » Needs review
StatusFileSize
new2.43 KB

I've been testing this on my fork over https://www.drupal.org/project/dependent_fields and I think the code needs to be on validateReferenceableEntities as well as in getReferenceableEntities.

I've pulled the entity from config for getting the parent values in a common way, let's see if this is a correct approach.