Improve documentation for EntityType::$entity_keys

Created on 24 April 2019, over 6 years ago
Updated 2 September 2025, 15 days ago

API page: https://api.drupal.org/api/drupal/core%21lib%21Drupal%21Core%21Entity%21...

ContentEntityBase can define a number of base fields, meaning that the specific entity has less code to implement.

It does this in baseFieldDefinitions(), by looking at the entity_keys. Several, but not all, entity keys are handled.

For example, if the entity has an 'id' entity key, then this is provided:

    if ($entity_type->hasKey('id')) {
      $fields[$entity_type->getKey('id')] = BaseFieldDefinition::create('integer')
        ->setLabel(new TranslatableMarkup('ID'))
        ->setReadOnly(TRUE)
        ->setSetting('unsigned', TRUE);
    }

This helpful behaviour should be documented in the class docs, so developers don't have to read the code to discover this or look up the details.

🐛 Bug report
Status

Active

Version

11.0 🔥

Component

documentation

Created by

🇬🇧United Kingdom joachim

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.

  • 🇨🇦Canada Liam Morland Ontario, CA 🇨🇦

    There needs to a general improvement in the documentation for EntityType::$entity_keys. I have read outside of api.drupal.org that it is related to key mapping. It also seem to play a role in whether or not a database column is set NOT NULL.

  • 🇬🇧United Kingdom joachim

    Agreed, but that's taking this issue to a much wider scope. It's best if documentation issues target single, small elements of the docs.

    Feel free to make a meta-issue called 'Improve documentation for EntityType::$entity_keys', and child issues with specific tasks!

  • 🇨🇦Canada Liam Morland Ontario, CA 🇨🇦

    I don't know what EntityType::$entity_keys does well enough to write specific tickets. I need the docs because I don't understand how it works.

  • 🇬🇧United Kingdom joachim

    Right -- I understand the circular problem there of not understanding things you don't know about!

    I've made a parent meta issue -- feel free to comment there with things about entity keys you're aware of that need clarifying, and I can make further child issues.

Production build 0.71.5 2024