Skip to content

Commit 0855d9e

Browse files
authored
Allow using custom action templates. (#197)
Before this change, using [custom template for actions](https://symfony.com/doc/2.x/bundles/EasyAdminBundle/tutorials/custom-actions.html#custom-templates-for-actions) wasn't possible, as well as [collapsing actions](https://symfony.com/doc/2.x/bundles/EasyAdminBundle/book/configuration-reference.html#collapse-actions). This PR makes it possible while keeping the [confirmation modal](https://symfony.com/doc/2.x/bundles/EasyAdminBundle/book/configuration-reference.html#collapse-actions). It introduces a new `action.html.twig` template, which keeps the confirmation behavior. This is the new template to use when creating a custom template. This PR also changes the `_actions_template` template selection, giving us the ability to use the collapsed version of actions. Side note: the new action template differs slightly from the original one in the way it deals with translation and doesn't use the same CSS classes for the action icon. This was left unchanged.
1 parent 71ee6aa commit 0855d9e

5 files changed

Lines changed: 37 additions & 20 deletions

File tree

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -422,6 +422,9 @@ easyadmin:
422422
- { name: disable, icon: ban, title: Disable user, label: false, target: _blank, confirm: User will lose any access to the platform ! }
423423
```
424424

425+
To keep the confirmation modal behavior while creating [a custom action template](https://symfony.com/doc/2.x/bundles/EasyAdminBundle/tutorials/custom-actions.html#custom-templates-for-actions) you need to use the action template provided by this bundle, replacing ` {{ include('@EasyAdmin/default/action.html.twig') }}
426+
` by ` {{ include('@EasyAdminExtension/default/action.html.twig') }}`.
427+
425428
### Exclude fields in forms
426429

427430
```yaml
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
<a name="{{ action.name }}" class="{{ action.css_class|default('') }}" title="{{ action.title|default('') is empty ? '' : action.title|trans(trans_parameters, translation_domain) }}" {% if not confirm %}href="{{ action_href }}" target="{{ action.target }}"{% else %}href="#" data-href="{{ action_href }}" data-confirm="{{ confirm }}"{% endif %}>
2+
{%- if action.icon %}<i class="fa fa-{{ action.icon }}"></i> {% endif -%}
3+
{%- if action.label is defined and not action.label is empty -%}
4+
{{ action.label|trans(trans_parameters|merge({ '%entity_id%': item_id }), translation_domain) }}
5+
{%- endif -%}
6+
</a>

src/Resources/views/default/embedded_list.html.twig

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -69,13 +69,19 @@
6969
{% set _column_label = 'list.row_actions'|trans(_trans_parameters, 'EasyAdminBundle') %}
7070
<td class="actions">
7171
{% block item_actions %}
72-
{{ include('@EasyAdmin/default/includes/_actions.html.twig', {
73-
actions: _list_item_actions|prune_item_actions(_entity_config, ['delete'], item),
74-
request_parameters: _request_parameters,
75-
translation_domain: _entity_config.translation_domain,
76-
trans_parameters: _trans_parameters,
77-
item_id: _item_id
78-
}, with_context = false) }}
72+
{% set _actions_template = _entity_config.list.collapse_actions
73+
? '@EasyAdmin/default/includes/_actions_dropdown.html.twig'
74+
: '@EasyAdmin/default/includes/_actions.html.twig'
75+
%}
76+
{{ include(_actions_template, {
77+
actions: _list_item_actions|prune_item_actions(_entity_config, ['delete'], item),
78+
entity_config: _entity_config,
79+
request_parameters: _request_parameters,
80+
translation_domain: _entity_config.translation_domain,
81+
trans_parameters: _trans_parameters,
82+
item_id: _item_id,
83+
item: item
84+
}, with_context = false) }}
7985
{% endblock item_actions %}
8086
</td>
8187
{% endif %}
Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
{% import _self as action_macros %}
2-
31
{% for action in actions %}
42
{% if 'list' == action.name %}
53
{% set action_href = request_parameters.referer|default('') ? request_parameters.referer|easyadmin_urldecode : path('easyadmin', request_parameters|merge({ action: 'list' })) %}
@@ -14,14 +12,17 @@
1412
{% set confirm = (action.confirm|trans({}, translation_domain))|default('confirm_modal.content'|trans({}, 'EasyAdminBundle')) %}
1513
{% endif %}
1614

17-
{{ action_macros.action_tpl(action, item_id, action_href, confirm, trans_parameters, translation_domain) }}
18-
{% endfor %}
1915

20-
{% macro action_tpl(action, item_id, action_href, confirm, trans_parameters, translation_domain) %}
21-
<a name="{{ action.name }}" class="{{ action.css_class|default('') }}" title="{{ action.title|default('') is empty ? '' : action.title|trans(trans_parameters, translation_domain) }}" {% if not confirm %}href="{{ action_href }}" target="{{ action.target }}"{% else %}href="#" data-href="{{ action_href }}" data-confirm="{{ confirm }}"{% endif %}>
22-
{%- if action.icon %}<i class="fa fa-{{ action.icon }}"></i> {% endif -%}
23-
{%- if action.label is defined and not action.label is empty -%}
24-
{{ action.label|trans(trans_parameters|merge({ '%entity_id%': item_id }), translation_domain) }}
25-
{%- endif -%}
26-
</a>
27-
{% endmacro %}
16+
{{ include(action.template, {
17+
action: action,
18+
action_href: action_href,
19+
is_dropdown: is_dropdown|default(false),
20+
item: item,
21+
item_id: item_id,
22+
request_parameters: request_parameters,
23+
translation_domain: translation_domain,
24+
trans_parameters: trans_parameters,
25+
confirm: confirm
26+
}, with_context = false) }}
27+
28+
{% endfor %}

src/Resources/views/form/bootstrap_4.html.twig

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@
4444
request_parameters: _request_parameters,
4545
translation_domain: _translation_domain,
4646
trans_parameters: _trans_parameters,
47-
item_id: _entity_id
47+
item_id: _entity_id,
48+
item: easyadmin.entity
4849
}, with_context = false) }}
4950
{% endblock item_actions %}

0 commit comments

Comments
 (0)