EntityReferencePathFormatter errors on invalid reference

Created on 7 March 2024, 4 months ago
Updated 27 March 2024, 3 months ago

Problem/Motivation

When an entity is deleted, Drupal core does not automatically clean up references to that entity in entity reference fields.
When this happens, EntityReferencePathFormatter causes errors as it does not check if there actually is an entity before calling toUrl() on it.
In our specific case, this was found in a series of relationships - node -> taxonomy term -> node.

Steps to reproduce

  1. Create a taxonomy vocabulary ("Topics") with a field to reference a node ("Basic Page").
  2. Configure a display for this vocabulary to show the referenced node, using the "Entity path" formatter.
  3. Create a content type ("Article") with a field to reference that terms of that vocabularly.
  4. Create a "page" node.
  5. Create a "topic" term that references that basic page.
  6. Create an "article" node that references that term.
  7. Configure a rest view display to show nodes of type "article". Include the related topics field, and configure the display of this field to use the "Export entity reference" formatter, with the term display configured in step 2.
  8. Delete the "page" node created in step 4.
  9. Access the rest view.
  10. See error:

Error: Call to a member function toUrl() on null in Drupal\rest_views\Plugin\Field\FieldFormatter\EntityReferencePathFormatter->viewElements() (line 31 of /code/web/modules/contrib/rest_views/src/Plugin/Field/FieldFormatter/EntityReferencePathFormatter.php).

Proposed resolution

In EntityReferencePathFormatter->viewElements(), skip items that have no `entity` property.

πŸ› Bug report
Status

Fixed

Version

3.0

Component

Code

Created by

πŸ‡ΊπŸ‡ΈUnited States lisagodare@gmail.com

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Merge Requests

Comments & Activities

Production build 0.69.0 2024