From 52881bcad2d8228ceeb93ced21d7ac5ad76ef648 Mon Sep 17 00:00:00 2001 From: ksn135 Date: Mon, 6 Feb 2023 12:39:33 +0300 Subject: [PATCH 1/5] [FEATRUE] add full width and twig context options to FieldDTO --- doc/fields.rst | 6 ++++++ src/Dto/FieldDto.php | 26 +++++++++++++++++++++++ src/Resources/views/crud/detail.html.twig | 6 +++++- 3 files changed, 37 insertions(+), 1 deletion(-) diff --git a/doc/fields.rst b/doc/fields.rst index d46e60fc19..4e55544114 100644 --- a/doc/fields.rst +++ b/doc/fields.rst @@ -556,6 +556,12 @@ Design Options // (this is not used in the 'edit'/'new' pages because they use Symfony Forms themes) ->setTemplatePath('admin/fields/my_template.html.twig') + // if you need to access custom twig variables setted via call to CrudController::configureResponseParameters + ->setWithTwigContext(true) + + // Display field as full-width, remove completly dt,dd tags and label + ->setFullWidth(true) + // useful for example to right-align numbers/money values (this setting is ignored in 'detail' page) ->setTextAlign('right') ; diff --git a/src/Dto/FieldDto.php b/src/Dto/FieldDto.php index 30d3df794a..996ab2816f 100644 --- a/src/Dto/FieldDto.php +++ b/src/Dto/FieldDto.php @@ -26,6 +26,8 @@ final class FieldDto private ?string $permission = null; private ?string $textAlign = null; private $help; + private bool $fullWidth = false; + private bool $withTwigContext = false; private string $cssClass = ''; // how many columns the field takes when rendering // (defined as Bootstrap 5 grid classes; e.g. 'col-md-6 col-xxl-3') @@ -262,6 +264,30 @@ public function setHelp(TranslatableInterface|string $help): void $this->help = $help; } + public function setFullWidth(bool $fullWidth = true): self + { + $this->fullWidth = $fullWidth; + + return $this; + } + + public function getFullWidth(): bool + { + return $this->fullWidth; + } + + public function setWithTwigContext(bool $withTwigContext = true): self + { + $this->withTwigContext = $withTwigContext; + + return $this; + } + + public function getWithTwigContext(): bool + { + return $this->withTwigContext; + } + public function getCssClass(): string { return $this->cssClass; diff --git a/src/Resources/views/crud/detail.html.twig b/src/Resources/views/crud/detail.html.twig index 08ad36662c..f224c059f6 100644 --- a/src/Resources/views/crud/detail.html.twig +++ b/src/Resources/views/crud/detail.html.twig @@ -178,6 +178,7 @@ {% macro render_field(entity, field) %}
+ {% if not field.fullWidth %}
{{ field.label|trans|raw }} @@ -188,7 +189,10 @@ {% endif %}
- {{ include(field.templatePath, { field: field, entity: entity }, with_context = false) }} + {% endif %} + {{ include(field.templatePath, { field: field, entity: entity }, with_context = field.withTwigContext) }} + {% if not field.fullWidth %}
+ {% endif %}
{% endmacro %} From 27739d3523372e66ee912db54c3e7dc25d28ced4 Mon Sep 17 00:00:00 2001 From: ksn135 Date: Mon, 6 Feb 2023 13:27:19 +0300 Subject: [PATCH 2/5] update FieldTrait --- src/Field/FieldTrait.php | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/src/Field/FieldTrait.php b/src/Field/FieldTrait.php index 86cd7e2008..69b2ff202d 100644 --- a/src/Field/FieldTrait.php +++ b/src/Field/FieldTrait.php @@ -165,6 +165,20 @@ public function setHelp(TranslatableInterface|string $help): self return $this; } + public function setFullWidth(bool $fullWidth = true): self + { + $this->dto->fullWidth = $fullWidth; + + return $this; + } + + public function setWithTwigContext(bool $withTwigContext = true): self + { + $this->dto->withTwigContext = $withTwigContext; + + return $this; + } + public function addCssClass(string $cssClass): self { $this->dto->setCssClass($this->dto->getCssClass().' '.$cssClass); From 8d1eaf0a99b8a2264354114b07e4ef5d14beda70 Mon Sep 17 00:00:00 2001 From: ksn135 Date: Mon, 6 Feb 2023 13:30:18 +0300 Subject: [PATCH 3/5] fix FieldTrait --- src/Field/FieldTrait.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Field/FieldTrait.php b/src/Field/FieldTrait.php index 69b2ff202d..12688e1a07 100644 --- a/src/Field/FieldTrait.php +++ b/src/Field/FieldTrait.php @@ -167,14 +167,14 @@ public function setHelp(TranslatableInterface|string $help): self public function setFullWidth(bool $fullWidth = true): self { - $this->dto->fullWidth = $fullWidth; + $this->dto->setFullWidth($fullWidth); return $this; } public function setWithTwigContext(bool $withTwigContext = true): self { - $this->dto->withTwigContext = $withTwigContext; + $this->dto->setWithTwigContext($withTwigContext); return $this; } From a05d56630a30f9ae6a52593eeda0d85cace4e255 Mon Sep 17 00:00:00 2001 From: ksn135 Date: Mon, 6 Feb 2023 13:44:57 +0300 Subject: [PATCH 4/5] add row class --- src/Resources/views/crud/detail.html.twig | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Resources/views/crud/detail.html.twig b/src/Resources/views/crud/detail.html.twig index f224c059f6..7c26c05a89 100644 --- a/src/Resources/views/crud/detail.html.twig +++ b/src/Resources/views/crud/detail.html.twig @@ -177,7 +177,7 @@ {% endmacro %} {% macro render_field(entity, field) %} -
+
{% if not field.fullWidth %}
{{ field.label|trans|raw }} From 6a2475e2a687eb0814ce16a8e33e57c4738208b2 Mon Sep 17 00:00:00 2001 From: ksn135 Date: Mon, 6 Feb 2023 14:02:56 +0300 Subject: [PATCH 5/5] fix withTwigContext --- src/Resources/views/crud/detail.html.twig | 18 ++++++++++-------- 1 file changed, 10 insertions(+), 8 deletions(-) diff --git a/src/Resources/views/crud/detail.html.twig b/src/Resources/views/crud/detail.html.twig index 7c26c05a89..fc3d8fd491 100644 --- a/src/Resources/views/crud/detail.html.twig +++ b/src/Resources/views/crud/detail.html.twig @@ -54,9 +54,9 @@ {% set field_layout = ea_create_field_layout(entity.fields) %} {% block detail_fields %} {% if field_layout.hasTabs %} - {{ _self.render_detail_fields_with_tabs(entity, field_layout) }} + {{ _self.render_detail_fields_with_tabs(entity, field_layout, _context) }} {% else %} - {{ _self.render_detail_fields(entity, field_layout.fields) }} + {{ _self.render_detail_fields(entity, field_layout.fields, _context) }} {% endif %} {% endblock detail_fields %} @@ -65,7 +65,7 @@ {% endblock delete_form %} {% endblock %} -{% macro render_detail_fields_with_tabs(entity, field_layout) %} +{% macro render_detail_fields_with_tabs(entity, field_layout, variables) %}
{% endif %}
- {{ _self.render_detail_fields(entity, field_layout.fieldsInTab(tab.uniqueId)) }} + {{ _self.render_detail_fields(entity, field_layout.fieldsInTab(tab.uniqueId), variables) }}
{% endfor %} @@ -98,7 +98,7 @@
{% endmacro %} -{% macro render_detail_fields(entity, fields) %} +{% macro render_detail_fields(entity, fields, variables) %} {% set form_panel_is_already_open = false %} {% for field in fields %} {% set is_form_field_panel = 'field-form_panel' in field.cssClass %} @@ -115,7 +115,7 @@ {% block detail_field %} {% if not is_form_field_panel %} - {{ _self.render_field(entity, field) }} + {{ _self.render_field(entity, field, variables) }} {% endif %} {% endblock %} {% endfor %} @@ -176,7 +176,7 @@
{% endmacro %} -{% macro render_field(entity, field) %} +{% macro render_field(entity, field, variables) %}
{% if not field.fullWidth %}
@@ -190,7 +190,9 @@
{% endif %} - {{ include(field.templatePath, { field: field, entity: entity }, with_context = field.withTwigContext) }} + {% set twigVars = { field: field, entity: entity } %} + {% if field.withTwigContext %}{% set twigVars = twigVars|merge({variables: variables}) %}{% endif %} + {{ include(field.templatePath, twigVars, with_context = false) }} {% if not field.fullWidth %}
{% endif %}