diff -u b/src/PanelsEvents.php b/src/PanelsEvents.php --- b/src/PanelsEvents.php +++ b/src/PanelsEvents.php @@ -8,9 +8,22 @@ final class PanelsEvents { /** - * The name of the event triggered when a Panels display variant is saved. + * The name of the event triggered before a Panels display variant is saved. * - * This event allows modules to react to a Panels display variant being saved. + * This event allows modules to react before a Panels display variant is + * saved. The event listener method receives a + * \Drupal\panels\PanelsVariantEvent instance. + * + * @Event + * + * @var string + */ + const VARIANT_PRE_SAVE = 'panels.variant.pre_save'; + + /** + * The name of the event triggered after a Panels display variant is saved. + * + * This event allows modules to react after a Panels display variant is saved. * The event listener method receives a \Drupal\panels\PanelsVariantEvent * instance. * @@ -19,5 +32,5 @@ * @var string */ - const VARIANT_SAVE = 'panels.variant_save'; + const VARIANT_POST_SAVE = 'panels.variant.post_save'; } reverted: --- b/src/Plugin/PanelsStorage/PageManagerPanelsStorage.php +++ a/src/Plugin/PanelsStorage/PageManagerPanelsStorage.php @@ -18,8 +18,6 @@ class PageManagerPanelsStorage extends PanelsStorageBase implements ContainerFactoryPluginInterface { /** - * The entity type manager. - * * @var \Drupal\Core\Entity\EntityTypeManagerInterface */ protected $entityTypeManager; diff -u b/src/Storage/PanelsStorageBase.php b/src/Storage/PanelsStorageBase.php --- b/src/Storage/PanelsStorageBase.php +++ b/src/Storage/PanelsStorageBase.php @@ -5,7 +5,4 @@ use Drupal\Component\Plugin\PluginBase; -/** - * Base class for Panels storage plugins. - */ abstract class PanelsStorageBase extends PluginBase implements PanelsStorageInterface { } diff -u b/src/Storage/PanelsStorageManager.php b/src/Storage/PanelsStorageManager.php --- b/src/Storage/PanelsStorageManager.php +++ b/src/Storage/PanelsStorageManager.php @@ -95,12 +95,13 @@ * {@inheritdoc} */ public function save(PanelsDisplayVariant $panels_display) { - $storage = $this->getStorage($panels_display->getStorageType()); - $storage->save($panels_display); - // Allow event subscribers to react to the variant being saved. $event = new PanelsVariantEvent($panels_display); - $this->eventDispatcher->dispatch(PanelsEvents::VARIANT_SAVE, $event); + + $this->eventDispatcher->dispatch(PanelsEvents::VARIANT_PRE_SAVE, $event); + $storage = $this->getStorage($panels_display->getStorageType()); + $storage->save($panels_display); + $this->eventDispatcher->dispatch(PanelsEvents::VARIANT_POST_SAVE, $event); } /** diff -u b/tests/src/Kernel/PanelsStorageManagerTest.php b/tests/src/Kernel/PanelsStorageManagerTest.php --- b/tests/src/Kernel/PanelsStorageManagerTest.php +++ b/tests/src/Kernel/PanelsStorageManagerTest.php @@ -5,8 +5,6 @@ use Drupal\KernelTests\KernelTestBase; use Drupal\panels\PanelsEvents; use Drupal\panels\PanelsVariantEvent; -use Prophecy\Argument; -use Symfony\Component\EventDispatcher\EventDispatcherInterface; /** * @coversDefaultClass \Drupal\panels\Storage\PanelsStorageManager @@ -23,15 +21,15 @@ * Tests that events are fired by the storage manager. */ public function testEvents() { - // Swap in a mock event dispatcher so we can spy on method calls. - $event_dispatcher = $this->prophesize(EventDispatcherInterface::class); - $this->container->set('event_dispatcher', $event_dispatcher->reveal()); - - // Set up the expected calls to the event dispatcher. - $event = Argument::type(PanelsVariantEvent::class); - $event_dispatcher - ->dispatch(PanelsEvents::VARIANT_SAVE, $event) - ->shouldBeCalled(); + /** @var \Symfony\Component\EventDispatcher\EventDispatcherInterface $event_dispatcher */ + $event_dispatcher = $this->container->get('event_dispatcher'); + + $event_dispatcher->addListener(PanelsEvents::VARIANT_PRE_SAVE, function (PanelsVariantEvent $event) { + $event->getVariant()->testPreSave = TRUE; + }); + $event_dispatcher->addListener(PanelsEvents::VARIANT_POST_SAVE, function (PanelsVariantEvent $event) { + $event->getVariant()->testPostSave = TRUE; + }); /** @var \Drupal\page_manager\PageInterface $page */ $page = $this->container @@ -62,9 +60,10 @@ ]); $variant->save(); - $this->container - ->get('panels.storage_manager') - ->save($variant->getVariantPlugin()); + $display = $variant->getVariantPlugin(); + $this->container->get('panels.storage_manager')->save($display); + $this->assertObjectHasAttribute('testPreSave', $display); + $this->assertObjectHasAttribute('testPostSave', $display); } } reverted: --- b/tests/src/Unit/PanelsStorageTest.php +++ a/tests/src/Unit/PanelsStorageTest.php @@ -82,12 +82,7 @@ $this->storage->load('doesnt_exist')->willReturn(NULL); $this->storage->load('not_a_panel')->willReturn($this->pageVariantNotPanels->reveal()); + $panels_storage = new PageManagerPanelsStorage([], '', [], $this->entityTypeManager->reveal()); - $panels_storage = new PageManagerPanelsStorage( - [], - '', - [], - $this->entityTypeManager->reveal() - ); // Test the success condition. $this->assertSame($this->panelsDisplay->reveal(), $panels_storage->load('id_exists')); @@ -113,12 +108,7 @@ $panels_display->getStorageId()->willReturn('id_exists'); $panels_display->getConfiguration()->willReturn($test_config); + $panels_storage = new PageManagerPanelsStorage([], '', [], $this->entityTypeManager->reveal()); - $panels_storage = new PageManagerPanelsStorage( - [], - '', - [], - $this->entityTypeManager->reveal() - ); $panels_storage->save($panels_display->reveal()); } @@ -138,12 +128,7 @@ $panels_display->getStorageId()->willReturn('doesnt_exist'); $panels_display->getConfiguration()->shouldNotBeCalled(); + $panels_storage = new PageManagerPanelsStorage([], '', [], $this->entityTypeManager->reveal()); - $panels_storage = new PageManagerPanelsStorage( - [], - '', - [], - $this->entityTypeManager->reveal() - ); $panels_storage->save($panels_display->reveal()); } @@ -163,12 +148,7 @@ $panels_display->getStorageId()->willReturn('not_a_panel'); $panels_display->getConfiguration()->shouldNotBeCalled(); + $panels_storage = new PageManagerPanelsStorage([], '', [], $this->entityTypeManager->reveal()); - $panels_storage = new PageManagerPanelsStorage( - [], - '', - [], - $this->entityTypeManager->reveal() - ); $panels_storage->save($panels_display->reveal()); } @@ -183,12 +163,7 @@ $this->pageVariant->access('read', $account->reveal(), TRUE)->willReturn(AccessResult::allowed()); + $panels_storage = new PageManagerPanelsStorage([], '', [], $this->entityTypeManager->reveal()); - $panels_storage = new PageManagerPanelsStorage( - [], - '', - [], - $this->entityTypeManager->reveal() - ); // Test the access condition. $this->assertEquals(AccessResult::allowed(), $panels_storage->access('id_exists', 'read', $account->reveal()));