From ce45034714543c5351f6030b94a7c1af4719866d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lo=C3=AFc=20Fr=C3=A9mont?= Date: Mon, 6 Jan 2025 11:43:34 +0100 Subject: [PATCH] [AdminUi] Managing page title via Twig hooks --- src/AdminUi/templates/base.html.twig | 7 ++++--- src/AdminUi/templates/crud/create.html.twig | 9 ++++++--- src/AdminUi/templates/crud/index.html.twig | 9 ++++++--- src/AdminUi/templates/crud/show.html.twig | 9 ++++++--- src/AdminUi/templates/crud/update.html.twig | 9 ++++++--- src/AdminUi/tests/Functional/TemplatesTest.php | 2 +- .../config/app/twig_hooks/common/create.php | 15 +++++++++++++++ .../config/app/twig_hooks/common/index.php | 15 +++++++++++++++ .../config/app/twig_hooks/common/show.php | 15 +++++++++++++++ .../config/app/twig_hooks/common/update.php | 15 +++++++++++++++ .../config/app/twig_hooks/layout.php | 5 +++++ .../shared/crud/common/title/current.html.twig | 4 ++++ .../shared/crud/common/title/parent.html.twig | 2 ++ .../shared/crud/common/title/separator.html.twig | 1 + .../templates/shared/layout/title.html.twig | 1 + .../tests/Functional/TemplatesTest.php | 1 + 16 files changed, 103 insertions(+), 16 deletions(-) create mode 100644 src/BootstrapAdminUi/templates/shared/crud/common/title/current.html.twig create mode 100644 src/BootstrapAdminUi/templates/shared/crud/common/title/parent.html.twig create mode 100644 src/BootstrapAdminUi/templates/shared/crud/common/title/separator.html.twig create mode 100644 src/BootstrapAdminUi/templates/shared/layout/title.html.twig diff --git a/src/AdminUi/templates/base.html.twig b/src/AdminUi/templates/base.html.twig index 7684dae0..60af54ca 100644 --- a/src/AdminUi/templates/base.html.twig +++ b/src/AdminUi/templates/base.html.twig @@ -1,4 +1,5 @@ {% set generic_hook = 'sylius_admin.base' %} +{% set prefixes = prefixes|default([])|merge([generic_hook]) %} @@ -7,11 +8,11 @@ - {% block title %}Sylius{% endblock %} + {% block title %}{% apply striptags %}{% hook '#base_title' with { _prefixes: prefixes } %}{% endapply %}{% endblock %} {% block metatags %}{% endblock %} {% block stylesheets %} - {% hook generic_hook ~ '#stylesheets' %} + {% hook '#stylesheets' with { _prefixes: prefixes } %} {% endblock %} @@ -20,7 +21,7 @@ {% block javascripts %} - {% hook generic_hook ~ '#javascripts' %} + {% hook '#javascripts' with { _prefixes: prefixes } %} {% endblock %} diff --git a/src/AdminUi/templates/crud/create.html.twig b/src/AdminUi/templates/crud/create.html.twig index 2290bc6d..3522e2c4 100644 --- a/src/AdminUi/templates/crud/create.html.twig +++ b/src/AdminUi/templates/crud/create.html.twig @@ -5,9 +5,12 @@ 'sylius_admin.common' ] %} -{% set header = metadata.applicationName ~ '.ui.' ~ metadata.pluralName %} - -{% block title %}{{ header|trans }} | {{ parent() }}{% endblock %} +{% block title %} + {% apply striptags %} + {% set parent = parent() %} + {% hook 'create#title' with { _prefixes: prefixes } %} + {% endapply %} +{% endblock %} {% block body %} {% hook 'create' with { _prefixes: prefixes, resource, metadata, configuration, form } %} diff --git a/src/AdminUi/templates/crud/index.html.twig b/src/AdminUi/templates/crud/index.html.twig index f3a5876e..e0413ec3 100644 --- a/src/AdminUi/templates/crud/index.html.twig +++ b/src/AdminUi/templates/crud/index.html.twig @@ -5,9 +5,12 @@ 'sylius_admin.common' ] %} -{% set header = custom_header|default(metadata.applicationName ~ '.ui.' ~ metadata.pluralName) %} - -{% block title %}{{ header|trans }} | {{ parent() }}{% endblock %} +{% block title %} + {% apply striptags %} + {% set parent = parent() %} + {% hook 'index#title' with { _prefixes: prefixes } %} + {% endapply %} +{% endblock %} {% block body %} {% hook 'index' with { _prefixes: prefixes, metadata, resources } %} diff --git a/src/AdminUi/templates/crud/show.html.twig b/src/AdminUi/templates/crud/show.html.twig index 88cd8115..dbf49574 100644 --- a/src/AdminUi/templates/crud/show.html.twig +++ b/src/AdminUi/templates/crud/show.html.twig @@ -5,9 +5,12 @@ 'sylius_admin.common' ] %} -{% set header = metadata.applicationName ~ '.ui.' ~ metadata.pluralName %} - -{% block title %}{{ header|trans }} | {{ parent() }}{% endblock %} +{% block title %} + {% apply striptags %} + {% set parent = parent() %} + {% hook 'show#title' with { _prefixes: prefixes } %} + {% endapply %} +{% endblock %} {% block body %} {% hook 'show' with { _prefixes: prefixes, resource, metadata, configuration } %} diff --git a/src/AdminUi/templates/crud/update.html.twig b/src/AdminUi/templates/crud/update.html.twig index 332ebe1c..ed825c7c 100644 --- a/src/AdminUi/templates/crud/update.html.twig +++ b/src/AdminUi/templates/crud/update.html.twig @@ -5,9 +5,12 @@ 'sylius_admin.common' ] %} -{% set header = metadata.applicationName ~ '.ui.' ~ metadata.pluralName %} - -{% block title %}{{ header|trans }} | {{ parent() }}{% endblock %} +{% block title %} + {% apply striptags %} + {% set parent = parent() %} + {% hook 'update#title' with { _prefixes: prefixes } %} + {% endapply %} +{% endblock %} {% block body %} {% hook 'update' with { _prefixes: prefixes, resource, metadata, configuration, form } %} diff --git a/src/AdminUi/tests/Functional/TemplatesTest.php b/src/AdminUi/tests/Functional/TemplatesTest.php index 7a11c573..78100e9e 100644 --- a/src/AdminUi/tests/Functional/TemplatesTest.php +++ b/src/AdminUi/tests/Functional/TemplatesTest.php @@ -30,7 +30,7 @@ public function testBaseTemplate(): void $this->client->request('GET', '/base'); self::assertResponseIsSuccessful(); - self::assertSelectorTextContains('title', 'Sylius'); + self::assertSelectorTextContains('title', ''); } public function testIndexTemplate(): void diff --git a/src/BootstrapAdminUi/config/app/twig_hooks/common/create.php b/src/BootstrapAdminUi/config/app/twig_hooks/common/create.php index 3d24920d..4ae3262b 100644 --- a/src/BootstrapAdminUi/config/app/twig_hooks/common/create.php +++ b/src/BootstrapAdminUi/config/app/twig_hooks/common/create.php @@ -16,6 +16,21 @@ return static function (ContainerConfigurator $container): void { $container->extension('sylius_twig_hooks', [ 'hooks' => [ + 'sylius_admin.common.create#title' => [ + 'current' => [ + 'template' => '@SyliusBootstrapAdminUi/shared/crud/common/title/current.html.twig', + 'priority' => 200, + ], + 'separator' => [ + 'template' => '@SyliusBootstrapAdminUi/shared/crud/common/title/separator.html.twig', + 'priority' => 100, + ], + 'parent' => [ + 'template' => '@SyliusBootstrapAdminUi/shared/crud/common/title/parent.html.twig', + 'priority' => 0, + ], + ], + 'sylius_admin.common.create' => [ 'sidebar' => [ 'template' => '@SyliusBootstrapAdminUi/shared/crud/common/sidebar.html.twig', diff --git a/src/BootstrapAdminUi/config/app/twig_hooks/common/index.php b/src/BootstrapAdminUi/config/app/twig_hooks/common/index.php index e612b0db..515b25bd 100644 --- a/src/BootstrapAdminUi/config/app/twig_hooks/common/index.php +++ b/src/BootstrapAdminUi/config/app/twig_hooks/common/index.php @@ -16,6 +16,21 @@ return static function (ContainerConfigurator $container): void { $container->extension('sylius_twig_hooks', [ 'hooks' => [ + 'sylius_admin.common.index#title' => [ + 'current' => [ + 'template' => '@SyliusBootstrapAdminUi/shared/crud/common/title/current.html.twig', + 'priority' => 200, + ], + 'separator' => [ + 'template' => '@SyliusBootstrapAdminUi/shared/crud/common/title/separator.html.twig', + 'priority' => 100, + ], + 'parent' => [ + 'template' => '@SyliusBootstrapAdminUi/shared/crud/common/title/parent.html.twig', + 'priority' => 0, + ], + ], + 'sylius_admin.common.index' => [ 'sidebar' => [ 'template' => '@SyliusBootstrapAdminUi/shared/crud/common/sidebar.html.twig', diff --git a/src/BootstrapAdminUi/config/app/twig_hooks/common/show.php b/src/BootstrapAdminUi/config/app/twig_hooks/common/show.php index 143d0b85..8f52883e 100644 --- a/src/BootstrapAdminUi/config/app/twig_hooks/common/show.php +++ b/src/BootstrapAdminUi/config/app/twig_hooks/common/show.php @@ -16,6 +16,21 @@ return static function (ContainerConfigurator $container): void { $container->extension('sylius_twig_hooks', [ 'hooks' => [ + 'sylius_admin.common.show#title' => [ + 'current' => [ + 'template' => '@SyliusBootstrapAdminUi/shared/crud/common/title/current.html.twig', + 'priority' => 200, + ], + 'separator' => [ + 'template' => '@SyliusBootstrapAdminUi/shared/crud/common/title/separator.html.twig', + 'priority' => 100, + ], + 'parent' => [ + 'template' => '@SyliusBootstrapAdminUi/shared/crud/common/title/parent.html.twig', + 'priority' => 0, + ], + ], + 'sylius_admin.common.show' => [ 'sidebar' => [ 'template' => '@SyliusBootstrapAdminUi/shared/crud/common/sidebar.html.twig', diff --git a/src/BootstrapAdminUi/config/app/twig_hooks/common/update.php b/src/BootstrapAdminUi/config/app/twig_hooks/common/update.php index deaa708a..5ea17868 100644 --- a/src/BootstrapAdminUi/config/app/twig_hooks/common/update.php +++ b/src/BootstrapAdminUi/config/app/twig_hooks/common/update.php @@ -16,6 +16,21 @@ return static function (ContainerConfigurator $container): void { $container->extension('sylius_twig_hooks', [ 'hooks' => [ + 'sylius_admin.common.update#title' => [ + 'current' => [ + 'template' => '@SyliusBootstrapAdminUi/shared/crud/common/title/current.html.twig', + 'priority' => 200, + ], + 'separator' => [ + 'template' => '@SyliusBootstrapAdminUi/shared/crud/common/title/separator.html.twig', + 'priority' => 100, + ], + 'parent' => [ + 'template' => '@SyliusBootstrapAdminUi/shared/crud/common/title/parent.html.twig', + 'priority' => 0, + ], + ], + 'sylius_admin.common.update' => [ 'sidebar' => [ 'template' => '@SyliusBootstrapAdminUi/shared/crud/common/sidebar.html.twig', diff --git a/src/BootstrapAdminUi/config/app/twig_hooks/layout.php b/src/BootstrapAdminUi/config/app/twig_hooks/layout.php index 2e7aa8ef..311b19f8 100644 --- a/src/BootstrapAdminUi/config/app/twig_hooks/layout.php +++ b/src/BootstrapAdminUi/config/app/twig_hooks/layout.php @@ -16,6 +16,11 @@ return static function (ContainerConfigurator $container): void { $container->extension('sylius_twig_hooks', [ 'hooks' => [ + 'sylius_admin.base#base_title' => [ + 'default' => [ + 'template' => '@SyliusBootstrapAdminUi/shared/layout/title.html.twig', + ], + ], 'sylius_admin.base#stylesheets' => [ 'styles' => [ 'template' => '@SyliusBootstrapAdminUi/shared/layout/stylesheets.html.twig', diff --git a/src/BootstrapAdminUi/templates/shared/crud/common/title/current.html.twig b/src/BootstrapAdminUi/templates/shared/crud/common/title/current.html.twig new file mode 100644 index 00000000..e86d52ce --- /dev/null +++ b/src/BootstrapAdminUi/templates/shared/crud/common/title/current.html.twig @@ -0,0 +1,4 @@ +{% set metadata = hookable_metadata.context.metadata %} +{% set header = hookable_metadata.configuration.header|default(metadata.applicationName ~ '.ui.' ~ metadata.pluralName) %} + +{{ header|trans }} diff --git a/src/BootstrapAdminUi/templates/shared/crud/common/title/parent.html.twig b/src/BootstrapAdminUi/templates/shared/crud/common/title/parent.html.twig new file mode 100644 index 00000000..2139f921 --- /dev/null +++ b/src/BootstrapAdminUi/templates/shared/crud/common/title/parent.html.twig @@ -0,0 +1,2 @@ +{% set parent = hookable_metadata.context.parent %} +{{ parent }} diff --git a/src/BootstrapAdminUi/templates/shared/crud/common/title/separator.html.twig b/src/BootstrapAdminUi/templates/shared/crud/common/title/separator.html.twig new file mode 100644 index 00000000..948cf947 --- /dev/null +++ b/src/BootstrapAdminUi/templates/shared/crud/common/title/separator.html.twig @@ -0,0 +1 @@ +| diff --git a/src/BootstrapAdminUi/templates/shared/layout/title.html.twig b/src/BootstrapAdminUi/templates/shared/layout/title.html.twig new file mode 100644 index 00000000..a84fd414 --- /dev/null +++ b/src/BootstrapAdminUi/templates/shared/layout/title.html.twig @@ -0,0 +1 @@ +Sylius diff --git a/src/BootstrapAdminUi/tests/Functional/TemplatesTest.php b/src/BootstrapAdminUi/tests/Functional/TemplatesTest.php index 2698dd55..c703b6f6 100644 --- a/src/BootstrapAdminUi/tests/Functional/TemplatesTest.php +++ b/src/BootstrapAdminUi/tests/Functional/TemplatesTest.php @@ -30,6 +30,7 @@ public function testIndexTemplate(): void $this->client->request('GET', '/books'); self::assertResponseIsSuccessful(); + self::assertSelectorTextContains('title', 'app.ui.books | Sylius'); self::assertSelectorTextContains('tr.item:first-child[data-test-resource-id]', 'Shinning'); self::assertSelectorTextContains('tr.item:last-child[data-test-resource-id]', 'Carrie'); }