Remove exception when accessing a non-existing field with ContentEntityInterface::get()

Created on 30 November 2024, 11 days ago

Problem/Motivation

There is an attempt to remove magic access for fields on content entities πŸ“Œ [meta] Deprecate __get/__set() on ContentEntityBase Postponed

One concern mentioned there is that $entity->get('field_name') throws an exception, while $entity->field_name does not.

This results in pretty unwieldy code:

if ($entity->hasField('field_name') && $entity->get('field_name')->value)

This made sense many years ago when it was added, but since then we have nullable operator, so if we remove the exception, we can do this:

if ($entity->get('field_name')?->value)

Steps to reproduce

Proposed resolution

Remove the exception.

Remaining tasks

Per my comment in the meta issue, there is one problem with this:

The only problem I see is someone is relying on the exception and is catching that, then it would result in a php warning or something instead. like this:

Unsure how to approach that:

1. We could ignore that, and that code will just need to be be adjusted. I hope people haven't done that a lot :)
2. We trigger a deprecation before throwing the exception and remove it in D12. Somewhat unlikely that code doing that will actually see that deprecation, but it gives it a chance and time to adjust.

User interface changes

Introduced terminology

API changes

Data model changes

Release notes snippet

πŸ“Œ Task
Status

Active

Version

11.1 πŸ”₯

Component

entity system

Created by

πŸ‡¨πŸ‡­Switzerland berdir Switzerland

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

Merge Requests

Comments & Activities

Production build 0.71.5 2024