diff --git a/modules/next/next.module b/modules/next/next.module index 2b85123b..815cb70d 100644 --- a/modules/next/next.module +++ b/modules/next/next.module @@ -69,7 +69,7 @@ function next_next_site_preview_alter(array &$preview, array $context) { */ function next_entity_insert(EntityInterface $entity) { $event = EntityActionEvent::createFromEntity($entity, EntityActionEventInterface::INSERT_ACTION); - drupal_register_shutdown_function('_next_dispatch_entity_action_event', $event); + \Drupal::service('next.entity_action_event_dispatcher')->addEvent($event); } /** @@ -77,7 +77,7 @@ function next_entity_insert(EntityInterface $entity) { */ function next_entity_update(EntityInterface $entity) { $event = EntityActionEvent::createFromEntity($entity, EntityActionEventInterface::UPDATE_ACTION); - drupal_register_shutdown_function('_next_dispatch_entity_action_event', $event); + \Drupal::service('next.entity_action_event_dispatcher')->addEvent($event); } /** @@ -85,15 +85,5 @@ function next_entity_update(EntityInterface $entity) { */ function next_entity_predelete(EntityInterface $entity) { $event = EntityActionEvent::createFromEntity($entity, EntityActionEventInterface::DELETE_ACTION); - drupal_register_shutdown_function('_next_dispatch_entity_action_event', $event); -} - -/** - * Helper to dispatch an entity action event. - * - * @param \Drupal\next\Event\EntityActionEventInterface $event - * The entity action event. - */ -function _next_dispatch_entity_action_event(EntityActionEventInterface $event) { - \Drupal::service('event_dispatcher')->dispatch($event, EntityEvents::ENTITY_ACTION); + \Drupal::service('next.entity_action_event_dispatcher')->addEvent($event); } diff --git a/modules/next/next.services.yml b/modules/next/next.services.yml index 1788ebbe..b5afca86 100644 --- a/modules/next/next.services.yml +++ b/modules/next/next.services.yml @@ -65,3 +65,9 @@ services: ] tags: - { name: event_subscriber } + + next.entity_action_event_dispatcher: + class: Drupal\next\EventSubscriber\EntityActionEventDispatcher + arguments: ['@event_dispatcher'] + tags: + - { name: needs_destruction } diff --git a/modules/next/src/EventSubscriber/EntityActionEventDispatcher.php b/modules/next/src/EventSubscriber/EntityActionEventDispatcher.php new file mode 100644 index 00000000..ee043a66 --- /dev/null +++ b/modules/next/src/EventSubscriber/EntityActionEventDispatcher.php @@ -0,0 +1,49 @@ +events[] = $event; + } + + /** + * {@inheritdoc} + */ + public function destruct() { + foreach ($this->events as $event) { + $this->eventDispatcher->dispatch($event, EntityEvents::ENTITY_ACTION); + } + } + +} diff --git a/modules/next/src/Form/NextSiteForm.php b/modules/next/src/Form/NextSiteForm.php index 46741ffd..94aa6683 100644 --- a/modules/next/src/Form/NextSiteForm.php +++ b/modules/next/src/Form/NextSiteForm.php @@ -38,7 +38,7 @@ public function form(array $form, FormStateInterface $form_state) { ]; $form['base_url'] = [ - '#type' => 'textfield', + '#type' => 'url', '#title' => $this->t('Base URL'), '#description' => $this->t('Enter the base URL for the Next.js site. Example: https://example.com.'), '#default_value' => $entity->getBaseUrl(), @@ -60,7 +60,7 @@ public function form(array $form, FormStateInterface $form_state) { ]; $form['preview']['preview_url'] = [ - '#type' => 'textfield', + '#type' => 'url', '#title' => $this->t('Preview URL'), '#description' => $this->t('Enter the preview URL. Example: https://example.com/api/preview.'), '#default_value' => $entity->getPreviewUrl(), @@ -83,7 +83,7 @@ public function form(array $form, FormStateInterface $form_state) { ]; $form['revalidation']['revalidate_url'] = [ - '#type' => 'textfield', + '#type' => 'url', '#title' => $this->t('Revalidate URL'), '#description' => $this->t('Enter the revalidate URL. Example: https://example.com/api/revalidate.'), '#default_value' => $entity->getRevalidateUrl(), diff --git a/modules/next/tests/src/Kernel/Event/EntityActionEventTest.php b/modules/next/tests/src/Kernel/Event/EntityActionEventTest.php index 3db364ba..4af09b25 100644 --- a/modules/next/tests/src/Kernel/Event/EntityActionEventTest.php +++ b/modules/next/tests/src/Kernel/Event/EntityActionEventTest.php @@ -7,6 +7,8 @@ use Drupal\KernelTests\KernelTestBase; use Drupal\node\Entity\NodeType; use Drupal\Tests\node\Traits\NodeCreationTrait; +use Symfony\Component\HttpFoundation\Request; +use Symfony\Component\HttpFoundation\Response; /** * Tests the EntityActionEvent. @@ -60,17 +62,17 @@ public function testEntityActionEvents() { // Insert. $page->save(); - _drupal_shutdown_function(); + $this->container->get('kernel')->terminate(Request::create('/'), new Response()); $this->assertLogsContains("Event next.entity.action dispatched for entity A page and action insert."); // Update. $page->set('title', 'A page updated')->save(); - _drupal_shutdown_function(); + $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. $page->delete(); - _drupal_shutdown_function(); + $this->container->get('kernel')->terminate(Request::create('/'), new Response()); $this->assertLogsContains("Event next.entity.action dispatched for entity A page updated and action delete."); } diff --git a/modules/next/tests/src/Kernel/Event/EntityRevalidatedEventTest.php b/modules/next/tests/src/Kernel/Event/EntityRevalidatedEventTest.php index 51d9e983..e8c69d07 100644 --- a/modules/next/tests/src/Kernel/Event/EntityRevalidatedEventTest.php +++ b/modules/next/tests/src/Kernel/Event/EntityRevalidatedEventTest.php @@ -7,6 +7,8 @@ use Drupal\KernelTests\KernelTestBase; use Drupal\next\Entity\NextEntityTypeConfig; use Drupal\Tests\node\Traits\NodeCreationTrait; +use Symfony\Component\HttpFoundation\Request; +use Symfony\Component\HttpFoundation\Response; /** * Tests the EntityRevalidatedEvent. @@ -70,17 +72,17 @@ public function testEntityRevalidatedEvents() { // Insert. $page->save(); - _drupal_shutdown_function(); + $this->container->get('kernel')->terminate(Request::create('/'), new Response()); $this->assertLogsContains("Entity A page, action insert, revalidated 0."); // Update. $page->set('title', 'A page updated')->save(); - _drupal_shutdown_function(); + $this->container->get('kernel')->terminate(Request::create('/'), new Response()); $this->assertLogsContains("Entity A page updated, action update, revalidated 0."); // Delete. $page->delete(); - _drupal_shutdown_function(); + $this->container->get('kernel')->terminate(Request::create('/'), new Response()); $this->assertLogsContains("Entity A page updated, action delete, revalidated 0."); } diff --git a/modules/next/tests/src/Kernel/Plugin/PathRevalidatorTest.php b/modules/next/tests/src/Kernel/Plugin/PathRevalidatorTest.php index 56306460..e3f79236 100644 --- a/modules/next/tests/src/Kernel/Plugin/PathRevalidatorTest.php +++ b/modules/next/tests/src/Kernel/Plugin/PathRevalidatorTest.php @@ -8,6 +8,8 @@ use Drupal\Tests\node\Traits\NodeCreationTrait; use GuzzleHttp\ClientInterface; use Prophecy\PhpUnit\ProphecyTrait; +use Symfony\Component\HttpFoundation\Request; +use Symfony\Component\HttpFoundation\Response; /** * Tests the path revalidator plugin. @@ -80,13 +82,13 @@ public function testRevalidate() { $client->request('GET', $this->any())->shouldNotBeCalled(); $page = $this->createNode(); $page->save(); - _drupal_shutdown_function(); + $this->container->get('kernel')->terminate(Request::create('/'), new Response()); $client->request('GET', 'http://blog.com/api/revalidate?slug=/node/2')->shouldBeCalled(); $blog_site->setRevalidateUrl('http://blog.com/api/revalidate')->save(); $page = $this->createNode(); $page->save(); - _drupal_shutdown_function(); + $this->container->get('kernel')->terminate(Request::create('/'), new Response()); $marketing = NextSite::create([ 'id' => 'marketing', @@ -105,7 +107,7 @@ public function testRevalidate() { $client->request('GET', 'http://blog.com/api/revalidate?slug=/node/3')->shouldBeCalled(); $page = $this->createNode(); $page->save(); - _drupal_shutdown_function(); + $this->container->get('kernel')->terminate(Request::create('/'), new Response()); $entity_type_config->setRevalidatorConfiguration('path', [ 'additional_paths' => "/\n/blog", @@ -119,7 +121,7 @@ public function testRevalidate() { $client->request('GET', 'http://blog.com/api/revalidate?slug=/blog')->shouldBeCalled(); $page = $this->createNode(); $page->save(); - _drupal_shutdown_function(); + $this->container->get('kernel')->terminate(Request::create('/'), new Response()); } } diff --git a/package.json b/package.json index 07dfe41a..1825d36a 100644 --- a/package.json +++ b/package.json @@ -19,7 +19,7 @@ "format": "prettier --write .", "format:check": "prettier --check .", "phpcs": "./drupal/vendor/bin/phpcs -p -s --colors --standard=modules/next/phpcs.xml modules/next", - "test:next": "./drupal/vendor/bin/phpunit -c ./drupal/web modules/next", + "test:next": "SIMPLETEST_DB=sqlite://localhost/:memory: ./drupal/vendor/bin/phpunit -c ./drupal/web/core modules/next", "sync:modules": "./scripts/sync-modules.sh \"modules/*\"", "sync:examples": "./scripts/sync-examples.sh \"examples/example-*\"", "sync:starters": "./scripts/sync-starters.sh \"starters/*\"",