Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Create custom intl twig functions #5975

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
},
Expand Down
4 changes: 3 additions & 1 deletion src/DependencyInjection/AbstractSonataAdminExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -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'], [
Expand Down
2 changes: 2 additions & 0 deletions src/DependencyInjection/SonataAdminExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -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';
}
Expand Down
10 changes: 10 additions & 0 deletions src/Resources/config/intl.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?xml version="1.0" encoding="UTF-8"?>
<container xmlns="http://symfony.com/schema/dic/services" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd">
<services>
<service id="Twig\Extra\Intl\IntlExtension"/>
<service id="Sonata\AdminBundle\Twig\Extension\IntlExtension">
<argument type="service" id="Twig\Extra\Intl\IntlExtension"/>
<tag name="twig.extension"/>
</service>
</services>
</container>
2 changes: 1 addition & 1 deletion src/Resources/views/CRUD/Intl/display_currency.html.twig
Original file line number Diff line number Diff line change
Expand Up @@ -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 -%}
2 changes: 1 addition & 1 deletion src/Resources/views/CRUD/Intl/display_date.html.twig
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ file that was distributed with this source code.
{% set dateType = field_description.options.dateType|default(null) %}

<time datetime="{{ value|date('Y-m-d', 'UTC') }}" title="{{ value|date('Y-m-d', 'UTC') }}">
{{ value|format_date(dateType, pattern, timezone, calendar, locale) }}
{{ value|sonata_format_date(dateType, pattern, timezone, calendar, locale) }}
</time>
{%- endif -%}
{% endapply -%}
2 changes: 1 addition & 1 deletion src/Resources/views/CRUD/Intl/display_datetime.html.twig
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ file that was distributed with this source code.
{% set timeType = field_description.options.timeType|default(null) %}

<time datetime="{{ value|date('c', 'UTC') }}" title="{{ value|date('c', 'UTC') }}">
{{ value|format_datetime(dateType, timeType, pattern, timezone, calendar, locale) }}
{{ value|sonata_format_datetime(dateType, timeType, pattern, timezone, calendar, locale) }}
</time>
{%- endif -%}
{% endapply -%}
2 changes: 1 addition & 1 deletion src/Resources/views/CRUD/Intl/display_decimal.html.twig
Original file line number Diff line number Diff line change
Expand Up @@ -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 -%}
2 changes: 1 addition & 1 deletion src/Resources/views/CRUD/Intl/display_percent.html.twig
Original file line number Diff line number Diff line change
Expand Up @@ -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 -%}
65 changes: 65 additions & 0 deletions src/Twig/Extension/IntlExtension.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
<?php

declare(strict_types=1);

/*
* This file is part of the Sonata Project package.
*
* (c) Thomas Rabaix <[email protected]>
*
* 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);
}
}
3 changes: 3 additions & 0 deletions tests/DependencyInjection/SonataAdminExtensionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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
Expand Down