Skip to content

Commit

Permalink
feat: add dynamic locale to Filter Date Type
Browse files Browse the repository at this point in the history
I had to handle the requestStack retrieval differently between those two classes. Don't know why this is the case.
  • Loading branch information
marcwieland95 committed Jul 19, 2024
1 parent 4f2f45d commit 0cb8191
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 2 deletions.
11 changes: 10 additions & 1 deletion src/Filter/Type/DateFilterType.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,18 @@
namespace araise\TableBundle\Filter\Type;

use Doctrine\ORM\QueryBuilder;
use Symfony\Component\HttpFoundation\RequestStack;

class DateFilterType extends DatetimeFilterType
{
protected $locale;

public function __construct(?string $column = null, array $joins = [], protected ?RequestStack $requestStack = null)
{
parent::__construct($column, $joins);

This comment has been minimized.

Copy link
@ArnoEgli

ArnoEgli Jul 19, 2024

Contributor

parent::__construct($column, $joins, $requestStack);

$this->locale = $requestStack->getMainRequest()?->getLocale() ?? 'en';
}

public function getValueField(?string $value = null, ?string $operator = null): string
{
$date = \DateTime::createFromFormat(static::getQueryDataFormat(), (string) $value) ?: new \DateTime();
Expand All @@ -16,7 +25,7 @@ public function getValueField(?string $value = null, ?string $operator = null):
return sprintf(
'<input type="date" name="{name}" value="%s" data-controller="araise--core-bundle--datetime" data-araise--core-bundle--datetime-lang-value="%s">',
$operator !== static::CRITERIA_IS_EMPTY ? $value : '',
'de'
$this->locale
);
}

Expand Down
9 changes: 8 additions & 1 deletion src/Filter/Type/DatetimeFilterType.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace araise\TableBundle\Filter\Type;

use Doctrine\ORM\QueryBuilder;
use Symfony\Component\HttpFoundation\RequestStack;

class DatetimeFilterType extends FilterType
{
Expand All @@ -20,6 +21,11 @@ class DatetimeFilterType extends FilterType

public const CRITERIA_IS_EMPTY = 'is_empty';

public function __construct(?string $column = null, array $joins = [], protected ?RequestStack $requestStack = null)
{
parent::__construct($column, $joins);
}

public function getOperators(): array
{
return [
Expand All @@ -38,11 +44,12 @@ public function getValueField(?string $value = null, ?string $operator = null):
{
$date = \DateTime::createFromFormat(static::getQueryDataFormat(), (string) $value) ?: new \DateTime();
$value = $date->format(static::getDateFormat());
$locale = $this->requestStack->getMainRequest()?->getLocale();

return sprintf(
'<input type="datetime-local" name="{name}" value="%s" data-controller="araise--core-bundle--datetime" data-araise--core-bundle--datetime-lang-value="%s">',
$operator !== static::CRITERIA_IS_EMPTY ? $value : '',
'de'
$locale ?? 'en'
);
}

Expand Down

0 comments on commit 0cb8191

Please sign in to comment.