EntityFieldDefinitionTrait::getEntityTypeId() prevents reuse of ContentEntity class outside of derived plugins

Created on 1 November 2021, over 2 years ago
Updated 28 February 2024, 4 months ago

Problem/Motivation

EntityFieldDefinitionTrait::getEntityTypeId() assumes that it is in a plugin whose ID is of the form 'foo:ENTITY_TYPE'.

This means that if you want to use ContentEntity as a base class for a custom source plugin, getEntityTypeId() fails to find the entity type.

The function is redundant, at least within ContentEntity's use of the trait, because ContentEntity does this in __construct():

    $this->entityType = $this->entityTypeManager->getDefinition($plugin_definition['entity_type']);

so the entity type and its ID are always available.

A workaround is to override the trait's method like this:

  /**
   * {@inheritdoc}
   */
  protected static function getEntityTypeId($plugin_id) {
    // Override this so it doesn't try to get the entity type ID from the plugin
    // ID, which will fail.
    return 'my_entity_type';
  }

but this violates DRY.

Steps to reproduce

Proposed resolution

Remove the method and get the entity type ID from $this->entityType->id().

Remaining tasks

User interface changes

API changes

Data model changes

Release notes snippet

🐛 Bug report
Status

Active

Version

11.0 🔥

Component
Migration 

Last updated about 12 hours ago

Created by

🇬🇧United Kingdom joachim

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

Merge Requests

Comments & Activities

Not all content is available!

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

Production build 0.69.0 2024