diff --git a/composer.json b/composer.json index 311aeff7c9..d7cc3be325 100644 --- a/composer.json +++ b/composer.json @@ -53,7 +53,6 @@ "symfony/twig-bundle": "^4.3", "symfony/validator": "^4.3", "twig/extensions": "^1.5", - "twig/extra-bundle": "^3.0", "twig/intl-extra": "^3.0", "twig/twig": "^2.12" }, diff --git a/src/DependencyInjection/AbstractSonataAdminExtension.php b/src/DependencyInjection/AbstractSonataAdminExtension.php index 3786ab059a..1cf5724b59 100644 --- a/src/DependencyInjection/AbstractSonataAdminExtension.php +++ b/src/DependencyInjection/AbstractSonataAdminExtension.php @@ -78,7 +78,9 @@ protected function fixTemplatesConfiguration( ], ]; - $useIntlTemplates = $container->getParameter('sonata.admin.configuration.use_intl_templates'); + $useIntlTemplates = $container->hasParameter('sonata.admin.configuration.use_intl_templates') + ? $container->getParameter('sonata.admin.configuration.use_intl_templates') + : false; if ($useIntlTemplates) { $defaultConfig['templates']['types']['list'] = array_merge($defaultConfig['templates']['types']['list'], [ diff --git a/src/DependencyInjection/SonataAdminExtension.php b/src/DependencyInjection/SonataAdminExtension.php index e87862d1dd..665eec2e38 100644 --- a/src/DependencyInjection/SonataAdminExtension.php +++ b/src/DependencyInjection/SonataAdminExtension.php @@ -91,6 +91,8 @@ public function load(array $configs, ContainerBuilder $container) $useIntlTemplates = $container->getParameter('sonata.admin.configuration.use_intl_templates'); if ($useIntlTemplates) { + $loader->load('intl.xml'); + if ('@SonataAdmin/CRUD/history_revision_timestamp.html.twig' === $config['templates']['history_revision_timestamp']) { $config['templates']['history_revision_timestamp'] = '@SonataAdmin/CRUD/Intl/history_revision_timestamp.html.twig'; } diff --git a/src/Resources/config/intl.xml b/src/Resources/config/intl.xml new file mode 100644 index 0000000000..65c3fc0de9 --- /dev/null +++ b/src/Resources/config/intl.xml @@ -0,0 +1,10 @@ + + + + + + + + + + diff --git a/src/Resources/views/CRUD/Intl/display_currency.html.twig b/src/Resources/views/CRUD/Intl/display_currency.html.twig index 400972c224..6cc815f3e7 100644 --- a/src/Resources/views/CRUD/Intl/display_currency.html.twig +++ b/src/Resources/views/CRUD/Intl/display_currency.html.twig @@ -17,6 +17,6 @@ file that was distributed with this source code. {% set attributes = field_description.options.attributes|default({}) %} {% set locale = field_description.options.locale|default(null) %} - {{ value|format_currency(currency, attributes, locale) }} + {{ value|sonata_format_currency(currency, attributes, locale) }} {%- endif -%} {% endapply -%} diff --git a/src/Resources/views/CRUD/Intl/display_date.html.twig b/src/Resources/views/CRUD/Intl/display_date.html.twig index f638256ca2..05b0db5827 100644 --- a/src/Resources/views/CRUD/Intl/display_date.html.twig +++ b/src/Resources/views/CRUD/Intl/display_date.html.twig @@ -20,7 +20,7 @@ file that was distributed with this source code. {% set dateType = field_description.options.dateType|default(null) %} {%- endif -%} {% endapply -%} diff --git a/src/Resources/views/CRUD/Intl/display_datetime.html.twig b/src/Resources/views/CRUD/Intl/display_datetime.html.twig index dd42a183fc..85bbf0af75 100644 --- a/src/Resources/views/CRUD/Intl/display_datetime.html.twig +++ b/src/Resources/views/CRUD/Intl/display_datetime.html.twig @@ -21,7 +21,7 @@ file that was distributed with this source code. {% set timeType = field_description.options.timeType|default(null) %} {%- endif -%} {% endapply -%} diff --git a/src/Resources/views/CRUD/Intl/display_decimal.html.twig b/src/Resources/views/CRUD/Intl/display_decimal.html.twig index f294cc9bc9..913792b15a 100644 --- a/src/Resources/views/CRUD/Intl/display_decimal.html.twig +++ b/src/Resources/views/CRUD/Intl/display_decimal.html.twig @@ -17,6 +17,6 @@ file that was distributed with this source code. {% set locale = field_description.options.locale|default(null) %} {% set type = field_description.options.type|default('default') %} - {{ value | format_number(attributes, 'decimal', type, locale) }} + {{ value | sonata_format_number(attributes, 'decimal', type, locale) }} {%- endif -%} {% endapply -%} diff --git a/src/Resources/views/CRUD/Intl/display_percent.html.twig b/src/Resources/views/CRUD/Intl/display_percent.html.twig index 43808f9e10..37cd696c2a 100644 --- a/src/Resources/views/CRUD/Intl/display_percent.html.twig +++ b/src/Resources/views/CRUD/Intl/display_percent.html.twig @@ -17,6 +17,6 @@ file that was distributed with this source code. {% set locale = field_description.options.locale|default(null) %} {% set type = field_description.options.type|default('default') %} - {{ value | format_number(attributes, 'percent', type, locale) }} + {{ value | sonata_format_number(attributes, 'percent', type, locale) }} {%- endif -%} {% endapply -%} diff --git a/src/Twig/Extension/IntlExtension.php b/src/Twig/Extension/IntlExtension.php new file mode 100644 index 0000000000..049850313d --- /dev/null +++ b/src/Twig/Extension/IntlExtension.php @@ -0,0 +1,65 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Sonata\AdminBundle\Twig\Extension; + +use Twig\Environment; +use Twig\Extension\AbstractExtension; +use Twig\Extra\Intl\IntlExtension as TwigIntlExtension; +use Twig\TwigFilter; + +/** + * @internal + */ +final class IntlExtension extends AbstractExtension +{ + /** + * @var TwigIntlExtension + */ + private $extension; + + public function __construct(TwigIntlExtension $extension) + { + $this->extension = $extension; + } + + public function getFilters(): array + { + return [ + new TwigFilter('sonata_format_currency', [$this, 'formatCurrency']), + new TwigFilter('sonata_format_number', [$this, 'formatNumber']), + new TwigFilter('sonata_format_datetime', [$this, 'formatDateTime'], ['needs_environment' => true]), + new TwigFilter('sonata_format_date', [$this, 'formatDate'], ['needs_environment' => true]), + ]; + } + + public function formatCurrency($amount, string $currency, array $attrs = [], string $locale = null): string + { + return $this->extension->formatCurrency($amount, $currency, $attrs, $locale); + } + + public function formatNumber($number, array $attrs = [], string $style = 'decimal', string $type = 'default', string $locale = null): string + { + return $this->extension->formatNumber($number, $attrs, $style, $type, $locale); + } + + public function formatDateTime(Environment $env, $date, ?string $dateFormat = 'medium', ?string $timeFormat = 'medium', string $pattern = '', $timezone = null, string $calendar = 'gregorian', string $locale = null): string + { + return $this->extension->formatDateTime($env, $date, $dateFormat, $timeFormat, $pattern, $timezone, $calendar, $locale); + } + + public function formatDate(Environment $env, $date, ?string $dateFormat = 'medium', string $pattern = '', $timezone = null, string $calendar = 'gregorian', string $locale = null): string + { + return $this->extension->formatDateTime($env, $date, $dateFormat, 'none', $pattern, $timezone, $calendar, $locale); + } +} diff --git a/tests/DependencyInjection/SonataAdminExtensionTest.php b/tests/DependencyInjection/SonataAdminExtensionTest.php index 5bbb91d69d..7ca31185e5 100644 --- a/tests/DependencyInjection/SonataAdminExtensionTest.php +++ b/tests/DependencyInjection/SonataAdminExtensionTest.php @@ -37,6 +37,7 @@ use Sonata\AdminBundle\Translator\NativeLabelTranslatorStrategy; use Sonata\AdminBundle\Translator\NoopLabelTranslatorStrategy; use Sonata\AdminBundle\Translator\UnderscoreLabelTranslatorStrategy; +use Sonata\AdminBundle\Twig\Extension\IntlExtension; use Sonata\AdminBundle\Twig\GlobalVariables; use Symfony\Component\DependencyInjection\ContainerBuilder; @@ -320,6 +321,8 @@ public function testLoadIntlTemplate(): void $templates = $container->getParameter('sonata.admin.configuration.templates'); $this->assertSame('@SonataAdmin/CRUD/Intl/history_revision_timestamp.html.twig', $templates['history_revision_timestamp']); + + $this->assertTrue($container->hasDefinition(IntlExtension::class)); } protected function getContainerExtensions(): array