Provide non-magic shortcuts for getting first field properties

Created on 6 January 2026, 25 days ago

Problem/Motivation

Opening this as a task, but it is a possible approach for the problem described in https://www.drupal.org/project/drupal/issues/3518668#comment-16356696 📌 Views entity rendering should use a lazy builder/placeholder Active and https://github.com/php/php-src/issues/14983

We have the following supporting things like $entity->fieldwhatever->entity;

FieldItemList
  /**
   * {@inheritdoc}
   */
  public function __get($property_name) {
    // For empty fields, $entity->field->property is NULL.
    if ($item = $this->first()) {
      return $item->__get($property_name);
    }
  }

Which calls this:

FieldItemBase
  /**
   * {@inheritdoc}
   */
  public function __get($name) {
    // There is either a property object or a plain value - possibly for a
    // not-defined property. If we have a plain value, directly return it.
    if (isset($this->properties[$name])) {
      return $this->properties[$name]->getValue();
    }
    elseif (isset($this->values[$name])) {
      return $this->values[$name];
    }
  } 

I think we could provide non-magic versions of these, e.g.:

FieldItemList
public function getFirstItem($name);
FieldItemBase
public function getValue($name);

Then you could do:

$entity->field_whatever->getFirstItem('entity');

Steps to reproduce

Proposed resolution

Remaining tasks

User interface changes

Introduced terminology

API changes

Data model changes

Release notes snippet

📌 Task
Status

Active

Version

11.0 🔥

Component

entity system

Created by

🇬🇧United Kingdom catch

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

Comments & Activities

Not all content is available!

It's likely this issue predates Contrib.social: some issue and comment data are missing.

No activities found.

Production build 0.71.5 2024