diff --git a/app/code/Magento/Catalog/Block/Adminhtml/Product/Attribute/Edit/Tab/Front.php b/app/code/Magento/Catalog/Block/Adminhtml/Product/Attribute/Edit/Tab/Front.php index 1f146dcbe254a..837e2ac4f8107 100644 --- a/app/code/Magento/Catalog/Block/Adminhtml/Product/Attribute/Edit/Tab/Front.php +++ b/app/code/Magento/Catalog/Block/Adminhtml/Product/Attribute/Edit/Tab/Front.php @@ -11,10 +11,14 @@ */ namespace Magento\Catalog\Block\Adminhtml\Product\Attribute\Edit\Tab; +use Magento\Backend\Block\Template\Context; use Magento\Backend\Block\Widget\Form; use Magento\Backend\Block\Widget\Form\Generic; use Magento\Config\Model\Config\Source\Yesno; use Magento\Catalog\Model\Entity\Attribute; +use Magento\Eav\Block\Adminhtml\Attribute\PropertyLocker; +use Magento\Framework\Data\FormFactory; +use Magento\Framework\Registry; class Front extends Generic { @@ -24,28 +28,28 @@ class Front extends Generic protected $_yesNo; /** - * @var array + * @var PropertyLocker */ - private $disableSearchable; + private $propertyLocker; /** - * @param \Magento\Backend\Block\Template\Context $context - * @param \Magento\Framework\Registry $registry - * @param \Magento\Framework\Data\FormFactory $formFactory + * @param Context $context + * @param Registry $registry + * @param FormFactory $formFactory * @param Yesno $yesNo + * @param PropertyLocker $propertyLocker * @param array $data - * @param array $disableSearchable */ public function __construct( - \Magento\Backend\Block\Template\Context $context, - \Magento\Framework\Registry $registry, - \Magento\Framework\Data\FormFactory $formFactory, + Context $context, + Registry $registry, + FormFactory $formFactory, Yesno $yesNo, - array $data = [], - array $disableSearchable = [] + PropertyLocker $propertyLocker, + array $data = [] ) { $this->_yesNo = $yesNo; - $this->disableSearchable = $disableSearchable; + $this->propertyLocker = $propertyLocker; parent::__construct($context, $registry, $formFactory, $data); } @@ -71,7 +75,6 @@ protected function _prepareForm() ['legend' => __('Frontend Properties'), 'collapsable' => $this->getRequest()->has('popup')] ); - $attrCode = $attributeObject->getAttributeCode(); $fieldset->addField( 'is_searchable', 'select', @@ -80,7 +83,6 @@ protected function _prepareForm() 'label' => __('Use in Search'), 'title' => __('Use in Search'), 'values' => $yesnoSource, - 'disabled' => isset($this->disableSearchable[$attrCode]) && $this->disableSearchable[$attrCode] ? 1 : 0 ] ); @@ -223,6 +225,7 @@ protected function _prepareForm() ); $form->setValues($attributeObject->getData()); + $this->propertyLocker->lock($form); $this->setForm($form); return parent::_prepareForm(); } diff --git a/app/code/Magento/Catalog/Block/Adminhtml/Product/Attribute/Edit/Tab/Main.php b/app/code/Magento/Catalog/Block/Adminhtml/Product/Attribute/Edit/Tab/Main.php index 35155d15e785a..343e9f60273c2 100644 --- a/app/code/Magento/Catalog/Block/Adminhtml/Product/Attribute/Edit/Tab/Main.php +++ b/app/code/Magento/Catalog/Block/Adminhtml/Product/Attribute/Edit/Tab/Main.php @@ -61,7 +61,6 @@ protected function _prepareForm() $response = new \Magento\Framework\Object(); $response->setTypes([]); $this->_eventManager->dispatch('adminhtml_product_attribute_types', ['response' => $response]); - $_disabledTypes = []; $_hiddenFields = []; foreach ($response->getTypes() as $type) { $additionalTypes[] = $type; diff --git a/app/code/Magento/Catalog/Model/Product/Attribute/Source/Inputtype.php b/app/code/Magento/Catalog/Model/Product/Attribute/Source/Inputtype.php index 769a2aeb5a832..7573262af0520 100644 --- a/app/code/Magento/Catalog/Model/Product/Attribute/Source/Inputtype.php +++ b/app/code/Magento/Catalog/Model/Product/Attribute/Source/Inputtype.php @@ -53,7 +53,6 @@ public function toOptionArray() $response = new \Magento\Framework\Object(); $response->setTypes([]); $this->_eventManager->dispatch('adminhtml_product_attribute_types', ['response' => $response]); - $_disabledTypes = []; $_hiddenFields = []; foreach ($response->getTypes() as $type) { $inputTypes[] = $type; diff --git a/app/code/Magento/Catalog/etc/adminhtml/di.xml b/app/code/Magento/Catalog/etc/adminhtml/di.xml index 31e2f1092287e..29aadc03ae11e 100644 --- a/app/code/Magento/Catalog/etc/adminhtml/di.xml +++ b/app/code/Magento/Catalog/etc/adminhtml/di.xml @@ -66,18 +66,4 @@ - - - - true - true - true - true - true - true - true - true - - - diff --git a/app/code/Magento/Catalog/etc/eav_attributes.xml b/app/code/Magento/Catalog/etc/eav_attributes.xml index a7972b6b53113..3ba4dfae9d8b0 100644 --- a/app/code/Magento/Catalog/etc/eav_attributes.xml +++ b/app/code/Magento/Catalog/etc/eav_attributes.xml @@ -12,12 +12,31 @@ + + + + + + + + + + + + + + + + + + + diff --git a/app/code/Magento/CatalogUrlRewrite/etc/eav_attributes.xml b/app/code/Magento/CatalogUrlRewrite/etc/eav_attributes.xml index 65818ad4546d1..076ffe0382866 100644 --- a/app/code/Magento/CatalogUrlRewrite/etc/eav_attributes.xml +++ b/app/code/Magento/CatalogUrlRewrite/etc/eav_attributes.xml @@ -9,6 +9,7 @@ + diff --git a/app/code/Magento/Eav/Block/Adminhtml/Attribute/Edit/Main/AbstractMain.php b/app/code/Magento/Eav/Block/Adminhtml/Attribute/Edit/Main/AbstractMain.php index 597b4f563dd27..0bff79e95a10f 100644 --- a/app/code/Magento/Eav/Block/Adminhtml/Attribute/Edit/Main/AbstractMain.php +++ b/app/code/Magento/Eav/Block/Adminhtml/Attribute/Edit/Main/AbstractMain.php @@ -32,9 +32,9 @@ abstract class AbstractMain extends \Magento\Backend\Block\Widget\Form\Generic protected $_eavData = null; /** - * @var \Magento\Eav\Model\Entity\Attribute\Config + * @var \Magento\Eav\Block\Adminhtml\Attribute\PropertyLocker */ - protected $_attributeConfig; + protected $propertyLocker; /** * @var \Magento\Config\Model\Config\Source\YesnoFactory @@ -53,7 +53,7 @@ abstract class AbstractMain extends \Magento\Backend\Block\Widget\Form\Generic * @param \Magento\Eav\Helper\Data $eavData * @param \Magento\Config\Model\Config\Source\YesnoFactory $yesnoFactory * @param \Magento\Eav\Model\Adminhtml\System\Config\Source\InputtypeFactory $inputTypeFactory - * @param \Magento\Eav\Model\Entity\Attribute\Config $attributeConfig + * @param \Magento\Eav\Block\Adminhtml\Attribute\PropertyLocker $propertyLocker * @param array $data */ public function __construct( @@ -63,13 +63,13 @@ public function __construct( \Magento\Eav\Helper\Data $eavData, \Magento\Config\Model\Config\Source\YesnoFactory $yesnoFactory, \Magento\Eav\Model\Adminhtml\System\Config\Source\InputtypeFactory $inputTypeFactory, - \Magento\Eav\Model\Entity\Attribute\Config $attributeConfig, + \Magento\Eav\Block\Adminhtml\Attribute\PropertyLocker $propertyLocker, array $data = [] ) { $this->_eavData = $eavData; $this->_yesnoFactory = $yesnoFactory; $this->_inputTypeFactory = $inputTypeFactory; - $this->_attributeConfig = $attributeConfig; + $this->propertyLocker = $propertyLocker; parent::__construct($context, $registry, $formFactory, $data); } @@ -257,6 +257,7 @@ protected function _prepareForm() } } + $this->propertyLocker->lock($form); $this->setForm($form); return parent::_prepareForm(); @@ -277,27 +278,6 @@ protected function _initFormValues() return parent::_initFormValues(); } - /** - * This method is called before rendering HTML - * - * @return $this - */ - protected function _beforeToHtml() - { - parent::_beforeToHtml(); - $attributeObject = $this->getAttributeObject(); - if ($attributeObject->getId()) { - $form = $this->getForm(); - foreach ($this->_attributeConfig->getLockedFields($attributeObject) as $field) { - if ($element = $form->getElement($field)) { - $element->setDisabled(1); - $element->setReadonly(1); - } - } - } - return $this; - } - /** * Processing block html after rendering * Adding js block to the end of this block diff --git a/app/code/Magento/Eav/Block/Adminhtml/Attribute/PropertyLocker.php b/app/code/Magento/Eav/Block/Adminhtml/Attribute/PropertyLocker.php new file mode 100644 index 0000000000000..ccaab44afee61 --- /dev/null +++ b/app/code/Magento/Eav/Block/Adminhtml/Attribute/PropertyLocker.php @@ -0,0 +1,57 @@ + + */ +class PropertyLocker +{ + /** + * @var Config + */ + private $attributeConfig; + + /** + * @var Registry + */ + protected $registry; + + /** + * @param Registry $registry + * @param Config $attributeConfig + */ + public function __construct( + Registry $registry, + Config $attributeConfig + ) { + $this->registry = $registry; + $this->attributeConfig = $attributeConfig; + } + + /** + * @param \Magento\Framework\Data\Form $form + * @return void + */ + public function lock(\Magento\Framework\Data\Form $form) + { + /** @var \Magento\Eav\Model\Entity\Attribute\AbstractAttribute $attributeObject */ + $attributeObject = $this->registry->registry('entity_attribute'); + if ($attributeObject->getId()) { + foreach ($this->attributeConfig->getLockedFields($attributeObject) as $field) { + if ($element = $form->getElement($field)) { + $element->setDisabled(1); + $element->setReadonly(1); + } + } + } + } +} diff --git a/app/code/Magento/Eav/Test/Unit/Block/Adminhtml/Attribute/PropertyLockerTest.php b/app/code/Magento/Eav/Test/Unit/Block/Adminhtml/Attribute/PropertyLockerTest.php new file mode 100644 index 0000000000000..cf0615c42a7d0 --- /dev/null +++ b/app/code/Magento/Eav/Test/Unit/Block/Adminhtml/Attribute/PropertyLockerTest.php @@ -0,0 +1,71 @@ +attributeMock = $this->getMockBuilder('Magento\Eav\Model\Entity\Attribute\AbstractAttribute') + ->setMethods(['getId']) + ->disableOriginalConstructor() + ->getMock(); + + $registryMock = $this->getMockBuilder('\Magento\Framework\Registry') + ->setMethods(['registry']) + ->disableOriginalConstructor() + ->getMock(); + $registryMock->expects($this->atLeastOnce())->method('registry')->willReturn($this->attributeMock); + + $this->attributeConfigMock = $this->getMockBuilder('Magento\Eav\Model\Entity\Attribute\Config') + ->setMethods(['getLockedFields']) + ->disableOriginalConstructor() + ->getMock(); + + $this->formMock = $this->getMockBuilder('Magento\Framework\Data\Form') + ->setMethods(['getElement']) + ->disableOriginalConstructor() + ->getMock(); + + $this->object = new PropertyLocker($registryMock, $this->attributeConfigMock); + } + + /** + * @covers \Magento\Eav\Block\Adminhtml\Attribute\PropertyLocker::lock + */ + public function testLock() + { + $lockedFields = [ + 'is_searchable' => 'is_searchable', + 'is_filterable' => 'is_filterable' + ]; + $this->attributeMock->expects($this->once())->method('getId')->willReturn(1); + $this->attributeConfigMock->expects($this->once())->method('getLockedFields')->willReturn($lockedFields); + + $elementMock = $this->getMockBuilder('\Magento\Framework\Data\Form\Element\AbstractElement') + ->setMethods(['setDisabled', 'setReadonly']) + ->disableOriginalConstructor() + ->getMockForAbstractClass(); + $elementMock->expects($this->exactly(2))->method('setDisabled'); + $elementMock->expects($this->exactly(2))->method('setReadonly'); + $this->formMock->expects($this->exactly(2))->method('getElement')->willReturn($elementMock); + $this->object->lock($this->formMock); + } +} diff --git a/dev/tests/integration/testsuite/Magento/Catalog/Block/Adminhtml/Product/Attribute/Edit/Tab/FrontTest.php b/dev/tests/integration/testsuite/Magento/Catalog/Block/Adminhtml/Product/Attribute/Edit/Tab/FrontTest.php index 631738f29155b..97fae046b884b 100644 --- a/dev/tests/integration/testsuite/Magento/Catalog/Block/Adminhtml/Product/Attribute/Edit/Tab/FrontTest.php +++ b/dev/tests/integration/testsuite/Magento/Catalog/Block/Adminhtml/Product/Attribute/Edit/Tab/FrontTest.php @@ -37,7 +37,7 @@ public function testToHtml($attributeCode) { /** @var \Magento\Catalog\Model\Resource\Eav\Attribute $model */ $model = $this->objectManager->create('Magento\Catalog\Model\Resource\Eav\Attribute'); - $model->load($attributeCode, 'attribute_code'); + $model->loadByCode(\Magento\Catalog\Model\Product::ENTITY, $attributeCode); /** @var \Magento\Framework\Registry $coreRegistry */ $coreRegistry = $this->objectManager->get('\Magento\Framework\Registry');