Skip to content

Commit 4a1afa9

Browse files
committed
Deprecate the global ea variable in favor of a Twig function
1 parent 2ff5269 commit 4a1afa9

24 files changed

+228
-26
lines changed

UPGRADE.md

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,19 @@
11
Upgrade between EasyAdmin 4.x versions
22
======================================
33

4+
EasyAdmin 4.25.0
5+
----------------
6+
7+
The global `ea` variable injected in all templates is deprecated.
8+
Use the equivalent `ea()` Twig function, which returns the current context
9+
of the EasyAdmin application.
10+
11+
// Before
12+
{{ ea.i18n.translationDomain }}
13+
14+
// After
15+
{{ ea().i18n.translationDomain }}
16+
417
EasyAdmin 4.22.0
518
----------------
619

src/Contracts/Provider/AdminContextProviderInterface.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,5 +13,6 @@ interface AdminContextProviderInterface extends AdminContextInterface
1313
{
1414
public function hasContext(): bool;
1515

16+
// the $throw parameter is deprecated and will be removed in 5.0
1617
public function getContext(bool $throw = false): ?AdminContextInterface;
1718
}

src/Provider/AdminContextProvider.php

Lines changed: 178 additions & 7 deletions
Large diffs are not rendered by default.

src/Twig/EasyAdminTwigExtension.php

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace EasyCorp\Bundle\EasyAdminBundle\Twig;
44

55
use EasyCorp\Bundle\EasyAdminBundle\Collection\FieldCollection;
6+
use EasyCorp\Bundle\EasyAdminBundle\Contracts\Context\AdminContextInterface;
67
use EasyCorp\Bundle\EasyAdminBundle\Contracts\Provider\AdminContextProviderInterface;
78
use EasyCorp\Bundle\EasyAdminBundle\Dto\FieldLayoutDto;
89
use EasyCorp\Bundle\EasyAdminBundle\Factory\FormLayoutFactory;
@@ -45,6 +46,7 @@ public function __construct(
4546
public function getFunctions(): array
4647
{
4748
return [
49+
new TwigFunction('ea', [$this, 'ea']),
4850
new TwigFunction('ea_url', [$this, 'getAdminUrlGenerator']),
4951
new TwigFunction('ea_form_ealabel', null, ['node_class' => 'Symfony\Bridge\Twig\Node\SearchAndRenderBlockNode', 'is_safe' => ['html']]),
5052
// deprecated functions
@@ -69,10 +71,14 @@ public function getFilters(): array
6971

7072
public function getGlobals(): array
7173
{
72-
// this is needed to make the admin context available on any Twig template via the short named variable 'ea'
7374
return ['ea' => $this->adminContextProvider];
7475
}
7576

77+
public function ea(): ?AdminContextInterface
78+
{
79+
return $this->adminContextProvider->getContext();
80+
}
81+
7682
/**
7783
* Transforms ['a' => 'foo', 'b' => ['c' => ['d' => 7]]] into ['a' => 'foo', 'b[c][d]' => 7]
7884
* It's useful to submit nested arrays (e.g. query string parameters) as form fields.

templates/crud/detail.html.twig

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
{# @var ea \EasyCorp\Bundle\EasyAdminBundle\Context\AdminContext #}
22
{# @var entity \EasyCorp\Bundle\EasyAdminBundle\Dto\EntityDto #}
3+
{% set ea = ea() %}
34
{% extends ea.templatePath('layout') %}
45

56
{% block body_id 'ea-detail-' ~ entity.name ~ '-' ~ entity.primaryKeyValue %}

templates/crud/edit.html.twig

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
{# @var ea \EasyCorp\Bundle\EasyAdminBundle\Context\AdminContext #}
22
{# @var entity \EasyCorp\Bundle\EasyAdminBundle\Dto\EntityDto #}
3+
{% set ea = ea() %}
34
{% extends ea.templatePath('layout') %}
45
{% form_theme edit_form with ea.crud.formThemes only %}
56

templates/crud/field/array.html.twig

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{# @var ea \EasyCorp\Bundle\EasyAdminBundle\Context\AdminContext #}
22
{# @var field \EasyCorp\Bundle\EasyAdminBundle\Dto\FieldDto #}
33
{# @var entity \EasyCorp\Bundle\EasyAdminBundle\Dto\EntityDto #}
4-
{% if ea.crud.currentAction == 'detail' %}
4+
{% if ea().crud.currentAction == 'detail' %}
55
<ul>
66
{% for item in field.value %}
77
<li>{{ item }}</li>

templates/crud/field/boolean.html.twig

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
{# @var ea \EasyCorp\Bundle\EasyAdminBundle\Context\AdminContext #}
22
{# @var field \EasyCorp\Bundle\EasyAdminBundle\Dto\FieldDto #}
33
{# @var entity \EasyCorp\Bundle\EasyAdminBundle\Dto\EntityDto #}
4+
{% set ea = ea() %}
45
{% trans_default_domain 'EasyAdminBundle' %}
56

67
{% if ea.crud.currentAction == 'detail' or not field.customOptions.get('renderAsSwitch') %}

templates/crud/field/code_editor.html.twig

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{# @var ea \EasyCorp\Bundle\EasyAdminBundle\Context\AdminContext #}
22
{# @var field \EasyCorp\Bundle\EasyAdminBundle\Dto\FieldDto #}
33
{# @var entity \EasyCorp\Bundle\EasyAdminBundle\Dto\EntityDto #}
4-
{% if ea.crud.currentAction == 'detail' %}
4+
{% if ea().crud.currentAction == 'detail' %}
55
{{ _self.render_code_editor(field) }}
66
{% else %}
77
{% set html_id = 'ea-code-editor-' ~ field.uniqueId %}

templates/crud/field/text.html.twig

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{# @var ea \EasyCorp\Bundle\EasyAdminBundle\Context\AdminContext #}
22
{# @var field \EasyCorp\Bundle\EasyAdminBundle\Dto\FieldDto #}
33
{# @var entity \EasyCorp\Bundle\EasyAdminBundle\Dto\EntityDto #}
4-
{% if ea.crud.currentAction == 'detail' %}
4+
{% if ea().crud.currentAction == 'detail' %}
55
<span title="{{ field.value }}">{{ field.formattedValue|raw|nl2br }}</span>
66
{% else %}
77
<span title="{{ field.value }}">{{ field.formattedValue|raw }}</span>

0 commit comments

Comments
 (0)