Skip to content

Commit f5a7ac9

Browse files
committed
MC-5233: DateTime product attributes support
1 parent 419fbf3 commit f5a7ac9

File tree

27 files changed

+872
-235
lines changed

27 files changed

+872
-235
lines changed

app/code/Magento/Catalog/Block/Adminhtml/Product/Attribute/Edit/Tab/Advanced.php

Lines changed: 46 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,16 +7,20 @@
77
namespace Magento\Catalog\Block\Adminhtml\Product\Attribute\Edit\Tab;
88

99
use Magento\Backend\Block\Widget\Form\Generic;
10+
use Magento\Catalog\Model\ResourceModel\Eav\Attribute;
1011
use Magento\Config\Model\Config\Source\Yesno;
1112
use Magento\Eav\Block\Adminhtml\Attribute\PropertyLocker;
1213
use Magento\Eav\Helper\Data;
1314
use Magento\Framework\App\ObjectManager;
15+
use Magento\Framework\Exception\LocalizedException;
16+
use Magento\Framework\Stdlib\DateTime;
1417

1518
/**
16-
* Product attribute add/edit form main tab
19+
* Product attribute add/edit advanced form tab
1720
*
1821
* @api
1922
* @since 100.0.2
23+
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
2024
*/
2125
class Advanced extends Generic
2226
{
@@ -70,7 +74,7 @@ public function __construct(
7074
* Adding product form elements for editing attribute
7175
*
7276
* @return $this
73-
* @throws \Magento\Framework\Exception\LocalizedException
77+
* @throws LocalizedException
7478
* @SuppressWarnings(PHPMD)
7579
*/
7680
protected function _prepareForm()
@@ -139,7 +143,21 @@ protected function _prepareForm()
139143
'label' => __('Default Value'),
140144
'title' => __('Default Value'),
141145
'value' => $attributeObject->getDefaultValue(),
142-
'date_format' => $dateFormat
146+
'date_format' => $dateFormat,
147+
]
148+
);
149+
150+
$timeFormat = $this->_localeDate->getTimeFormat(\IntlDateFormatter::SHORT);
151+
$fieldset->addField(
152+
'default_value_datetime',
153+
'date',
154+
[
155+
'name' => 'default_value_datetime',
156+
'label' => __('Default Value'),
157+
'title' => __('Default Value'),
158+
'value' => $this->getLocalizedDateDefaultValue(),
159+
'date_format' => $dateFormat,
160+
'time_format' => $timeFormat,
143161
]
144162
);
145163

@@ -266,7 +284,7 @@ protected function _initFormValues()
266284
/**
267285
* Retrieve attribute object from registry
268286
*
269-
* @return mixed
287+
* @return Attribute
270288
*/
271289
private function getAttributeObject()
272290
{
@@ -285,4 +303,28 @@ private function getPropertyLocker()
285303
}
286304
return $this->propertyLocker;
287305
}
306+
307+
/**
308+
* Get localized date default value
309+
*
310+
* @return string
311+
* @throws LocalizedException
312+
*/
313+
private function getLocalizedDateDefaultValue(): string
314+
{
315+
$attributeObject = $this->getAttributeObject();
316+
if (empty($attributeObject->getDefaultValue()) || $attributeObject->getFrontendInput() !== 'datetime') {
317+
return (string)$attributeObject->getDefaultValue();
318+
}
319+
320+
try {
321+
$localizedDate = $this->_localeDate->date($attributeObject->getDefaultValue(), null, false);
322+
$localizedDate->setTimezone(new \DateTimeZone($this->_localeDate->getConfigTimezone()));
323+
$localizedDate = $localizedDate->format(DateTime::DATETIME_PHP_FORMAT);
324+
} catch (\Exception $e) {
325+
throw new LocalizedException(__('The default date is invalid.'));
326+
}
327+
328+
return $localizedDate;
329+
}
288330
}

app/code/Magento/Catalog/Block/Adminhtml/Product/Attribute/Edit/Tab/Main.php

Lines changed: 72 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -7,60 +7,60 @@
77
/**
88
* Product attribute add/edit form main tab
99
*
10-
* @author Magento Core Team <[email protected]>
10+
* @author Magento Core Team <[email protected]>
1111
*/
1212
namespace Magento\Catalog\Block\Adminhtml\Product\Attribute\Edit\Tab;
1313

14+
use Magento\Catalog\Block\Adminhtml\Product\Helper\Form\Apply as HelperApply;
1415
use Magento\Eav\Block\Adminhtml\Attribute\Edit\Main\AbstractMain;
16+
use Magento\Framework\Data\Form\Element\AbstractElement;
17+
use Magento\Framework\Data\Form\Element\Fieldset;
18+
use Magento\Framework\DataObject;
1519

