Specifying a custom clone handler unecessarily requires specifying a clone form handler too

Created on 8 May 2021, over 3 years ago
Updated 30 July 2023, over 1 year ago

Problem/Motivation

If you specify an entity clone handler for an entity type, you must also specify an entity clone form handler, because the mechanism for setting a default entity clone form fails when an entity clone handler is set.

Steps to reproduce

Doing this doesn't work properly:

function my_module_entity_type_build(array &$entity_types) {
  $entity_types['my_entity']->setHandlerClass('entity_clone', \Drupal\my_module\EntityClone\MyEntityEntityClone::class);
}

instead you have to do this, which is silly:

function my_module_entity_type_build(array &$entity_types) {
  $entity_types['my_entity']->setHandlerClass('entity_clone', \Drupal\my_module\EntityClone\MyEntityEntityClone::class);
  $entity_types['my_entity']->setHandlerClass('entity_clone_form', \Drupal\entity_clone\EntityClone\Content\ContentEntityCloneFormBase::class);
}

Proposed resolution

Fix the current code:

function entity_clone_entity_type_build(array &$entity_types) {
...

  /** @var \Drupal\Core\Entity\EntityTypeInterface[] $entity_types */
  foreach ($entity_types as $entity_type_id => $entity_type) {
    $has_entity_clone_handler = $entity_type->getHandlerClass('entity_clone');
    if (!$has_entity_clone_handler) {
      if ($entity_type instanceof ContentEntityTypeInterface) {
        $entity_type->setHandlerClass('entity_clone', ContentEntityCloneBase::class);
        $entity_type->setHandlerClass('entity_clone_form', ContentEntityCloneFormBase::class);
      }
      elseif ($entity_type instanceof ConfigEntityTypeInterface) {
        $entity_type->setHandlerClass('entity_clone', ConfigEntityCloneBase::class);
        $entity_type->setHandlerClass('entity_clone_form', ConfigEntityCloneFormBase::class);
      }
    }

to something like this:

function entity_clone_entity_type_build(array &$entity_types) {
...

  /** @var \Drupal\Core\Entity\EntityTypeInterface[] $entity_types */
  foreach ($entity_types as $entity_type_id => $entity_type) {
    $has_entity_clone_handler = $entity_type->getHandlerClass('entity_clone');
    $has_entity_clone_form_handler = $entity_type->getHandlerClass('entity_clone_form');
    $isContent = $entity_type instanceof ContentEntityTypeInterface;
    $isConfig = $entity_type instanceof ConfigEntityTypeInterface;
    if (!$has_entity_clone_handler && $isContent) {
      $entity_type->setHandlerClass('entity_clone', ContentEntityCloneBase::class);
    }
    elseif (!$has_entity_clone_handler && $isConfig) {
      $entity_type->setHandlerClass('entity_clone', ConfigEntityCloneBase::class);
    }
    if (!$has_entity_clone_form_handler && $isContent) {
      $entity_type->setHandlerClass('entity_clone_form', ContentEntityCloneBase::class);
    }
    elseif (!$has_entity_clone_form_handler && $isConfig) {
      $entity_type->setHandlerClass('entity_clone_form', ConfigEntityCloneBase::class);
    }

Remaining tasks

User interface changes

None.

API changes

None.

Data model changes

None.

🐛 Bug report
Status

Active

Version

1.0

Component

Code

Created by

🇬🇧United Kingdom jonathanshaw Stroud, UK

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.

Production build 0.71.5 2024