Skip to content

Commit 8d38f85

Browse files
committedNov 23, 2024
feature #6536 Split ea_form_fieldset_open_row form theme block (WedgeSama)
This PR was merged into the 4.x branch. Discussion ---------- Split `ea_form_fieldset_open_row` form theme block Split `ea_form_fieldset_open_row` with its `_label` + create `_ealabel` for inner content. Add new form template blocks for fieldset to ease template customization. Replace `fieldset_title_contents` by dedicated block that can be overridden. ================= Its add a new twig function `ea_form_ealabel` to be used like any other form function (`form_row`, `form_widget`, `form_label`, etc...) but for specific EA form part. In this PR, I only add a split for `ea_form_fieldset_open_row`, if this PR is accepted, I will split more template parts. ================= Why did I suggest that? IMO, for 3 reasons: - It use more sf Form component rendering functions. - It will make `templates/crud/form_theme.html.twig` more readable. - Make `templates/crud/form_theme.html.twig` override easier and more update proof. ================= Usage example: ```php class MyController extends AbstractCrudController { //... public function configureFields(string $pageName): iterable { return [ FormField::addFieldset('My Title') ->setFormTypeOption('block_prefix', 'foobar') ]; } //... } ``` ```twig {% extends '`@EasyAdmin`/crud/form_theme.html.twig' %} {% block foobar_ealabel %} <span>Add something before native block</span> {{ block('ea_form_fieldset_open_ealabel') }} <span>Add something after native block</span> {% endblock foobar_ealabel %} ``` Commits ------- 626be05 Split `ea_form_fieldset_open_row` with its `_label` + create `_ealabel` for inner content.
2 parents 374d42a + 626be05 commit 8d38f85

File tree

3 files changed

+58
-31
lines changed

3 files changed

+58
-31
lines changed
 

‎config/services.php

+4
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,7 @@
8383
use EasyCorp\Bundle\EasyAdminBundle\Security\AuthorizationChecker;
8484
use EasyCorp\Bundle\EasyAdminBundle\Security\SecurityVoter;
8585
use EasyCorp\Bundle\EasyAdminBundle\Twig\EasyAdminTwigExtension;
86+
use EasyCorp\Bundle\EasyAdminBundle\Twig\FormExtension;
8687
use Symfony\Component\DependencyInjection\ContainerInterface;
8788
use Symfony\Component\DependencyInjection\Reference;
8889
use Symfony\Component\DependencyInjection\ServiceLocator;
@@ -136,6 +137,9 @@
136137
->arg(4, service('translator'))
137138
->tag('twig.extension')
138139

140+
->set(FormExtension::class)
141+
->tag('twig.extension')
142+
139143
->set(EaCrudFormTypeExtension::class)
140144
->arg(0, service(AdminContextProvider::class))
141145
->tag('form.type_extension')

‎src/Twig/FormExtension.php

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<?php
2+
3+
namespace EasyCorp\Bundle\EasyAdminBundle\Twig;
4+
5+
use Twig\Extension\AbstractExtension;
6+
use Twig\TwigFunction;
7+
8+
/**
9+
* @author Benjamin Georgeault <git@wedgesama.fr>
10+
*/
11+
class FormExtension extends AbstractExtension
12+
{
13+
public function getFunctions(): array
14+
{
15+
return [
16+
new TwigFunction('ea_form_ealabel', null, ['node_class' => 'Symfony\Bridge\Twig\Node\SearchAndRenderBlockNode', 'is_safe' => ['html']]),
17+
];
18+
}
19+
}

‎templates/crud/form_theme.html.twig

+35-31
Original file line numberDiff line numberDiff line change
@@ -581,43 +581,47 @@
581581
</div>
582582
{% endblock ea_form_column_close_row %}
583583

584+
{% block ea_form_fieldset_open_ealabel %}
585+
{% if ea_is_collapsible %}
586+
<i class="fas fw fa-chevron-right form-fieldset-collapse-marker"></i>
587+
{% endif %}
588+
589+
{% if ea_icon %}
590+
<i class="form-fieldset-icon {{ ea_icon }}"></i>
591+
{% endif %}
592+
593+
{{ form.vars.label|trans|raw }}
594+
{% endblock ea_form_fieldset_open_ealabel %}
595+
596+
{% block ea_form_fieldset_open_label %}
597+
<div class="form-fieldset-header {{ ea_is_collapsible ? 'collapsible' }} {{ ea_help is not empty ? 'with-help' }}">
598+
<div class="form-fieldset-title">
599+
{% if ea_is_collapsible %}
600+
<a href="#content-{{ form.vars.name }}" data-bs-toggle="collapse"
601+
class="form-fieldset-title-content form-fieldset-collapse {{ ea_is_collapsed ? 'collapsed' }}"
602+
aria-expanded="{{ ea_is_collapsed ? 'false' : 'true' }}" aria-controls="content-{{ form.vars.name }}">
603+
{{ ea_form_ealabel(form) }}
604+
</a>
605+
{% else %}
606+
<span class="not-collapsible form-fieldset-title-content">
607+
{{ ea_form_ealabel(form) }}
608+
</span>
609+
{% endif %}
610+
611+
{% if ea_help %}
612+
<div class="form-fieldset-help">{{ ea_help|trans|raw }}</div>
613+
{% endif %}
614+
</div>
615+
</div>
616+
{% endblock ea_form_fieldset_open_label %}
617+
584618
{% block ea_form_fieldset_open_row %}
585619
{% set fieldset_has_header = form.vars.label or ea_icon or ea_help %}
586620

587621
<div class="form-fieldset {{ not fieldset_has_header ? 'form-fieldset-no-header' }} {{ ea_css_class }}">
588622
<fieldset>
589623
{% if fieldset_has_header %}
590-
<div class="form-fieldset-header {{ ea_is_collapsible ? 'collapsible' }} {{ ea_help is not empty ? 'with-help' }}">
591-
<div class="form-fieldset-title">
592-
{% set fieldset_title_contents %}
593-
{% if ea_is_collapsible %}
594-
<i class="fas fw fa-chevron-right form-fieldset-collapse-marker"></i>
595-
{% endif %}
596-
597-
{% if ea_icon %}
598-
<i class="form-fieldset-icon {{ ea_icon }}"></i>
599-
{% endif %}
600-
601-
{{ form.vars.label|trans|raw }}
602-
{% endset %}
603-
604-
{% if ea_is_collapsible %}
605-
<a href="#content-{{ form.vars.name }}" data-bs-toggle="collapse"
606-
class="form-fieldset-title-content form-fieldset-collapse {{ ea_is_collapsed ? 'collapsed' }}"
607-
aria-expanded="{{ ea_is_collapsed ? 'false' : 'true' }}" aria-controls="content-{{ form.vars.name }}">
608-
{{ fieldset_title_contents|raw }}
609-
</a>
610-
{% else %}
611-
<span class="not-collapsible form-fieldset-title-content">
612-
{{ fieldset_title_contents|raw }}
613-
</span>
614-
{% endif %}
615-
616-
{% if ea_help %}
617-
<div class="form-fieldset-help">{{ ea_help|trans|raw }}</div>
618-
{% endif %}
619-
</div>
620-
</div>
624+
{{ form_label(form) }}
621625
{% endif %}
622626

623627
<div id="content-{{ form.vars.name }}" class="form-fieldset-body {{ not fieldset_has_header ? 'without-header' }} {{ ea_is_collapsible ? 'collapse' }} {{ not ea_is_collapsed ? 'show'}}">

0 commit comments

Comments
 (0)
Please sign in to comment.