From 12b7e5ce1cb8c7407f083cd42c5e7333e9d648e2 Mon Sep 17 00:00:00 2001 From: Jasper Zeinstra Date: Wed, 21 Apr 2021 10:14:29 +0200 Subject: [PATCH] Add 'Use default value' checkbox to category attributes --- Model/Category/DataProvider/Plugin.php | 102 +++++++++++++++++++++++++ etc/di.xml | 4 + 2 files changed, 106 insertions(+) create mode 100644 Model/Category/DataProvider/Plugin.php diff --git a/Model/Category/DataProvider/Plugin.php b/Model/Category/DataProvider/Plugin.php new file mode 100644 index 00000000..244fc00d --- /dev/null +++ b/Model/Category/DataProvider/Plugin.php @@ -0,0 +1,102 @@ +eavConfig = $eavConfig; + $this->auth = $auth ?? ObjectManager::getInstance()->get(AuthorizationInterface::class); + } + + /** + * @param CategoryDataProvider $subject + * @param array $meta + * + * @return array + * + * @throws LocalizedException + */ + public function afterPrepareMeta(CategoryDataProvider $subject, $meta) + { + $meta = array_replace_recursive( + $meta, + $this->prepareFieldsMeta( + $this->getFieldsMap(), + $subject->getAttributesMeta($this->eavConfig->getEntityType('catalog_category')) + ) + ); + + return $meta; + } + + /** + * @param array $fieldsMap + * @param array $fieldsMeta + * + * @return array + */ + private function prepareFieldsMeta($fieldsMap, $fieldsMeta) + { + $canEditDesign = $this->auth->isAllowed('Magento_Catalog::edit_category_design'); + + $result = []; + foreach ($fieldsMap as $fieldSet => $fields) { + foreach ($fields as $field) { + if (isset($fieldsMeta[$field])) { + $config = $fieldsMeta[$field]; + if (($fieldSet === 'design' || $fieldSet === 'schedule_design_update') && !$canEditDesign) { + $config['required'] = 1; + $config['disabled'] = 1; + $config['serviceDisabled'] = true; + } + + $result[$fieldSet]['children'][$field]['arguments']['data']['config'] = $config; + } + } + } + + return $result; + } + + /** + * @return array + */ + protected function getFieldsMap() + { + return [ + 'tweakwise' => [ + 'tweakwise_featured_template', + 'tweakwise_crosssell_template', + 'tweakwise_crosssell_group_code', + 'tweakwise_upsell_template', + 'tweakwise_upsell_group_code', + ], + ]; + } +} diff --git a/etc/di.xml b/etc/di.xml index 193a8ee2..5a688be4 100644 --- a/etc/di.xml +++ b/etc/di.xml @@ -66,6 +66,10 @@ + + + +