Problem/Motivation

If there is reference to "deleted" entity, the referencing entity can't be saved/changed anymore.
There is a validation error: This entity (node: 11729) cannot be referenced.
And on the reference field, instead of label the "- Restricted access - (11729)" is displayed.
Even if user has "View deleted entities" permission.

An additional problem is custom code that outputs entity-derived data (e.g. counts of referenced entities) without going through access checks.

Proposed resolution

1. Prevent trashed entities from being returned by entity_load

Comments

kala4ek created an issue. See original summary.

kala4ek’s picture

Assigned: kala4ek » Unassigned
Status: Active » Needs review
StatusFileSize
new0 bytes
kala4ek’s picture

StatusFileSize
new3.02 KB
kala4ek’s picture

StatusFileSize
new9.54 KB

Also need to override default ValidReference constraint.

amateescu’s picture

Title: Reference to entity in trash error » Prevent trashed entities from being returned by entity_load
Issue summary: View changes
StatusFileSize
new7 KB

I've been thinking about this problem for a while, and finally decided to go in a (somewhat radical) different direction: Trash needs to prevent entity_load from returning trashed entities completely.

This was based on observations from custom code on various sites, and the problem that stood out the most was that there are cases where entity-derived data is displayed without going through access checks, for example directly outputting the result of count($entity->get('some_reference_field')->referencedEntities()), which internally does an entity_load_multiple, but doesn't run any access checks. This specific example could've been solved by overriding core's field item list class for ER fields, but then contrib/custom code that also override it wouldn't benefit from our changes.

This decision is also in line with the recent fix from #3393095: Trash breaks revision-based views and relation queries, which made all views (using either the data or the revision data as the base table) stop returning trashed entities by default.

amateescu’s picture

Status: Needs review » Fixed

  • amateescu committed dc4093ac on 3.x
    Issue #3370639 followup: Prevent trashed entities from being returned by...
amateescu’s picture

StatusFileSize
new4.91 KB

Now that we don't return anything on entity_load, we need different approaches for trash routes (restore, purge) and for viewing trashed entities.

Committed the followup patch attached to 3.x.

Status: Fixed » Closed (fixed)

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

kala4ek’s picture

Hm, but how it will works with entity edit forms? Looks like now trashed entites wouldn't displayed in referency fields anymore and after submitting the form, reference would gone. That means if the admin will restore the trashed node, it wouldn't appears at the place where it was before...

amateescu’s picture

@kala4ek, that's right, the new behavior is closer to the entity being actually deleted, which matches the scenario you described. Fixing the problem within \Drupal\Core\Field\EntityReferenceFieldItemList::referencedEntities() would have had the same outcome.