-
-
Notifications
You must be signed in to change notification settings - Fork 9.7k
[TwigBundle] make date formats and number formats configurable #13554
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -42,6 +42,7 @@ public function getConfigTreeBuilder() | |
$this->addFormThemesSection($rootNode); | ||
$this->addGlobalsSection($rootNode); | ||
$this->addTwigOptions($rootNode); | ||
$this->addTwigFormatOptions($rootNode); | ||
|
||
return $treeBuilder; | ||
} | ||
|
@@ -207,4 +208,33 @@ private function addTwigOptions(ArrayNodeDefinition $rootNode) | |
->end() | ||
; | ||
} | ||
|
||
private function addTwigFormatOptions(ArrayNodeDefinition $rootNode) | ||
{ | ||
$rootNode | ||
->children() | ||
->arrayNode('date') | ||
->info('The default format options used by the date filter') | ||
->addDefaultsIfNotSet() | ||
->children() | ||
->scalarNode('format')->defaultValue('F j, Y H:i')->end() | ||
->scalarNode('interval_format')->defaultValue('%d days')->end() | ||
->scalarNode('timezone') | ||
->info('The timezone used when formatting dates, when set to null, the timezone returned by date_default_timezone_get() is used') | ||
->defaultNull() | ||
->end() | ||
->end() | ||
->end() | ||
->arrayNode('number_format') | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. should add info that this section configures the default behavior of the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. done |
||
->info('The default format options for the number_format filter') | ||
->addDefaultsIfNotSet() | ||
->children() | ||
->integerNode('decimals')->defaultValue(0)->end() | ||
->scalarNode('decimal_point')->defaultValue('.')->end() | ||
->scalarNode('thousands_separator')->defaultValue(',')->end() | ||
->end() | ||
->end() | ||
->end() | ||
; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <fabien@symfony.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Symfony\Bundle\TwigBundle\DependencyInjection\Configurator; | ||
|
||
/** | ||
* Twig environment configurator. | ||
* | ||
* @author Christian Flothmann <christian.flothmann@xabbuh.de> | ||
*/ | ||
class EnvironmentConfigurator | ||
{ | ||
private $dateFormat; | ||
private $intervalFormat; | ||
private $timezone; | ||
private $decimals; | ||
private $decimalPoint; | ||
private $thousandsSeparator; | ||
|
||
public function __construct($dateFormat, $intervalFormat, $timezone, $decimals, $decimalPoint, $thousandsSeparator) | ||
{ | ||
$this->dateFormat = $dateFormat; | ||
$this->intervalFormat = $intervalFormat; | ||
$this->timezone = $timezone; | ||
$this->decimals = $decimals; | ||
$this->decimalPoint = $decimalPoint; | ||
$this->thousandsSeparator = $thousandsSeparator; | ||
} | ||
|
||
public function configure(\Twig_Environment $environment) | ||
{ | ||
$environment->getExtension('core')->setDateFormat($this->dateFormat, $this->intervalFormat); | ||
|
||
if (null !== $this->timezone) { | ||
$environment->getExtension('core')->setTimezone($this->timezone); | ||
} | ||
|
||
$environment->getExtension('core')->setNumberFormat($this->decimals, $this->decimalPoint, $this->thousandsSeparator); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -57,6 +57,14 @@ public function load(array $configs, ContainerBuilder $container) | |
|
||
$container->setParameter('twig.form.resources', $config['form_themes']); | ||
|
||
$envConfiguratorDefinition = $container->getDefinition('twig.configurator.environment'); | ||
$envConfiguratorDefinition->replaceArgument(0, $config['date']['format']); | ||
$envConfiguratorDefinition->replaceArgument(1, $config['date']['interval_format']); | ||
$envConfiguratorDefinition->replaceArgument(2, $config['date']['timezone']); | ||
$envConfiguratorDefinition->replaceArgument(3, $config['number_format']['decimals']); | ||
$envConfiguratorDefinition->replaceArgument(4, $config['number_format']['decimal_point']); | ||
$envConfiguratorDefinition->replaceArgument(5, $config['number_format']['thousands_separator']); | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. All those parameters should be removed and instead, you should inject the value in the service directly. That's what we are doing now as much as possible to avoid polluting the parameters with information you don't need to access. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. done |
||
$twigFilesystemLoaderDefinition = $container->getDefinition('twig.loader.filesystem'); | ||
|
||
// register user-configured paths | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
should add info that this section configures the default behavior of the
date
filter