Skip to content
Snippets Groups Projects
Commit aae43b18 authored by catch's avatar catch
Browse files

Issue #3040556 by sakiland, taran2l, jhuhta, berdir, richgerdes, julien.sibi,...

Issue #3040556 by sakiland, taran2l, jhuhta, berdir, richgerdes, julien.sibi, ksenzee, aaronmchale, bojanz, nicxvan, hchonov, godotislate, joachim: It is not possible to react to an entity being duplicated
parent 07f08eb8
No related branches found
No related tags found
7 merge requests!11197Issue #3506427 by eduardo morales alberti: Remove responsive_image.ajax from hook,!5423Draft: Resolve #3329907 "Test2",!3478Issue #3337882: Deleted menus are not removed from content type config,!2964Issue #2865710 : Dependencies from only one instance of a widget are used in display modes,!10223132456: Fix issue where views instances are emptied before an ajax request is complete,!579Issue #2230909: Simple decimals fail to pass validation,!213Issue #2906496: Give Media a menu item under Content
Pipeline #421116 passed with warnings
Pipeline: drupal

#421123

    Pipeline: drupal

    #421117

      ...@@ -1217,6 +1217,11 @@ public function createDuplicate() { ...@@ -1217,6 +1217,11 @@ public function createDuplicate() {
      $duplicate->loadedRevisionId = NULL; $duplicate->loadedRevisionId = NULL;
      } }
      // Modules might need to add or change the data initially held by the new
      // entity object, for instance to fill-in default values.
      \Drupal::moduleHandler()->invokeAll($this->getEntityTypeId() . '_duplicate', [$duplicate, $this]);
      \Drupal::moduleHandler()->invokeAll('entity_duplicate', [$duplicate, $this]);
      return $duplicate; return $duplicate;
      } }
      ......
      ...@@ -393,6 +393,12 @@ public function createDuplicate() { ...@@ -393,6 +393,12 @@ public function createDuplicate() {
      if ($entity_type->hasKey('uuid')) { if ($entity_type->hasKey('uuid')) {
      $duplicate->{$entity_type->getKey('uuid')} = $this->uuidGenerator()->generate(); $duplicate->{$entity_type->getKey('uuid')} = $this->uuidGenerator()->generate();
      } }
      // Modules might need to add or change the data initially held by the new
      // entity object, for instance to fill-in default values.
      \Drupal::moduleHandler()->invokeAll($this->getEntityTypeId() . '_duplicate', [$duplicate, $this]);
      \Drupal::moduleHandler()->invokeAll('entity_duplicate', [$duplicate, $this]);
      return $duplicate; return $duplicate;
      } }
      ......
      ...@@ -977,6 +977,36 @@ function hook_ENTITY_TYPE_create(\Drupal\Core\Entity\EntityInterface $entity) { ...@@ -977,6 +977,36 @@ function hook_ENTITY_TYPE_create(\Drupal\Core\Entity\EntityInterface $entity) {
      \Drupal::logger('example')->info('ENTITY_TYPE created: @label', ['@label' => $entity->label()]); \Drupal::logger('example')->info('ENTITY_TYPE created: @label', ['@label' => $entity->label()]);
      } }
      /**
      * Acts when duplicating an existing entity.
      *
      * @param \Drupal\Core\Entity\EntityInterface $duplicate
      * The duplicated entity object.
      * @param \Drupal\Core\Entity\EntityInterface $entity
      * The original entity object.
      *
      * @ingroup entity_crud
      * @see hook_ENTITY_TYPE_duplicate()
      */
      function hook_entity_duplicate(\Drupal\Core\Entity\EntityInterface $duplicate, \Drupal\Core\Entity\EntityInterface $entity): void {
      \Drupal::logger('example')->info('Entity duplicated: @label', ['@label' => $entity->label()]);
      }
      /**
      * Acts when duplicating an existing entity of a specific type.
      *
      * @param \Drupal\Core\Entity\EntityInterface $duplicate
      * The duplicated entity object.
      * @param \Drupal\Core\Entity\EntityInterface $entity
      * The original entity object.
      *
      * @ingroup entity_crud
      * @see hook_entity_duplicate()
      */
      function hook_ENTITY_TYPE_duplicate(\Drupal\Core\Entity\EntityInterface $duplicate, \Drupal\Core\Entity\EntityInterface $entity): void {
      \Drupal::logger('example')->info('ENTITY_TYPE duplicated: @label', ['@label' => $entity->label()]);
      }
      /** /**
      * Respond to entity revision creation. * Respond to entity revision creation.
      * *
      ......
      ...@@ -22,6 +22,7 @@ ...@@ -22,6 +22,7 @@
      use Drupal\Core\Entity\EntityTypeInterface; use Drupal\Core\Entity\EntityTypeInterface;
      use Drupal\Core\Hook\Attribute\Hook; use Drupal\Core\Hook\Attribute\Hook;
      use Drupal\entity_test\EntityTestHelper; use Drupal\entity_test\EntityTestHelper;
      use Drupal\entity_test\Entity\EntityTest;
      /** /**
      * Hook implementations for entity_test. * Hook implementations for entity_test.
      ...@@ -705,4 +706,24 @@ public function entityTestFormModeAlter(&$form_mode, EntityInterface $entity) : ...@@ -705,4 +706,24 @@ public function entityTestFormModeAlter(&$form_mode, EntityInterface $entity) :
      } }
      } }
      /**
      * Implements hook_entity_duplicate().
      */
      #[Hook('entity_duplicate')]
      public function entityDuplicateAlter(EntityInterface $duplicate, EntityInterface $entity) : void {
      if ($duplicate instanceof ContentEntityInterface && str_contains($duplicate->label(), 'UUID CRUD test entity') && $duplicate->hasField('name')) {
      $duplicate->set('name', $duplicate->label() . ' duplicate');
      }
      }
      /**
      * Implements hook_ENTITY_TYPE_duplicate().
      */
      #[Hook('entity_test_duplicate')]
      public function entityTestDuplicate(EntityTest $duplicate, EntityTest $entity) : void {
      if (str_contains($duplicate->label(), 'UUID CRUD test entity') && $duplicate->hasField('name')) {
      $duplicate->set('name', 'prefix ' . $duplicate->label());
      }
      }
      } }
      ...@@ -62,7 +62,7 @@ protected function assertCRUD(string $entity_type): void { ...@@ -62,7 +62,7 @@ protected function assertCRUD(string $entity_type): void {
      // Verify that a new UUID is generated upon creating an entity. // Verify that a new UUID is generated upon creating an entity.
      $entity = $this->container->get('entity_type.manager') $entity = $this->container->get('entity_type.manager')
      ->getStorage($entity_type) ->getStorage($entity_type)
      ->create(['name' => $this->randomMachineName()]); ->create(['name' => 'UUID CRUD test entity']);
      $uuid = $entity->uuid(); $uuid = $entity->uuid();
      $this->assertNotEmpty($uuid); $this->assertNotEmpty($uuid);
      ...@@ -109,6 +109,17 @@ protected function assertCRUD(string $entity_type): void { ...@@ -109,6 +109,17 @@ protected function assertCRUD(string $entity_type): void {
      $this->assertNotEquals($entity->{$property}->getValue(), $entity_duplicate->{$property}->getValue()); $this->assertNotEquals($entity->{$property}->getValue(), $entity_duplicate->{$property}->getValue());
      break; break;
      case 'name':
      // Assert alter hooks in \Drupal\entity_test\Hook\EntityTestHooks.
      if ($entity_type === 'entity_test') {
      $this->assertEquals('prefix UUID CRUD test entity duplicate', $entity_duplicate->label());
      }
      else {
      $this->assertEquals('UUID CRUD test entity duplicate', $entity_duplicate->label());
      }
      $this->assertEquals('UUID CRUD test entity', $entity->label());
      break;
      default: default:
      $this->assertEquals($entity->{$property}->getValue(), $entity_duplicate->{$property}->getValue()); $this->assertEquals($entity->{$property}->getValue(), $entity_duplicate->{$property}->getValue());
      } }
      ......
      ...@@ -507,6 +507,11 @@ public function testCreateDuplicate(): void { ...@@ -507,6 +507,11 @@ public function testCreateDuplicate(): void {
      $this->assertNull($duplicate->getOriginalId()); $this->assertNull($duplicate->getOriginalId());
      $this->assertNotEquals($this->entity->uuid(), $duplicate->uuid()); $this->assertNotEquals($this->entity->uuid(), $duplicate->uuid());
      $this->assertSame($new_uuid, $duplicate->uuid()); $this->assertSame($new_uuid, $duplicate->uuid());
      $this->moduleHandler->invokeAll($this->entityTypeId . '_duplicate', [$duplicate, $this->entity])
      ->shouldHaveBeenCalled();
      $this->moduleHandler->invokeAll('entity_duplicate', [$duplicate, $this->entity])
      ->shouldHaveBeenCalled();
      } }
      /** /**
      ......
      0% Loading or .
      You are about to add 0 people to the discussion. Proceed with caution.
      Please register or to comment