From e9511e8db117b06516c4b8cc541376e5657a264d Mon Sep 17 00:00:00 2001 From: Dan Wallis Date: Wed, 13 May 2020 21:24:10 +0100 Subject: [PATCH 01/13] Replace hard-coded list of category attributes --- .../Catalog/Model/Category/DataProvider.php | 75 +++++++------------ app/code/Magento/Catalog/etc/di.xml | 3 + 2 files changed, 28 insertions(+), 50 deletions(-) diff --git a/app/code/Magento/Catalog/Model/Category/DataProvider.php b/app/code/Magento/Catalog/Model/Category/DataProvider.php index d8c79c485e3e5..d9e655fff2cef 100644 --- a/app/code/Magento/Catalog/Model/Category/DataProvider.php +++ b/app/code/Magento/Catalog/Model/Category/DataProvider.php @@ -22,6 +22,7 @@ use Magento\Eav\Model\Entity\Type; use Magento\Framework\App\ObjectManager; use Magento\Framework\App\RequestInterface; +use Magento\Framework\Config\DataInterfaceFactory; use Magento\Framework\Exception\LocalizedException; use Magento\Framework\Exception\NoSuchEntityException; use Magento\Framework\Registry; @@ -153,6 +154,11 @@ class DataProvider extends ModifierPoolDataProvider */ private $categoryFactory; + /** + * @var DataInterfaceFactory + */ + protected $uiConfigFactory; + /** * @var ScopeOverriddenValue */ @@ -193,6 +199,7 @@ class DataProvider extends ModifierPoolDataProvider * @param Config $eavConfig * @param RequestInterface $request * @param CategoryFactory $categoryFactory + * @param DataInterfaceFactory $uiConfigFactory * @param array $meta * @param array $data * @param PoolInterface|null $pool @@ -215,6 +222,7 @@ public function __construct( Config $eavConfig, RequestInterface $request, CategoryFactory $categoryFactory, + DataInterfaceFactory $uiConfigFactory, array $meta = [], array $data = [], PoolInterface $pool = null, @@ -233,6 +241,7 @@ public function __construct( $this->storeManager = $storeManager; $this->request = $request; $this->categoryFactory = $categoryFactory; + $this->uiConfigFactory = $uiConfigFactory; $this->auth = $auth ?? ObjectManager::getInstance()->get(AuthorizationInterface::class); $this->arrayUtils = $arrayUtils ?? ObjectManager::getInstance()->get(ArrayUtils::class); $this->scopeOverriddenValue = $scopeOverriddenValue ?: @@ -645,56 +654,22 @@ public function getDefaultMetaData($result) */ protected function getFieldsMap() { - return [ - 'general' => [ - 'parent', - 'path', - 'is_active', - 'include_in_menu', - 'name', - ], - 'content' => [ - 'image', - 'description', - 'landing_page', - ], - 'display_settings' => [ - 'display_mode', - 'is_anchor', - 'available_sort_by', - 'use_config.available_sort_by', - 'default_sort_by', - 'use_config.default_sort_by', - 'filter_price_range', - 'use_config.filter_price_range', - ], - 'search_engine_optimization' => [ - 'url_key', - 'url_key_create_redirect', - 'url_key_group', - 'meta_title', - 'meta_keywords', - 'meta_description', - ], - 'assign_products' => [ - ], - 'design' => [ - 'custom_use_parent_settings', - 'custom_apply_to_products', - 'custom_design', - 'page_layout', - 'custom_layout_update', - 'custom_layout_update_file' - ], - 'schedule_design_update' => [ - 'custom_design_from', - 'custom_design_to', - ], - 'category_view_optimization' => [ - ], - 'category_permissions' => [ - ], - ]; + $referenceName = 'category_form'; + $config = $this->uiConfigFactory + ->create(['componentName' => $referenceName]) + ->get($referenceName); + + $fieldsMap = []; + + if (isset($config['children']) && !empty($config['children'])) { + foreach ($config['children'] as $name => $child) { + if (isset($child['children']) && !empty($child['children'])) { + $fieldsMap[$name] = array_keys($child['children']); + } + } + } + + return $fieldsMap; } /** diff --git a/app/code/Magento/Catalog/etc/di.xml b/app/code/Magento/Catalog/etc/di.xml index 5a7a3135b4bfe..b012219ee8f44 100644 --- a/app/code/Magento/Catalog/etc/di.xml +++ b/app/code/Magento/Catalog/etc/di.xml @@ -79,6 +79,9 @@ + + uiComponentConfigFactory + From 3bd5fa090b9c4a597220af07281b894b38372314 Mon Sep 17 00:00:00 2001 From: Dan Wallis Date: Mon, 13 Jul 2020 18:50:46 +0100 Subject: [PATCH 02/13] Fix unit test --- .../Test/Unit/Model/Category/DataProviderTest.php | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/app/code/Magento/Catalog/Test/Unit/Model/Category/DataProviderTest.php b/app/code/Magento/Catalog/Test/Unit/Model/Category/DataProviderTest.php index e2c37c904ee82..b824929de0733 100644 --- a/app/code/Magento/Catalog/Test/Unit/Model/Category/DataProviderTest.php +++ b/app/code/Magento/Catalog/Test/Unit/Model/Category/DataProviderTest.php @@ -20,6 +20,8 @@ use Magento\Eav\Model\Entity\Type; use Magento\Framework\App\RequestInterface; use Magento\Framework\AuthorizationInterface; +use Magento\Framework\Config\Data; +use Magento\Framework\Config\DataInterfaceFactory; use Magento\Framework\Registry; use Magento\Framework\Stdlib\ArrayUtils; use Magento\Framework\TestFramework\Unit\Helper\ObjectManager; @@ -151,6 +153,15 @@ protected function setUp(): void ->disableOriginalConstructor() ->getMock(); + $dataMock = $this->getMockBuilder(Data::class) + ->disableOriginalConstructor() + ->getMock(); + $this->uiConfigFactory = $this->getMockBuilder(DataInterfaceFactory::class) + ->disableOriginalConstructor() + ->getMock(); + $this->uiConfigFactory->method('create') + ->willReturn($dataMock); + $this->fileInfo = $this->getMockBuilder(FileInfo::class) ->disableOriginalConstructor() ->getMock(); @@ -198,6 +209,7 @@ private function getModel() 'eavConfig' => $this->eavConfig, 'request' => $this->request, 'categoryFactory' => $this->categoryFactory, + 'uiConfigFactory' => $this->uiConfigFactory, 'pool' => $this->modifierPool, 'auth' => $this->auth, 'arrayUtils' => $this->arrayUtils, From 471dc6b18b03b615729c51f22fc061a209e7e05a Mon Sep 17 00:00:00 2001 From: Dan Wallis Date: Mon, 27 Jul 2020 18:46:48 +0100 Subject: [PATCH 03/13] Add special handling of config nodes --- .../Catalog/Model/Category/DataProvider.php | 25 ++++++++++++++++--- 1 file changed, 21 insertions(+), 4 deletions(-) diff --git a/app/code/Magento/Catalog/Model/Category/DataProvider.php b/app/code/Magento/Catalog/Model/Category/DataProvider.php index d9e655fff2cef..5d2dcf07f68bc 100644 --- a/app/code/Magento/Catalog/Model/Category/DataProvider.php +++ b/app/code/Magento/Catalog/Model/Category/DataProvider.php @@ -659,14 +659,31 @@ protected function getFieldsMap() ->create(['componentName' => $referenceName]) ->get($referenceName); + if (empty($config)) { + return []; + } + $fieldsMap = []; - if (isset($config['children']) && !empty($config['children'])) { - foreach ($config['children'] as $name => $child) { - if (isset($child['children']) && !empty($child['children'])) { - $fieldsMap[$name] = array_keys($child['children']); + foreach ($config['children'] as $group => $node) { + $fieldsMap[$group] = []; + + foreach ($node['children'] as $childName => $childNode) { + if (!empty($childNode['children'])) { + // nodes need special handling + $fieldsMap[$group] = array_merge( + $fieldsMap[$group], + array_keys($childNode['children']) + ); + } else { + $fieldsMap[$group][] = $childName; } } + + // Remove empty groups + if (empty($fieldsMap[$group])) { + unset($fieldsMap[$group]); + } } return $fieldsMap; From d3e011ca213a94d631c7d9c1a0966225c289b4ec Mon Sep 17 00:00:00 2001 From: Dan Wallis Date: Mon, 27 Jul 2020 19:36:47 +0100 Subject: [PATCH 04/13] Skip disabled components --- app/code/Magento/Catalog/Model/Category/DataProvider.php | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/app/code/Magento/Catalog/Model/Category/DataProvider.php b/app/code/Magento/Catalog/Model/Category/DataProvider.php index 5d2dcf07f68bc..fe2fdaf53686b 100644 --- a/app/code/Magento/Catalog/Model/Category/DataProvider.php +++ b/app/code/Magento/Catalog/Model/Category/DataProvider.php @@ -666,6 +666,11 @@ protected function getFieldsMap() $fieldsMap = []; foreach ($config['children'] as $group => $node) { + // Skip disabled components (required for Commerce Edition) + if ($node['arguments']['data']['config']['componentDisabled'] ?? false) { + continue; + } + $fieldsMap[$group] = []; foreach ($node['children'] as $childName => $childNode) { From 8d899e19c5414414cf037fdfa27f9aed3e95d209 Mon Sep 17 00:00:00 2001 From: Dan Wallis Date: Mon, 27 Jul 2020 21:20:55 +0100 Subject: [PATCH 05/13] Avoid use of array_merge() to appease code linter --- app/code/Magento/Catalog/Model/Category/DataProvider.php | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/app/code/Magento/Catalog/Model/Category/DataProvider.php b/app/code/Magento/Catalog/Model/Category/DataProvider.php index fe2fdaf53686b..613205ee10e22 100644 --- a/app/code/Magento/Catalog/Model/Category/DataProvider.php +++ b/app/code/Magento/Catalog/Model/Category/DataProvider.php @@ -676,10 +676,9 @@ protected function getFieldsMap() foreach ($node['children'] as $childName => $childNode) { if (!empty($childNode['children'])) { // nodes need special handling - $fieldsMap[$group] = array_merge( - $fieldsMap[$group], - array_keys($childNode['children']) - ); + foreach ($childNode['children'] as $grandchildName => $grandchildNode) { + $fieldsMap[$group][] = $grandchildName; + } } else { $fieldsMap[$group][] = $childName; } From d62b0b5d36854065a420492684be0681793f3071 Mon Sep 17 00:00:00 2001 From: Dan Wallis Date: Mon, 27 Jul 2020 21:23:02 +0100 Subject: [PATCH 06/13] Declare missing property --- .../Catalog/Test/Unit/Model/Category/DataProviderTest.php | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/app/code/Magento/Catalog/Test/Unit/Model/Category/DataProviderTest.php b/app/code/Magento/Catalog/Test/Unit/Model/Category/DataProviderTest.php index b824929de0733..5b2334cd55f05 100644 --- a/app/code/Magento/Catalog/Test/Unit/Model/Category/DataProviderTest.php +++ b/app/code/Magento/Catalog/Test/Unit/Model/Category/DataProviderTest.php @@ -72,6 +72,11 @@ class DataProviderTest extends TestCase */ private $categoryFactory; + /** + * @var DataInterfaceFactory|MockObject + */ + private $uiConfigFactory; + /** * @var Collection|MockObject */ From fe28e234a488ad1ce8219241afb6e9cb16cd97a2 Mon Sep 17 00:00:00 2001 From: Dan Wallis Date: Wed, 26 Aug 2020 22:06:28 +0100 Subject: [PATCH 07/13] Refactor fields array population per code review --- .../Catalog/Model/Category/DataProvider.php | 15 +++++++-------- 1 file changed, 7 insertions(+), 8 deletions(-) diff --git a/app/code/Magento/Catalog/Model/Category/DataProvider.php b/app/code/Magento/Catalog/Model/Category/DataProvider.php index c8f21780cf6f7..1d4edec299717 100644 --- a/app/code/Magento/Catalog/Model/Category/DataProvider.php +++ b/app/code/Magento/Catalog/Model/Category/DataProvider.php @@ -22,6 +22,7 @@ use Magento\Eav\Model\Entity\Type; use Magento\Framework\App\ObjectManager; use Magento\Framework\App\RequestInterface; +use Magento\Framework\AuthorizationInterface; use Magento\Framework\Config\DataInterfaceFactory; use Magento\Framework\Exception\LocalizedException; use Magento\Framework\Exception\NoSuchEntityException; @@ -33,7 +34,6 @@ use Magento\Ui\Component\Form\Field; use Magento\Ui\DataProvider\EavValidationRules; use Magento\Ui\DataProvider\Modifier\PoolInterface; -use Magento\Framework\AuthorizationInterface; use Magento\Ui\DataProvider\ModifierPoolDataProvider; /** @@ -671,22 +671,21 @@ protected function getFieldsMap() continue; } - $fieldsMap[$group] = []; + $fields = []; foreach ($node['children'] as $childName => $childNode) { if (!empty($childNode['children'])) { // nodes need special handling - foreach ($childNode['children'] as $grandchildName => $grandchildNode) { - $fieldsMap[$group][] = $grandchildName; + foreach (array_keys($childNode['children']) as $grandchildName) { + $fields[] = $grandchildName; } } else { - $fieldsMap[$group][] = $childName; + $fields[] = $childName; } } - // Remove empty groups - if (empty($fieldsMap[$group])) { - unset($fieldsMap[$group]); + if (count($fields)) { + $fieldsMap[$group] = $fields; } } From 58651f999cc532e931b1a53077be6259737626fd Mon Sep 17 00:00:00 2001 From: Dan Wallis Date: Wed, 26 Aug 2020 23:16:07 +0100 Subject: [PATCH 08/13] Use optional argument in constructor --- app/code/Magento/Catalog/Model/Category/DataProvider.php | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/app/code/Magento/Catalog/Model/Category/DataProvider.php b/app/code/Magento/Catalog/Model/Category/DataProvider.php index 1d4edec299717..8efd5201639b2 100644 --- a/app/code/Magento/Catalog/Model/Category/DataProvider.php +++ b/app/code/Magento/Catalog/Model/Category/DataProvider.php @@ -222,7 +222,6 @@ public function __construct( Config $eavConfig, RequestInterface $request, CategoryFactory $categoryFactory, - DataInterfaceFactory $uiConfigFactory, array $meta = [], array $data = [], PoolInterface $pool = null, @@ -231,7 +230,8 @@ public function __construct( ScopeOverriddenValue $scopeOverriddenValue = null, ArrayManager $arrayManager = null, FileInfo $fileInfo = null, - ?Image $categoryImage = null + ?Image $categoryImage = null, + ?DataInterfaceFactory $uiConfigFactory = null ) { $this->eavValidationRules = $eavValidationRules; $this->collection = $categoryCollectionFactory->create(); @@ -241,7 +241,6 @@ public function __construct( $this->storeManager = $storeManager; $this->request = $request; $this->categoryFactory = $categoryFactory; - $this->uiConfigFactory = $uiConfigFactory; $this->auth = $auth ?? ObjectManager::getInstance()->get(AuthorizationInterface::class); $this->arrayUtils = $arrayUtils ?? ObjectManager::getInstance()->get(ArrayUtils::class); $this->scopeOverriddenValue = $scopeOverriddenValue ?: @@ -249,6 +248,10 @@ public function __construct( $this->arrayManager = $arrayManager ?: ObjectManager::getInstance()->get(ArrayManager::class); $this->fileInfo = $fileInfo ?: ObjectManager::getInstance()->get(FileInfo::class); $this->categoryImage = $categoryImage ?? ObjectManager::getInstance()->get(Image::class); + $this->uiConfigFactory = $uiConfigFactory ?? ObjectManager::getInstance()->create( + DataInterfaceFactory::class, + ['instanceName' => \Magento\Ui\Config\Data::class] + ); parent::__construct($name, $primaryFieldName, $requestFieldName, $meta, $data, $pool); } From d267e3bfae51f0210b275d76e9842c0597d3b96a Mon Sep 17 00:00:00 2001 From: Dan Wallis Date: Thu, 27 Aug 2020 00:00:42 +0100 Subject: [PATCH 09/13] Correct constructor docblock --- app/code/Magento/Catalog/Model/Category/DataProvider.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/code/Magento/Catalog/Model/Category/DataProvider.php b/app/code/Magento/Catalog/Model/Category/DataProvider.php index 8efd5201639b2..fe13b9c5e96a4 100644 --- a/app/code/Magento/Catalog/Model/Category/DataProvider.php +++ b/app/code/Magento/Catalog/Model/Category/DataProvider.php @@ -199,7 +199,6 @@ class DataProvider extends ModifierPoolDataProvider * @param Config $eavConfig * @param RequestInterface $request * @param CategoryFactory $categoryFactory - * @param DataInterfaceFactory $uiConfigFactory * @param array $meta * @param array $data * @param PoolInterface|null $pool @@ -209,6 +208,7 @@ class DataProvider extends ModifierPoolDataProvider * @param ArrayManager|null $arrayManager * @param FileInfo|null $fileInfo * @param Image|null $categoryImage + * @param DataInterfaceFactory|null $uiConfigFactory * @SuppressWarnings(PHPMD.ExcessiveParameterList) */ public function __construct( From e2c42e638f52b3937526b05ba6642f97fc213983 Mon Sep 17 00:00:00 2001 From: Dan Wallis Date: Thu, 27 Aug 2020 23:33:10 +0100 Subject: [PATCH 10/13] Fix out-of-scope PHPStan error --- app/code/Magento/Catalog/Model/Category/DataProvider.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/code/Magento/Catalog/Model/Category/DataProvider.php b/app/code/Magento/Catalog/Model/Category/DataProvider.php index fe13b9c5e96a4..80147260cd822 100644 --- a/app/code/Magento/Catalog/Model/Category/DataProvider.php +++ b/app/code/Magento/Catalog/Model/Category/DataProvider.php @@ -623,7 +623,7 @@ private function convertValues($category, $categoryData): array $categoryData[$attributeCode][0]['url'] = $this->categoryImage->getUrl($category, $attributeCode); - $categoryData[$attributeCode][0]['size'] = isset($stat) ? $stat['size'] : 0; + $categoryData[$attributeCode][0]['size'] = $stat['size']; $categoryData[$attributeCode][0]['type'] = $mime; } } From 749ee559a934ee0ac801645a00171099205bc5ac Mon Sep 17 00:00:00 2001 From: Dan Wallis Date: Thu, 27 Aug 2020 23:34:47 +0100 Subject: [PATCH 11/13] Replace count() with !empty() --- app/code/Magento/Catalog/Model/Category/DataProvider.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/code/Magento/Catalog/Model/Category/DataProvider.php b/app/code/Magento/Catalog/Model/Category/DataProvider.php index 80147260cd822..6a500c326b358 100644 --- a/app/code/Magento/Catalog/Model/Category/DataProvider.php +++ b/app/code/Magento/Catalog/Model/Category/DataProvider.php @@ -687,7 +687,7 @@ protected function getFieldsMap() } } - if (count($fields)) { + if (!empty($fields)) { $fieldsMap[$group] = $fields; } } From e59de414e184be5eee0608b94509193dcfe37963 Mon Sep 17 00:00:00 2001 From: Pavel Bystritsky Date: Tue, 22 Sep 2020 12:34:25 +0300 Subject: [PATCH 12/13] magento/magento2#13440 - Adding custom attribute to category doesn't show store specific value - integration test fix. --- .../Catalog/Block/Adminhtml/Category/Tab/AttributesTest.php | 3 +++ 1 file changed, 3 insertions(+) diff --git a/dev/tests/integration/testsuite/Magento/CatalogUrlRewrite/Plugin/Catalog/Block/Adminhtml/Category/Tab/AttributesTest.php b/dev/tests/integration/testsuite/Magento/CatalogUrlRewrite/Plugin/Catalog/Block/Adminhtml/Category/Tab/AttributesTest.php index e2f8a2d5e4c21..3f9ab815038cc 100644 --- a/dev/tests/integration/testsuite/Magento/CatalogUrlRewrite/Plugin/Catalog/Block/Adminhtml/Category/Tab/AttributesTest.php +++ b/dev/tests/integration/testsuite/Magento/CatalogUrlRewrite/Plugin/Catalog/Block/Adminhtml/Category/Tab/AttributesTest.php @@ -9,6 +9,9 @@ use Magento\Eav\Model\Config as EavConfig; use Magento\TestFramework\Helper\Bootstrap; +/** + * @magentoAppArea adminhtml + */ class AttributesTest extends \PHPUnit\Framework\TestCase { /** From cebb5becc68549a26f7c404b471e0f6b95f66a84 Mon Sep 17 00:00:00 2001 From: Pavel Bystritsky Date: Tue, 22 Sep 2020 17:25:34 +0300 Subject: [PATCH 13/13] magento/magento2#13440 - Adding custom attribute to category doesn't show store specific value - SVC test fix. --- app/code/Magento/Catalog/Model/Category/DataProvider.php | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/app/code/Magento/Catalog/Model/Category/DataProvider.php b/app/code/Magento/Catalog/Model/Category/DataProvider.php index 6a500c326b358..f3e3caf309059 100644 --- a/app/code/Magento/Catalog/Model/Category/DataProvider.php +++ b/app/code/Magento/Catalog/Model/Category/DataProvider.php @@ -157,7 +157,7 @@ class DataProvider extends ModifierPoolDataProvider /** * @var DataInterfaceFactory */ - protected $uiConfigFactory; + private $uiConfigFactory; /** * @var ScopeOverriddenValue @@ -183,6 +183,7 @@ class DataProvider extends ModifierPoolDataProvider * @var AuthorizationInterface */ private $auth; + /** * @var Image */