Quantcast
Channel: Issues for Drupal core
Viewing all articles
Browse latest Browse all 299521

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

$
0
0

Problem/Motivation

There is an attempt to remove magic access for fields on content entities #3281720: [meta] Deprecate __get/__set() on ContentEntityBase

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


Viewing all articles
Browse latest Browse all 299521

Latest Images

Trending Articles



Latest Images

<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>