[12.x] Try to use property hooks to lazy instantiate entity keys

Created on 22 August 2025, about 19 hours ago

Problem/Motivation

Spin-off from 🐛 \Drupal\user\ContextProvider\CurrentUserContext::onBlockActiveContext() loads the anonymous user Postponed: needs info and possibly/hopefully 📌 Load user entity in Cookie AuthenticationProvider instead of using manual queries Needs work .

PHP 8.4 introduced property hooks, this allows logic to run when class properties are get and set.

In ContentEntityBase::__construct() we set the the $entityKeys and $translatableEntityKeys properties based on field definitions - this requires fetching field definitions in cases where they might not be used.

I haven't actually used property hooks yet, but I think it would be possible to move all of that logic to the first time either one of those properties is accessed while maintaining full bc. We'd have to defer setting the bundle entity key too but that seems very doable.

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.
  • Performance

    It affects performance. It is often combined with the Needs profiling tag.

Sign in to follow issues

Comments & Activities

  • Issue created by @catch
  • 🇷🇴Romania amateescu

    Another option for improving ContentEntityBase::__construct() would be to extend \Drupal\Core\Entity\EntityFieldManager::$fieldMap to include the translatable status per field and bundle, and use that instead of the field definitions.

Production build 0.71.5 2024