1620
/**
21+
* Product attribute add/edit form main tab
22+
*
1723
* @api
18-
* @SuppressWarnings(PHPMD.DepthOfInheritance)
1924
* @since 100.0.2
2025
*/
2126
class Main extends AbstractMain
2227
{
2328
/**
24-
* Adding product form elements for editing attribute
25-
*
26-
* @return $this
27-
* @SuppressWarnings(PHPMD.UnusedLocalVariable)
29+
* @inheritdoc
2830
*/
2931
protected function _prepareForm()
3032
{
3133
parent::_prepareForm();
32-
/** @var \Magento\Catalog\Model\ResourceModel\Eav\Attribute $attributeObject */
33-
$attributeObject = $this->getAttributeObject();
34-
/* @var $form \Magento\Framework\Data\Form */
35-
$form = $this->getForm();
36-
/* @var $fieldset \Magento\Framework\Data\Form\Element\Fieldset */
37-
$fieldset = $form->getElement('base_fieldset');
38-
$fieldsToRemove = ['attribute_code', 'is_unique', 'frontend_class'];
3934

40-
foreach ($fieldset->getElements() as $element) {
41-
/** @var \Magento\Framework\Data\Form\AbstractForm $element */
42-
if (substr($element->getId(), 0, strlen('default_value')) == 'default_value') {
43-
$fieldsToRemove[] = $element->getId();
44-
}
45-
}
46-
foreach ($fieldsToRemove as $id) {
47-
$fieldset->removeField($id);
48-
}
35+
$this->removeUnusedFields();
36+
$this->processFrontendInputTypes();
37+
38+
$this->_eventManager->dispatch('product_attribute_form_build_main_tab', ['form' => $this->getForm()]);
39+
40+
return $this;
41+
}
4942

43+
/**
44+
* @inheritdoc
45+
*/
46+
protected function _getAdditionalElementTypes()
47+
{
48+
return ['apply' => HelperApply::class];
49+
}
50+
51+
/**
52+
* Process frontend input types for product attributes
53+
*
54+
* @return void
55+
*/
56+
private function processFrontendInputTypes(): void
57+
{
58+
$form = $this->getForm();
59+
/** @var AbstractElement $frontendInputElm */
5060
$frontendInputElm = $form->getElement('frontend_input');
51-
$additionalTypes = [
52-
['value' => 'price', 'label' => __('Price')],
53-
['value' => 'media_image', 'label' => __('Media Image')],
54-
];
55-
$additionalReadOnlyTypes = ['gallery' => __('Gallery')];
56-
if (isset($additionalReadOnlyTypes[$attributeObject->getFrontendInput()])) {
57-
$additionalTypes[] = [
58-
'value' => $attributeObject->getFrontendInput(),
59-
'label' => $additionalReadOnlyTypes[$attributeObject->getFrontendInput()],
60-
];
61-
}
61+
$additionalTypes = $this->getAdditionalFrontendInputTypes();
6262

63-
$response = new \Magento\Framework\DataObject();
63+
$response = new DataObject();
6464
$response->setTypes([]);
6565
$this->_eventManager->dispatch('adminhtml_product_attribute_types', ['response' => $response]);
6666
$_hiddenFields = [];
@@ -74,19 +74,51 @@ protected function _prepareForm()
7474

7575
$frontendInputValues = array_merge($frontendInputElm->getValues(), $additionalTypes);
7676
$frontendInputElm->setValues($frontendInputValues);
77+
}
7778

78-
$this->_eventManager->dispatch('product_attribute_form_build_main_tab', ['form' => $form]);
79+
/**
80+
* Get additional Frontend Input Types for product attributes
81+
*
82+
* @return array
83+
*/
84+
private function getAdditionalFrontendInputTypes(): array
85+
{
86+
$additionalTypes = [
87+
['value' => 'price', 'label' => __('Price')],
88+
['value' => 'media_image', 'label' => __('Media Image')],
89+
];
7990

80-
return $this;
91+
$additionalReadOnlyTypes = ['gallery' => __('Gallery')];
92+
$attributeObject = $this->getAttributeObject();
93+
if (isset($additionalReadOnlyTypes[$attributeObject->getFrontendInput()])) {
94+
$additionalTypes[] = [
95+
'value' => $attributeObject->getFrontendInput(),
96+
'label' => $additionalReadOnlyTypes[$attributeObject->getFrontendInput()],
97+
];
98+
}
99+
100+
return $additionalTypes;
81101
}
82102

83103
/**
84-
* Retrieve additional element types for product attributes
104+
* Remove unused form fields
85105
*
86-
* @return array
106+
* @return void
87107
*/
88-
protected function _getAdditionalElementTypes()
108+
private function removeUnusedFields(): void
89109
{
90-
return ['apply' => \Magento\Catalog\Block\Adminhtml\Product\Helper\Form\Apply::class];
110+
$form = $this->getForm();
111+
/* @var $fieldset Fieldset */
112+
$fieldset = $form->getElement('base_fieldset');
113+
$fieldsToRemove = ['attribute_code', 'is_unique', 'frontend_class'];
114+
foreach ($fieldset->getElements() as $element) {
115+
/** @var AbstractElement $element */
116+
if (substr($element->getId(), 0, strlen('default_value')) == 'default_value') {
117+
$fieldsToRemove[] = $element->getId();
118+
}
119+
}
120+
foreach ($fieldsToRemove as $id) {
121+
$fieldset->removeField($id);
122+
}
91123
}
92124
}

0 commit comments

Comments
 (0)