Allow storage classes to define their own dependencies and services

Created on 12 May 2014, almost 11 years ago
Updated 17 March 2025, about 2 months ago

Problem/Motivation

We would like a storage class that is decoupled from the actual table layout.

Proposed resolution

A separate factory encapsulating the table-layout logic that instantiates the table mapping. We could exploit per-entity-type services to make the factory swappable.

Every storage class can implement its own ::createInstance() method and define its own dependencies and services. As long as we return an implementation of TableMappingFactoryInterface, there is really no point in requiring the service string identifier to be fixed. For instance:

class ContentEntityDatabaseStorage extends ContentEntityStorageBase implements SqlStorageInterface, ContentEntitySchemaHandlerInterface {
  public function __construct(EntityTypeInterface $entity_type, Connection $database, EntityManagerInterface $entity_manager, FieldInfo $field_info, TableMappingFactoryInterface $table_mapping_factory) {
    // ...
  }
}
class NodeStorage extends ContentEntityDatabaseStorage {
  /**
   * {@inheritdoc}
   */
  public static function createInstance(ContainerInterface $container, EntityTypeInterface $entity_type) {
    return new static(
      $entity_type,
      $container->get('database'),
      $container->get('entity.manager'),
      $container->get('field.info'),
      $container->get('node.table_mapping_factory')
    );
  }
}
class CommentStorage extends ContentEntityDatabaseStorage {
  /**
   * {@inheritdoc}
   */
  public static function createInstance(ContainerInterface $container, EntityTypeInterface $entity_type) {
    return new static(
      $entity_type,
      $container->get('database'),
      $container->get('entity.manager'),
      $container->get('field.info'),
      $container->get('comment.table_mapping_factory')
    );
  }
}

Remaining tasks

Do it.

User interface changes

None.

API changes

Perhaps. Minor and largely non-disruptive if so. We would probably end-up changing the services passed to ContentEntityDatabaseStorage::__construct()

πŸ“Œ Task
Status

Postponed: needs info

Version

11.0 πŸ”₯

Component

entity system

Created by

πŸ‡ΊπŸ‡ΈUnited States jessebeach

Live updates comments and jobs are added and updated live.
  • stale-issue-cleanup

    To track issues in the developing policy for closing stale issues, [Policy, no patch] closing older issues

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.

  • πŸ‡ΊπŸ‡ΈUnited States smustgrave

    Thank you for creating this issue to improve Drupal.

    We are working to decide if this task is still relevant to a currently supported version of Drupal. There hasn't been any discussion here for over 8 years which suggests that this has either been implemented or is no longer relevant. Your thoughts on this will allow a decision to be made.

    Since we need more information to move forward with this issue, the status is now Postponed (maintainer needs more info). If we don't receive additional information to help with the issue, it may be closed after three months.

Production build 0.71.5 2024