Skip to content

Commit

Permalink
feature #6199 Fixed #6191 // Added support for translatable objects i…
Browse files Browse the repository at this point in the history
…n Twig (Geolim4)

This PR was merged into the 4.x branch.

Discussion
----------

Fixed #6191 // Added support for translatable objects in Twig

Fixed #6191 // Added support for translatable objects in Twig

Commits
-------

9aa5893 Fixed #6191 // Added support for translatable objects in Twig
  • Loading branch information
javiereguiluz committed Mar 15, 2024
2 parents 42a3177 + 9aa5893 commit b1e739e
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 1 deletion.
1 change: 1 addition & 0 deletions src/Resources/config/services.php
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,7 @@
->arg(1, service(AdminContextProvider::class))
->arg(2, new Reference('security.csrf.token_manager', ContainerInterface::NULL_ON_INVALID_REFERENCE))
->arg(3, new Reference('asset_mapper.importmap.renderer', ContainerInterface::NULL_ON_INVALID_REFERENCE))
->arg(4, service('translator.default'))
->tag('twig.extension')

->set(EaCrudFormTypeExtension::class)
Expand Down
10 changes: 9 additions & 1 deletion src/Twig/EasyAdminTwigExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
use Symfony\Component\AssetMapper\ImportMap\ImportMapRenderer;
use Symfony\Component\DependencyInjection\ServiceLocator;
use Symfony\Component\Security\Csrf\CsrfTokenManagerInterface;
use Symfony\Contracts\Translation\TranslatableInterface;
use Symfony\Contracts\Translation\TranslatorInterface;
use Twig\Environment;
use Twig\Error\RuntimeError;
use Twig\Extension\AbstractExtension;
Expand All @@ -31,13 +33,15 @@ class EasyAdminTwigExtension extends AbstractExtension implements GlobalsInterfa
private AdminContextProvider $adminContextProvider;
private ?CsrfTokenManagerInterface $csrfTokenManager;
private ?ImportMapRenderer $importMapRenderer;
private TranslatorInterface $translator;

public function __construct(ServiceLocator $serviceLocator, AdminContextProvider $adminContextProvider, ?CsrfTokenManagerInterface $csrfTokenManager, ?ImportMapRenderer $importMapRenderer)
public function __construct(ServiceLocator $serviceLocator, AdminContextProvider $adminContextProvider, ?CsrfTokenManagerInterface $csrfTokenManager, ?ImportMapRenderer $importMapRenderer, TranslatorInterface $translator)
{
$this->serviceLocator = $serviceLocator;
$this->adminContextProvider = $adminContextProvider;
$this->csrfTokenManager = $csrfTokenManager;
$this->importMapRenderer = $importMapRenderer;
$this->translator = $translator;
}

public function getFunctions(): array
Expand Down Expand Up @@ -155,6 +159,10 @@ public function representAsString($value): string
}

if (\is_object($value)) {
if ($value instanceof TranslatableInterface) {
return $value->trans($this->translator);
}

if (method_exists($value, '__toString')) {
return (string) $value;
}
Expand Down

0 comments on commit b1e739e

Please sign in to comment.