Skip to content
This repository has been archived by the owner on Oct 19, 2022. It is now read-only.

Commit

Permalink
Merge pull request #154 from jasperzeinstra/feature/add-default-value
Browse files Browse the repository at this point in the history
Add 'Use default value' checkbox to category attributes
  • Loading branch information
Hnto authored Apr 28, 2021
2 parents ababc9d + 12b7e5c commit f9dc58f
Show file tree
Hide file tree
Showing 2 changed files with 106 additions and 0 deletions.
102 changes: 102 additions & 0 deletions Model/Category/DataProvider/Plugin.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
<?php

namespace Emico\Tweakwise\Model\Category\DataProvider;

use Magento\Catalog\Model\Category\DataProvider as CategoryDataProvider;
use Magento\Eav\Model\Config;
use Magento\Framework\App\ObjectManager;
use Magento\Framework\AuthorizationInterface;
use Magento\Framework\Exception\LocalizedException;

class Plugin
{
/**
* @var Config
*/
protected $eavConfig;

/**
* @var AuthorizationInterface
*/
private $auth;

/**
* DataProvider constructor.
*
* @param Config $eavConfig
* @param AuthorizationInterface|null $auth
*/
public function __construct(
Config $eavConfig,
?AuthorizationInterface $auth = null
) {
$this->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',
],
];
}
}
4 changes: 4 additions & 0 deletions etc/di.xml
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,10 @@
<type name="Magento\Framework\View\Page\Config">
<plugin name="emico-tweakwise" type="Emico\Tweakwise\Model\Seo\Robots\Plugin" />
</type>
<!-- Add meta of custom fields to data provider -->
<type name="Magento\Catalog\Model\Category\DataProvider">
<plugin name="emico-tweakwise" type="Emico\Tweakwise\Model\Category\DataProvider\Plugin" />
</type>

<!-- If you need other layouts or possible even layer objects register them here -->
<type name="Emico\Tweakwise\Controller\Ajax\Navigation">
Expand Down

0 comments on commit f9dc58f

Please sign in to comment.