RenderedEntity::getEntityTranslationByRelationship() throws TypeError when entity is null

Created on 20 November 2024, about 23 hours ago

Problem/Motivation

When using the RenderedEntity::getEntityTranslationByRelationship() method in Drupal 9.5.11, a TypeError occurs if the entity is null. This happens because the code attempts to access a method on a null object, causing the error and halting code execution. This issue affects the proper rendering of entities in views when no entity is found or the relationship is not correctly defined.

Steps to reproduce

  1. Create a view that includes the RenderedEntity field.
  2. Ensure that one of the related entities is null or empty.
  3. Attempt to render the view and observe that the TypeError occurs because the getEntityTranslationByRelationship() method is called with a null entity.

Proposed resolution

To avoid the error, a check should be added to ensure that the entity is not null before attempting to access its translation. If the entity is null, the method should return an empty value or handle the case appropriately without throwing a type error.

Remaining tasks

  • Review the code in RenderedEntity.php to add the null entity validation.
  • Test the patch to ensure that the TypeError is resolved when the entity is null.
  • Verify that the solution does not negatively affect other parts of the system.

Release notes snippet

Fixes a TypeError in the getEntityTranslationByRelationship() method of the RenderedEntity class when the entity is null.

🐛 Bug report
Status

Needs review

Version

9.5

Component

views.module

Created by

🇪🇸Spain whereiam

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

Comments & Activities

Production build 0.71.5 2024