If no target_id is set, the function simply returns before checking the validity of the ids, but also before executing the behavior validate() methods. In my case I want to force selection of an entity for this particular field if the user does not have a certain permission but I can't currently do this.

If I move the behavior calls above the return it works and I can't see off the top of my head any downsides to this?

Comments

richard.thomas’s picture

Here's a quick patch to move it.

richard.thomas’s picture

Status: Active » Needs review

Status: Needs review » Needs work

The last submitted patch, entityreference-validate-on-empty-1789660-1.patch, failed testing.

richard.thomas’s picture

Status: Needs work » Needs review
StatusFileSize
new861 bytes

Let's try that again (one day I'll get the hang of this).

amitaibu’s picture

Status: Needs review » Closed (won't fix)

> In my case I want to force selection of an entity for this particular field

You can already do this by implementing your logic in EntityReference_BehaviorHandler::insert() or EntityReference_BehaviorHandler::update()

Please re-open if you think differently.

richard.thomas’s picture

Status: Closed (won't fix) » Active

I could use other hooks (although none would be as neat), but my understanding is the behavior method validate() is supposed to be emulating hook_field_validate() for a specific entityreference field, so why wouldn't we call it in the same situations that hook_field_validate() is called? Is it an issue with changing the code path that behavior plugins may be relying on?

damien tournoud’s picture

Status: Active » Fixed
StatusFileSize
new1.68 KB

Sounds reasonable. I merged the attached patch in 7.x-1.x.

Status: Fixed » Closed (fixed)

Automatically closed -- issue fixed for 2 weeks with no activity.

GeorgeJr’s picture

Issue summary: View changes

I'm getting "The referenced entity (node: 17029) is invalid." error since the id of the form being reset is included on the list of id's. To get rid of the error I check the node status before getting the id's.

foreach ($items as $delta => $item) {
$target_id = $item['target_id'];
if($target_id != ""){
$query = "SELECT `status`
FROM node
WHERE node.nid = $target_id";
$queryStatus = db_query($query)->fetchAll();
}
if (!entityreference_field_is_empty($item, $field) && $item['target_id'] !== NULL && $queryStatus['status'] == 1) {
$ids[$item['target_id']] = $delta;
}
}

Is it safe to place this query just to exclude the id of the reset forms?