Skip to content

Commit

Permalink
Allow using custom action templates. (#197)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
SelrahcD authored Jun 18, 2021
1 parent 71ee6aa commit 0855d9e
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 20 deletions.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -422,6 +422,9 @@ easyadmin:
- { name: disable, icon: ban, title: Disable user, label: false, target: _blank, confirm: User will lose any access to the platform ! }
```

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') }}
` by ` {{ include('@EasyAdminExtension/default/action.html.twig') }}`.

### Exclude fields in forms

```yaml
Expand Down
6 changes: 6 additions & 0 deletions src/Resources/views/default/action.html.twig
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<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 %}>
{%- if action.icon %}<i class="fa fa-{{ action.icon }}"></i> {% endif -%}
{%- if action.label is defined and not action.label is empty -%}
{{ action.label|trans(trans_parameters|merge({ '%entity_id%': item_id }), translation_domain) }}
{%- endif -%}
</a>
20 changes: 13 additions & 7 deletions src/Resources/views/default/embedded_list.html.twig
Original file line number Diff line number Diff line change
Expand Up @@ -69,13 +69,19 @@
{% set _column_label = 'list.row_actions'|trans(_trans_parameters, 'EasyAdminBundle') %}
<td class="actions">
{% block item_actions %}
{{ include('@EasyAdmin/default/includes/_actions.html.twig', {
actions: _list_item_actions|prune_item_actions(_entity_config, ['delete'], item),
request_parameters: _request_parameters,
translation_domain: _entity_config.translation_domain,
trans_parameters: _trans_parameters,
item_id: _item_id
}, with_context = false) }}
{% set _actions_template = _entity_config.list.collapse_actions
? '@EasyAdmin/default/includes/_actions_dropdown.html.twig'
: '@EasyAdmin/default/includes/_actions.html.twig'
%}
{{ include(_actions_template, {
actions: _list_item_actions|prune_item_actions(_entity_config, ['delete'], item),
entity_config: _entity_config,
request_parameters: _request_parameters,
translation_domain: _entity_config.translation_domain,
trans_parameters: _trans_parameters,
item_id: _item_id,
item: item
}, with_context = false) }}
{% endblock item_actions %}
</td>
{% endif %}
Expand Down
25 changes: 13 additions & 12 deletions src/Resources/views/default/includes/_actions.html.twig
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
{% import _self as action_macros %}

{% for action in actions %}
{% if 'list' == action.name %}
{% set action_href = request_parameters.referer|default('') ? request_parameters.referer|easyadmin_urldecode : path('easyadmin', request_parameters|merge({ action: 'list' })) %}
Expand All @@ -14,14 +12,17 @@
{% set confirm = (action.confirm|trans({}, translation_domain))|default('confirm_modal.content'|trans({}, 'EasyAdminBundle')) %}
{% endif %}

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

{% macro action_tpl(action, item_id, action_href, confirm, trans_parameters, translation_domain) %}
<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 %}>
{%- if action.icon %}<i class="fa fa-{{ action.icon }}"></i> {% endif -%}
{%- if action.label is defined and not action.label is empty -%}
{{ action.label|trans(trans_parameters|merge({ '%entity_id%': item_id }), translation_domain) }}
{%- endif -%}
</a>
{% endmacro %}
{{ include(action.template, {
action: action,
action_href: action_href,
is_dropdown: is_dropdown|default(false),
item: item,
item_id: item_id,
request_parameters: request_parameters,
translation_domain: translation_domain,
trans_parameters: trans_parameters,
confirm: confirm
}, with_context = false) }}

{% endfor %}
3 changes: 2 additions & 1 deletion src/Resources/views/form/bootstrap_4.html.twig
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
request_parameters: _request_parameters,
translation_domain: _translation_domain,
trans_parameters: _trans_parameters,
item_id: _entity_id
item_id: _entity_id,
item: easyadmin.entity
}, with_context = false) }}
{% endblock item_actions %}

0 comments on commit 0855d9e

Please sign in to comment.