Skip to content

Commit

Permalink
feature #6536 Split ea_form_fieldset_open_row form theme block (Wed…
Browse files Browse the repository at this point in the history
…geSama)

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.
  • Loading branch information
javiereguiluz committed Nov 23, 2024
2 parents 374d42a + 626be05 commit 8d38f85
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 31 deletions.
4 changes: 4 additions & 0 deletions config/services.php
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@
use EasyCorp\Bundle\EasyAdminBundle\Security\AuthorizationChecker;
use EasyCorp\Bundle\EasyAdminBundle\Security\SecurityVoter;
use EasyCorp\Bundle\EasyAdminBundle\Twig\EasyAdminTwigExtension;
use EasyCorp\Bundle\EasyAdminBundle\Twig\FormExtension;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\DependencyInjection\Reference;
use Symfony\Component\DependencyInjection\ServiceLocator;
Expand Down Expand Up @@ -136,6 +137,9 @@
->arg(4, service('translator'))
->tag('twig.extension')

->set(FormExtension::class)
->tag('twig.extension')

->set(EaCrudFormTypeExtension::class)
->arg(0, service(AdminContextProvider::class))
->tag('form.type_extension')
Expand Down
19 changes: 19 additions & 0 deletions src/Twig/FormExtension.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

namespace EasyCorp\Bundle\EasyAdminBundle\Twig;

use Twig\Extension\AbstractExtension;
use Twig\TwigFunction;

/**
* @author Benjamin Georgeault <git@wedgesama.fr>
*/
class FormExtension extends AbstractExtension
{
public function getFunctions(): array
{
return [
new TwigFunction('ea_form_ealabel', null, ['node_class' => 'Symfony\Bridge\Twig\Node\SearchAndRenderBlockNode', 'is_safe' => ['html']]),
];
}
}
66 changes: 35 additions & 31 deletions templates/crud/form_theme.html.twig
Original file line number Diff line number Diff line change
Expand Up @@ -581,43 +581,47 @@
</div>
{% endblock ea_form_column_close_row %}

{% block ea_form_fieldset_open_ealabel %}
{% if ea_is_collapsible %}
<i class="fas fw fa-chevron-right form-fieldset-collapse-marker"></i>
{% endif %}

{% if ea_icon %}
<i class="form-fieldset-icon {{ ea_icon }}"></i>
{% endif %}

{{ form.vars.label|trans|raw }}
{% endblock ea_form_fieldset_open_ealabel %}

{% block ea_form_fieldset_open_label %}
<div class="form-fieldset-header {{ ea_is_collapsible ? 'collapsible' }} {{ ea_help is not empty ? 'with-help' }}">
<div class="form-fieldset-title">
{% if ea_is_collapsible %}
<a href="#content-{{ form.vars.name }}" data-bs-toggle="collapse"
class="form-fieldset-title-content form-fieldset-collapse {{ ea_is_collapsed ? 'collapsed' }}"
aria-expanded="{{ ea_is_collapsed ? 'false' : 'true' }}" aria-controls="content-{{ form.vars.name }}">
{{ ea_form_ealabel(form) }}
</a>
{% else %}
<span class="not-collapsible form-fieldset-title-content">
{{ ea_form_ealabel(form) }}
</span>
{% endif %}

{% if ea_help %}
<div class="form-fieldset-help">{{ ea_help|trans|raw }}</div>
{% endif %}
</div>
</div>
{% endblock ea_form_fieldset_open_label %}

{% block ea_form_fieldset_open_row %}
{% set fieldset_has_header = form.vars.label or ea_icon or ea_help %}

<div class="form-fieldset {{ not fieldset_has_header ? 'form-fieldset-no-header' }} {{ ea_css_class }}">
<fieldset>
{% if fieldset_has_header %}
<div class="form-fieldset-header {{ ea_is_collapsible ? 'collapsible' }} {{ ea_help is not empty ? 'with-help' }}">
<div class="form-fieldset-title">
{% set fieldset_title_contents %}
{% if ea_is_collapsible %}
<i class="fas fw fa-chevron-right form-fieldset-collapse-marker"></i>
{% endif %}

{% if ea_icon %}
<i class="form-fieldset-icon {{ ea_icon }}"></i>
{% endif %}

{{ form.vars.label|trans|raw }}
{% endset %}

{% if ea_is_collapsible %}
<a href="#content-{{ form.vars.name }}" data-bs-toggle="collapse"
class="form-fieldset-title-content form-fieldset-collapse {{ ea_is_collapsed ? 'collapsed' }}"
aria-expanded="{{ ea_is_collapsed ? 'false' : 'true' }}" aria-controls="content-{{ form.vars.name }}">
{{ fieldset_title_contents|raw }}
</a>
{% else %}
<span class="not-collapsible form-fieldset-title-content">
{{ fieldset_title_contents|raw }}
</span>
{% endif %}

{% if ea_help %}
<div class="form-fieldset-help">{{ ea_help|trans|raw }}</div>
{% endif %}
</div>
</div>
{{ form_label(form) }}
{% endif %}

<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'}}">
Expand Down

0 comments on commit 8d38f85

Please sign in to comment.