From 8e79c655035ef712dde0eba544e6ee2c9f0a6358 Mon Sep 17 00:00:00 2001 From: Vincent Langlet Date: Mon, 7 Mar 2022 11:52:14 +0100 Subject: [PATCH] Allow blockContext without template (#1026) --- src/Block/BlockContext.php | 35 +++++++++++++------------------ src/Block/BlockContextManager.php | 1 + 2 files changed, 15 insertions(+), 21 deletions(-) diff --git a/src/Block/BlockContext.php b/src/Block/BlockContext.php index 2964ab4a..b516ff1a 100644 --- a/src/Block/BlockContext.php +++ b/src/Block/BlockContext.php @@ -32,26 +32,6 @@ final class BlockContext implements BlockContextInterface */ public function __construct(BlockInterface $block, array $settings = []) { - if (!\array_key_exists('template', $settings)) { - @trigger_error( - 'Not providing a "template" setting is deprecated since sonata-project/block-bundle 4.10' - .' and will be throw an exception in version 5.0.', - \E_USER_DEPRECATED - ); - - // NEXT_MAJOR: Uncomment the exception instead. - // throw new \InvalidArgumentException('The "template" setting is required.'); - } elseif (!\is_string($settings['template'])) { - @trigger_error( - 'Not providing a string value for the "template" setting is deprecated since' - .' sonata-project/block-bundle 4.10 and will be throw an exception in version 5.0.', - \E_USER_DEPRECATED - ); - - // NEXT_MAJOR: Uncomment the exception instead. - // throw new \InvalidArgumentException('The "template" setting MUST be a string.'); - } - $this->block = $block; $this->settings = $settings; } @@ -91,6 +71,19 @@ public function setSetting(string $name, $value): BlockContextInterface */ public function getTemplate(): ?string { - return $this->getSetting('template'); + $template = $this->getSetting('template'); + + if (!\is_string($template)) { + @trigger_error( + 'Not providing a string value for the "template" setting is deprecated since' + .' sonata-project/block-bundle 4.10 and will be throw an exception in version 5.0.', + \E_USER_DEPRECATED + ); + + // NEXT_MAJOR: Uncomment the exception instead. + // throw new \InvalidArgumentException('The "template" setting MUST be a string.'); + } + + return $template; } } diff --git a/src/Block/BlockContextManager.php b/src/Block/BlockContextManager.php index bf553cd3..b75d13ed 100644 --- a/src/Block/BlockContextManager.php +++ b/src/Block/BlockContextManager.php @@ -118,6 +118,7 @@ public function get($meta, array $settings = []): BlockContextInterface $e->getMessage() )); + // NEXT_MAJOR: Only pass the template value if it's a string. $settings = $this->resolve($block, $settings + ['template' => $block->getSetting('template')]); }