From b1c5591c0fa592a85c1054298f120963c429db53 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lo=C3=AFc=20Fr=C3=A9mont?= Date: Tue, 5 Nov 2024 12:24:53 +0100 Subject: [PATCH 1/3] Fix form without data id --- .../templates/shared/crud/common/content/form.html.twig | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/src/BootstrapAdminUi/templates/shared/crud/common/content/form.html.twig b/src/BootstrapAdminUi/templates/shared/crud/common/content/form.html.twig index 5b38bfea..0a2dc286 100644 --- a/src/BootstrapAdminUi/templates/shared/crud/common/content/form.html.twig +++ b/src/BootstrapAdminUi/templates/shared/crud/common/content/form.html.twig @@ -2,6 +2,10 @@ {% set form = hookable_metadata.context.form %} {% endif %} +{% if resource is not defined %} + {% set resource = hookable_metadata.context.resource %} +{% endif %} + {% form_theme form '@SyliusBootstrapAdminUi/shared/form_theme.html.twig' %}
@@ -9,13 +13,13 @@
{% if hookable_metadata.configuration.method is defined %} - {% elseif form.vars.data.id is not null %} + {% elseif form.vars.data.id|default(null) is not null %} {% endif %} {{ form_errors(form) }} {{ form_widget(form._token) }} - {% hook 'form' with { form } %} + {% hook 'form' with { form, resource } %}
{{ form_end(form, {render_rest: hookable_metadata.configuration.render_rest|default(false)}) }}
From 0d2c9016e185744383682285d1f15cd4b4ed3755 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lo=C3=AFc=20Fr=C3=A9mont?= Date: Wed, 6 Nov 2024 10:00:43 +0100 Subject: [PATCH 2/3] [Fix] Add missing getting started links to the summary --- docs/SUMMARY.md | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/docs/SUMMARY.md b/docs/SUMMARY.md index 1ce73977..4b4c7ccc 100644 --- a/docs/SUMMARY.md +++ b/docs/SUMMARY.md @@ -2,6 +2,14 @@ * [About Sylius Stack](README.md) +## Admin UI + +* [Getting started](admin-ui/getting-started.md) + +## Bootstrap Admin UI + +* [Getting started](bootstrap-admin-ui/getting-started.md) + ## 🍀 Twig Extra * [Getting started](twig-extra/getting-started.md) From fb7deebd865e2716b62771ae9564f8453e8b6a68 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lo=C3=AFc=20Fr=C3=A9mont?= Date: Thu, 7 Nov 2024 17:08:24 +0100 Subject: [PATCH 3/3] Fix rendering Twig hook message exception --- .../Renderer/Exception/HookRenderException.php | 14 ++++++++++++++ .../Renderer/HookableTemplateRenderer.php | 5 ++++- .../Renderer/HookableTemplateRendererTest.php | 18 ++++++++++++++++++ 3 files changed, 36 insertions(+), 1 deletion(-) diff --git a/src/TwigHooks/src/Hookable/Renderer/Exception/HookRenderException.php b/src/TwigHooks/src/Hookable/Renderer/Exception/HookRenderException.php index a67c54c1..ea858749 100644 --- a/src/TwigHooks/src/Hookable/Renderer/Exception/HookRenderException.php +++ b/src/TwigHooks/src/Hookable/Renderer/Exception/HookRenderException.php @@ -13,8 +13,22 @@ namespace Sylius\TwigHooks\Hookable\Renderer\Exception; +use Twig\Error\Error; use Twig\Error\RuntimeError; +use Twig\Source; class HookRenderException extends RuntimeError { + public function __construct(string $message, ?int $lineno = null, ?Source $source = null, ?\Throwable $previous = null) + { + $lineno ??= $previous?->getLine() ?? -1; + $source ??= $previous instanceof Error ? $previous->getSourceContext() : null; + + parent::__construct( + $message, + $lineno, + $source, + $previous, + ); + } } diff --git a/src/TwigHooks/src/Hookable/Renderer/HookableTemplateRenderer.php b/src/TwigHooks/src/Hookable/Renderer/HookableTemplateRenderer.php index d7745d35..adf8f6ae 100644 --- a/src/TwigHooks/src/Hookable/Renderer/HookableTemplateRenderer.php +++ b/src/TwigHooks/src/Hookable/Renderer/HookableTemplateRenderer.php @@ -42,12 +42,15 @@ public function render(AbstractHookable $hookable, HookableMetadata $metadata): return $this->twig->render($hookable->template, [ HooksRuntime::HOOKABLE_METADATA => $metadata, ]); + } catch (HookRenderException $exception) { + throw $exception; } catch (\Throwable $exception) { throw new HookRenderException( sprintf( - 'An error occurred during rendering the "%s" hook in the "%s" hookable', + 'An error occurred during rendering the "%s" hook in the "%s" hookable. %s', $hookable->name, $hookable->hookName, + $exception->getMessage(), ), previous: $exception, ); diff --git a/src/TwigHooks/tests/Unit/Hookable/Renderer/HookableTemplateRendererTest.php b/src/TwigHooks/tests/Unit/Hookable/Renderer/HookableTemplateRendererTest.php index 225c763d..a1f56d50 100644 --- a/src/TwigHooks/tests/Unit/Hookable/Renderer/HookableTemplateRendererTest.php +++ b/src/TwigHooks/tests/Unit/Hookable/Renderer/HookableTemplateRendererTest.php @@ -16,10 +16,12 @@ use PHPUnit\Framework\MockObject\MockObject; use PHPUnit\Framework\TestCase; use Sylius\TwigHooks\Hookable\Metadata\HookableMetadata; +use Sylius\TwigHooks\Hookable\Renderer\Exception\HookRenderException; use Sylius\TwigHooks\Hookable\Renderer\HookableTemplateRenderer; use Tests\Sylius\TwigHooks\Utils\MotherObject\HookableComponentMotherObject; use Tests\Sylius\TwigHooks\Utils\MotherObject\HookableTemplateMotherObject; use Twig\Environment as Twig; +use Twig\Error\Error; final class HookableTemplateRendererTest extends TestCase { @@ -65,6 +67,22 @@ public function testItRendersHookableTemplate(): void $this->assertSame('some-rendered-template', $renderedTemplate); } + public function testItThrowsAnExceptionWhenTwigThrowsAnError(): void + { + $metadata = $this->createMock(HookableMetadata::class); + + $this->twig->expects($this->once())->method('render')->with('some-template', [ + 'hookable_metadata' => $metadata, + ])->willThrowException(new Error('Unable to find the template.')); + + $hookable = HookableTemplateMotherObject::withTarget('some-template'); + + $this->expectException(HookRenderException::class); + $this->expectExceptionMessage('An error occurred during rendering the "some_name" hook in the "some_hook" hookable. Unable to find the template at line 76.'); + + $this->getTestSubject()->render($hookable, $metadata); + } + private function getTestSubject(): HookableTemplateRenderer { return new HookableTemplateRenderer($this->twig);