From ecbc6c47582f371fbf6f8147954809de34e95962 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Elias=20H=C3=A4u=C3=9Fler?= Date: Wed, 8 Jan 2025 08:25:12 +0100 Subject: [PATCH] [FEATURE] Allow modifying a single variable in `BeforeRenderingEvent` --- Classes/Event/BeforeRenderingEvent.php | 12 ++++++++++ Tests/Unit/Event/BeforeRenderingEventTest.php | 23 +++++++++++++++++++ 2 files changed, 35 insertions(+) diff --git a/Classes/Event/BeforeRenderingEvent.php b/Classes/Event/BeforeRenderingEvent.php index e2aa020c..12065a76 100644 --- a/Classes/Event/BeforeRenderingEvent.php +++ b/Classes/Event/BeforeRenderingEvent.php @@ -64,6 +64,18 @@ public function setVariables(array $variables): self return $this; } + public function addVariable(string $name, mixed $value): self + { + $this->variables[$name] = $value; + return $this; + } + + public function removeVariable(string $name): self + { + unset($this->variables[$name]); + return $this; + } + public function getRenderer(): HandlebarsRenderer { return $this->renderer; diff --git a/Tests/Unit/Event/BeforeRenderingEventTest.php b/Tests/Unit/Event/BeforeRenderingEventTest.php index 03348f09..85d74f10 100644 --- a/Tests/Unit/Event/BeforeRenderingEventTest.php +++ b/Tests/Unit/Event/BeforeRenderingEventTest.php @@ -69,6 +69,29 @@ public function setVariablesModifiesVariables(): void self::assertSame(['modified' => 'variables'], $this->subject->getVariables()); } + #[Framework\Attributes\Test] + public function addVariableAddsSingleVariable(): void + { + $this->subject->addVariable('foo', 'boo'); + $this->subject->addVariable('baz', 'foo'); + + self::assertSame( + [ + 'foo' => 'boo', + 'baz' => 'foo', + ], + $this->subject->getVariables(), + ); + } + + #[Framework\Attributes\Test] + public function removeVariableRemoveSingleVariable(): void + { + $this->subject->removeVariable('foo'); + + self::assertSame([], $this->subject->getVariables()); + } + #[Framework\Attributes\Test] public function getRendererReturnsRenderer(): void {