Skip to content

Commit 26dc09b

Browse files
committed
ACP2E-2127: Date filter is not working in admin grid.
- Changed the solution based on CR comments.
1 parent 68a3ace commit 26dc09b

File tree

3 files changed

+35
-42
lines changed

3 files changed

+35
-42
lines changed

app/code/Magento/Ui/Component/Filters/Type/Date.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,13 +84,25 @@ protected function applyFilter()
8484

8585
if (is_array($value)) {
8686
if (isset($value['from'])) {
87+
if (!$this->getData('config/options/showsTime')) {
88+
$value['from'] = $this->wrappedComponent->convertDateFormat(
89+
$value['from'],
90+
$this->getData('config/dateFormat')
91+
);
92+
}
8793
$this->applyFilterByType(
8894
'gteq',
8995
$this->convertDatetime((string)$value['from'])
9096
);
9197
}
9298

9399
if (isset($value['to'])) {
100+
if (!$this->getData('config/options/showsTime')) {
101+
$value['to'] = $this->wrappedComponent->convertDateFormat(
102+
$value['to'],
103+
$this->getData('config/dateFormat')
104+
);
105+
}
94106
$this->applyFilterByType(
95107
'lteq',
96108
$this->convertDatetime((string)$value['to'], 23, 59, 59)

app/code/Magento/Ui/Component/Form/Element/DataType/Date.php

Lines changed: 23 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ class Date extends AbstractDataType
1717
{
1818
public const NAME = 'date';
1919

20+
public const CUSTOM_DATE_FORMAT = 'Y-MM-dd';
21+
2022
/**
2123
* Current locale
2224
*
@@ -111,15 +113,6 @@ public function getComponentName()
111113
public function convertDate($date, $hour = 0, $minute = 0, $second = 0, $setUtcTimeZone = true)
112114
{
113115
try {
114-
if (str_contains($date, '-')) {
115-
$date = $this->localeDate->formatDateTime(
116-
$date,
117-
null,
118-
null,
119-
$this->getLocale(),
120-
date_default_timezone_get()
121-
);
122-
}
123116
$dateObj = $this->localeDate->date($date, $this->getLocale(), false, false);
124117
$dateObj->setTime($hour, $minute, $second);
125118
//convert store date to default date in UTC timezone without DST
@@ -153,4 +146,25 @@ public function convertDatetime(string $date, bool $setUtcTimezone = true): ?\Da
153146
return null;
154147
}
155148
}
149+
150+
/**
151+
* Convert given date to specific date format based on locale
152+
*
153+
* @param string $date
154+
* @param string $dateFormat
155+
* @return String
156+
*/
157+
public function convertDateFormat(string $date, string $dateFormat): String
158+
{
159+
if ($dateFormat === self::CUSTOM_DATE_FORMAT) {
160+
$date = $this->localeDate->formatDateTime(
161+
$date,
162+
null,
163+
null,
164+
$this->getLocale(),
165+
date_default_timezone_get()
166+
);
167+
}
168+
return $date;
169+
}
156170
}

app/code/Magento/Ui/Test/Unit/Component/Form/Element/DataType/DateTest.php

Lines changed: 0 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77

88
namespace Magento\Ui\Test\Unit\Component\Form\Element\DataType;
99

10-
use Exception;
1110
use Magento\Framework\Locale\ResolverInterface;
1211
use Magento\Framework\Stdlib\DateTime\TimezoneInterface;
1312
use Magento\Framework\TestFramework\Unit\Helper\ObjectManager;
@@ -172,38 +171,6 @@ public function testConvertDatetime(string $dateStr, bool $setUtcTimeZone, strin
172171
);
173172
}
174173

175-
/**
176-
* Test to Convert given date to default (UTC) timezone
177-
*
178-
* @throws Exception
179-
*/
180-
public function testConvertDate()
181-
{
182-
$dateStr = '2023-10-11';
183-
$this->date = $this->objectManagerHelper->getObject(
184-
Date::class,
185-
[
186-
'localeDate' => $this->localeDateMock,
187-
]
188-
);
189-
$this->localeDateMock->expects($this->once())
190-
->method('formatDateTime')
191-
->with(
192-
$dateStr
193-
)
194-
->willReturn(date_format(date_create($dateStr), 'd/m/Y'));
195-
$this->localeDateMock->expects($this->once())
196-
->method('date')
197-
->willReturn(
198-
(new \DateTime('now', new \DateTimeZone('UTC')))
199-
->setTimestamp(strtotime($dateStr))
200-
);
201-
$this->assertEquals(
202-
$dateStr,
203-
$this->date->convertDate($dateStr)->format('Y-m-d')
204-
);
205-
}
206-
207174
/**
208175
* @return array
209176
*/

0 commit comments

Comments
 (0)