Add a method for saving cloned entity in handler

Created on 6 November 2023, about 1 year ago
Updated 9 August 2024, 3 months ago

Problem/Motivation

On our project we created a custom handler for saving cloned entities. We extended ContentEntityCloneBase and then overrode ContentEntityCloneBase::cloneEntity() we only needed a few minor changes so we had to copy the most the code from the base class into our own handler. When we upgraded the module then we started seeing all sorts of odd behavior. That was because our version of ContentEntityCloneBase::cloneEntity was outdated. We have to copy and modify that code again.

Note that we are using the Event subscribers as well but our use case also required us to create a custom handler.

Proposed resolution

Add in another function either right before $cloned_entity->save()or perhaps have a function that contains that code itself. This will make it much safer for developers to write their own handlers.

  /**
   * Save the cloned entity.
   *
   * @param \Drupal\Core\Entity\EntityInterface $entity
   *   The entity.
   * @param \Drupal\Core\Entity\EntityInterface $cloned_entity
   *   The cloned entity.
   * @param array $properties
   *   The array of properties.
   */
  protected function saveClonedEntity(EntityInterface $entity, EntityInterface $cloned_entity, array $properties = []) {
    $cloned_entity->save();
  }

I've attached a patch with an example but I'm open to any sort of idea.

✨ Feature request
Status

Active

Version

2.0

Component

Code

Created by

πŸ‡ΊπŸ‡ΈUnited States partdigital

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

Comments & Activities

Production build 0.71.5 2024