Skip to content

Commit

Permalink
[AdminUi][BootstrapAdminUi] Managing page title via Twig hooks (#205)
Browse files Browse the repository at this point in the history
  • Loading branch information
loic425 authored Jan 7, 2025
2 parents e1d2e01 + ce45034 commit 06e086f
Show file tree
Hide file tree
Showing 16 changed files with 103 additions and 16 deletions.
7 changes: 4 additions & 3 deletions src/AdminUi/templates/base.html.twig
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
{% set generic_hook = 'sylius_admin.base' %}
{% set prefixes = prefixes|default([])|merge([generic_hook]) %}

<!DOCTYPE html>
<html lang="{{ app.request.locale }}">
Expand All @@ -7,11 +8,11 @@
<meta name="viewport" content="width=device-width, initial-scale=1.0"/>
<meta http-equiv="X-UA-Compatible" content="IE=edge">

<title>{% block title %}Sylius{% endblock %}</title>
<title>{% block title %}{% apply striptags %}{% hook '#base_title' with { _prefixes: prefixes } %}{% endapply %}{% endblock %}</title>

{% block metatags %}{% endblock %}
{% block stylesheets %}
{% hook generic_hook ~ '#stylesheets' %}
{% hook '#stylesheets' with { _prefixes: prefixes } %}
{% endblock %}
</head>
<body class="{% block body_class %}{% endblock %}">
Expand All @@ -20,7 +21,7 @@
</div>

{% block javascripts %}
{% hook generic_hook ~ '#javascripts' %}
{% hook '#javascripts' with { _prefixes: prefixes } %}
{% endblock %}
</body>
</html>
9 changes: 6 additions & 3 deletions src/AdminUi/templates/crud/create.html.twig
Original file line number Diff line number Diff line change
Expand Up @@ -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 } %}
Expand Down
9 changes: 6 additions & 3 deletions src/AdminUi/templates/crud/index.html.twig
Original file line number Diff line number Diff line change
Expand Up @@ -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 } %}
Expand Down
9 changes: 6 additions & 3 deletions src/AdminUi/templates/crud/show.html.twig
Original file line number Diff line number Diff line change
Expand Up @@ -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 } %}
Expand Down
9 changes: 6 additions & 3 deletions src/AdminUi/templates/crud/update.html.twig
Original file line number Diff line number Diff line change
Expand Up @@ -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 } %}
Expand Down
2 changes: 1 addition & 1 deletion src/AdminUi/tests/Functional/TemplatesTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
15 changes: 15 additions & 0 deletions src/BootstrapAdminUi/config/app/twig_hooks/common/create.php
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down
15 changes: 15 additions & 0 deletions src/BootstrapAdminUi/config/app/twig_hooks/common/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down
15 changes: 15 additions & 0 deletions src/BootstrapAdminUi/config/app/twig_hooks/common/show.php
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down
15 changes: 15 additions & 0 deletions src/BootstrapAdminUi/config/app/twig_hooks/common/update.php
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down
5 changes: 5 additions & 0 deletions src/BootstrapAdminUi/config/app/twig_hooks/layout.php
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{% set metadata = hookable_metadata.context.metadata %}
{% set header = hookable_metadata.configuration.header|default(metadata.applicationName ~ '.ui.' ~ metadata.pluralName) %}

{{ header|trans }}
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
{% set parent = hookable_metadata.context.parent %}
{{ parent }}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
|
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Sylius
1 change: 1 addition & 0 deletions src/BootstrapAdminUi/tests/Functional/TemplatesTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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');
}
Expand Down

0 comments on commit 06e086f

Please sign in to comment.