From f3dbfd94fe3c628c9615d4ac7e58bca24e789ff9 Mon Sep 17 00:00:00 2001 From: Bojan Bogdanovic Date: Tue, 18 Jun 2024 16:13:16 +0200 Subject: [PATCH 1/2] Add event action on entity translation delete --- modules/next/next.module | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/modules/next/next.module b/modules/next/next.module index 815cb70d..9a1787e1 100644 --- a/modules/next/next.module +++ b/modules/next/next.module @@ -87,3 +87,11 @@ function next_entity_predelete(EntityInterface $entity) { $event = EntityActionEvent::createFromEntity($entity, EntityActionEventInterface::DELETE_ACTION); \Drupal::service('next.entity_action_event_dispatcher')->addEvent($event); } + +/** + * Implements hook_entity_translation_delete(). + */ +function next_entity_translation_delete(EntityInterface $translation) { + $event = EntityActionEvent::createFromEntity($translation, EntityActionEventInterface::DELETE_ACTION); + \Drupal::service('next.entity_action_event_dispatcher')->addEvent($event); +} From 9f2f14e315e6f369d8147ef584b236016f9f6543 Mon Sep 17 00:00:00 2001 From: Bojan Bogdanovic Date: Wed, 19 Jun 2024 08:48:55 +0200 Subject: [PATCH 2/2] Add test coverage --- .../src/Kernel/Event/EntityActionEventTest.php | 15 ++++++++++++++- .../Kernel/Event/EntityRevalidatedEventTest.php | 13 +++++++++++++ 2 files changed, 27 insertions(+), 1 deletion(-) diff --git a/modules/next/tests/src/Kernel/Event/EntityActionEventTest.php b/modules/next/tests/src/Kernel/Event/EntityActionEventTest.php index 4af09b25..a2636fc7 100644 --- a/modules/next/tests/src/Kernel/Event/EntityActionEventTest.php +++ b/modules/next/tests/src/Kernel/Event/EntityActionEventTest.php @@ -5,6 +5,7 @@ use Drupal\Core\Database\Database; use Drupal\dblog\Controller\DbLogController; use Drupal\KernelTests\KernelTestBase; +use Drupal\language\Entity\ConfigurableLanguage; use Drupal\node\Entity\NodeType; use Drupal\Tests\node\Traits\NodeCreationTrait; use Symfony\Component\HttpFoundation\Request; @@ -30,6 +31,8 @@ class EntityActionEventTest extends KernelTestBase { 'system', 'user', 'dblog', + 'content_translation', + 'language', ]; /** @@ -40,7 +43,7 @@ protected function setUp(): void { $this->installEntitySchema('node'); $this->installEntitySchema('user'); - $this->installConfig(['filter', 'next', 'system', 'user']); + $this->installConfig(['filter', 'next', 'system', 'user', 'language']); $this->installSchema('dblog', ['watchdog']); $this->installSchema('system', ['sequences']); $this->installSchema('node', ['node_access']); @@ -52,6 +55,9 @@ protected function setUp(): void { 'label' => 'Page', ]); $page_type->save(); + + // Set up multilingual. + ConfigurableLanguage::createFromLangcode('nl')->save(); } /** @@ -59,6 +65,7 @@ protected function setUp(): void { */ public function testEntityActionEvents() { $page = $this->createNode(['type' => 'page', 'title' => 'A page']); + $page->addTranslation('nl', ['title' => 'Translation']); // Insert. $page->save(); @@ -70,6 +77,12 @@ public function testEntityActionEvents() { $this->container->get('kernel')->terminate(Request::create('/'), new Response()); $this->assertLogsContains("Event next.entity.action dispatched for entity A page updated and action update."); + // Delete translation. + $page->removeTranslation('nl'); + $page->save(); + $this->container->get('kernel')->terminate(Request::create('/'), new Response()); + $this->assertLogsContains("Event next.entity.action dispatched for entity Translation and action delete."); + // Delete. $page->delete(); $this->container->get('kernel')->terminate(Request::create('/'), new Response()); diff --git a/modules/next/tests/src/Kernel/Event/EntityRevalidatedEventTest.php b/modules/next/tests/src/Kernel/Event/EntityRevalidatedEventTest.php index e8c69d07..928819eb 100644 --- a/modules/next/tests/src/Kernel/Event/EntityRevalidatedEventTest.php +++ b/modules/next/tests/src/Kernel/Event/EntityRevalidatedEventTest.php @@ -5,6 +5,7 @@ use Drupal\Core\Database\Database; use Drupal\dblog\Controller\DbLogController; use Drupal\KernelTests\KernelTestBase; +use Drupal\language\Entity\ConfigurableLanguage; use Drupal\next\Entity\NextEntityTypeConfig; use Drupal\Tests\node\Traits\NodeCreationTrait; use Symfony\Component\HttpFoundation\Request; @@ -30,6 +31,8 @@ class EntityRevalidatedEventTest extends KernelTestBase { 'system', 'user', 'dblog', + 'content_translation', + 'language', ]; /** @@ -47,6 +50,9 @@ protected function setUp(): void { $this->installSchema('node', ['node_access']); $this->installSchema('user', ['users_data']); + // Set up multilingual. + ConfigurableLanguage::createFromLangcode('nl')->save(); + // Create entity type config. $entity_type_config = NextEntityTypeConfig::create([ 'id' => 'node.page', @@ -69,6 +75,7 @@ protected function setUp(): void { */ public function testEntityRevalidatedEvents() { $page = $this->createNode(['type' => 'page', 'title' => 'A page']); + $page->addTranslation('nl', ['title' => 'Translation']); // Insert. $page->save(); @@ -80,6 +87,12 @@ public function testEntityRevalidatedEvents() { $this->container->get('kernel')->terminate(Request::create('/'), new Response()); $this->assertLogsContains("Entity A page updated, action update, revalidated 0."); + // Delete translation. + $page->removeTranslation('nl'); + $page->save(); + $this->container->get('kernel')->terminate(Request::create('/'), new Response()); + $this->assertLogsContains("Entity Translation, action delete, revalidated 0."); + // Delete. $page->delete(); $this->container->get('kernel')->terminate(Request::create('/'), new Response());