diff --git a/src/Event/PageActionsEvent.php b/src/Event/PageActionsEvent.php index a410bd6be..d68c608dd 100644 --- a/src/Event/PageActionsEvent.php +++ b/src/Event/PageActionsEvent.php @@ -22,6 +22,7 @@ class PageActionsEvent extends ThemeEvent { private int $divider = 0; private ?string $locale = null; + private ?object $item = null; /** * @param array $payload @@ -32,10 +33,17 @@ class PageActionsEvent extends ThemeEvent if (!\array_key_exists('actions', $payload)) { $payload['actions'] = []; } + // only for BC reasons, do not access it directly! if (!\array_key_exists('view', $payload)) { $payload['view'] = $view; } + + if (\array_key_exists('item', $payload)) { + $this->item = $payload['item']; + unset($payload['item']); + } + parent::__construct($user, $payload); } @@ -228,4 +236,12 @@ class PageActionsEvent extends ThemeEvent { $this->locale = $locale; } + + /** + * Returns null for "listing" pages. + */ + public function getItem(): object|null + { + return $this->item; + } } diff --git a/tests/Event/PageActionsEventTest.php b/tests/Event/PageActionsEventTest.php index f9670a46d..a24d76e82 100644 --- a/tests/Event/PageActionsEventTest.php +++ b/tests/Event/PageActionsEventTest.php @@ -33,10 +33,15 @@ class PageActionsEventTest extends TestCase self::assertEquals([], $sut->getActions()); self::assertEquals(['actions' => [], 'view' => 'bar'], $sut->getPayload()); self::assertNull($sut->getLocale()); + self::assertNull($sut->getItem()); - $sut = new PageActionsEvent($user, ['hello' => 'world'], 'foo', 'bar'); + $item = new User(); + + $sut = new PageActionsEvent($user, ['hello' => 'world', 'item' => $item], 'foo', 'bar'); self::assertSame($user, $sut->getUser()); self::assertEquals([], $sut->getActions()); + self::assertNotNull($sut->getItem()); + self::assertInstanceOf(User::class, $sut->getItem()); self::assertEquals(['hello' => 'world', 'actions' => [], 'view' => 'bar'], $sut->getPayload()); }