diff --git a/vendor/magento/module-catalog/Model/Product/Attribute/AttributeSetUnassignValidator.php b/vendor/magento/module-catalog/Model/Product/Attribute/AttributeSetUnassignValidator.php new file mode 100644 index 0000000000000..783602b09beef --- /dev/null +++ b/vendor/magento/module-catalog/Model/Product/Attribute/AttributeSetUnassignValidator.php @@ -0,0 +1,43 @@ +unassignable)) { + $this->unassignable = $this->attributeConfig->getAttributeNames('unassignable'); + } + if (in_array($attribute->getAttributeCode(), $this->unassignable)) { + throw new LocalizedException( + __("The system attribute can't be deleted.") + ); + } + } +} diff --git a/vendor/magento/module-catalog/Model/Product/Attribute/AttributeSetUnassignValidatorInterface.php b/vendor/magento/module-catalog/Model/Product/Attribute/AttributeSetUnassignValidatorInterface.php new file mode 100644 index 0000000000000..a50935f67f364 --- /dev/null +++ b/vendor/magento/module-catalog/Model/Product/Attribute/AttributeSetUnassignValidatorInterface.php @@ -0,0 +1,28 @@ +_attributeCollectionFactory = $attributeCollectionFactory; + $this->attributeSetUnassignValidator = $attributeSetUnassignValidator + ?: ObjectManager::getInstance()->get(AttributeSetUnassignValidatorInterface::class); parent::__construct( $context, $registry, @@ -72,4 +84,24 @@ public function hasSystemAttributes() } return $result; } + + /** + * @inheritdoc + */ + public function beforeDelete() + { + $attributesCollection = $this->_attributeCollectionFactory->create(); + $attributesCollection->setAttributeGroupFilter($this->getId()); + foreach ($attributesCollection as $attribute) { + try { + $this->attributeSetUnassignValidator->validate($attribute, (int) $attribute->getAttributeSetId()); + } catch (LocalizedException $e) { + throw new LocalizedException( + __("This group contains system attributes." . + " Please move system attributes to another group and try again.") + ); + } + } + return parent::beforeDelete(); + } } diff --git a/vendor/magento/module-catalog/Model/ResourceModel/Eav/Attribute.php b/vendor/magento/module-catalog/Model/ResourceModel/Eav/Attribute.php index 07ce84c7cd62e..403cfec206eb6 100644 --- a/vendor/magento/module-catalog/Model/ResourceModel/Eav/Attribute.php +++ b/vendor/magento/module-catalog/Model/ResourceModel/Eav/Attribute.php @@ -8,8 +8,10 @@ use Magento\Catalog\Model\Attribute\Backend\DefaultBackend; use Magento\Catalog\Model\Attribute\LockValidatorInterface; +use Magento\Catalog\Model\Product\Attribute\AttributeSetUnassignValidatorInterface; use Magento\Eav\Model\Entity; use Magento\Framework\Api\AttributeValueFactory; +use Magento\Framework\App\ObjectManager; use Magento\Framework\Stdlib\DateTime\DateTimeFormatterInterface; /** @@ -98,6 +100,11 @@ class Attribute extends \Magento\Eav\Model\Entity\Attribute implements */ private $eavAttributeFactory; + /** + * @var AttributeSetUnassignValidatorInterface + */ + private $attributeSetUnassignValidator; + /** * @param \Magento\Framework\Model\Context $context * @param \Magento\Framework\Registry $registry @@ -123,6 +130,7 @@ class Attribute extends \Magento\Eav\Model\Entity\Attribute implements * @param \Magento\Framework\Data\Collection\AbstractDb|null $resourceCollection * @param array $data * @param \Magento\Eav\Api\Data\AttributeExtensionFactory|null $eavAttributeFactory + * @param AttributeSetUnassignValidatorInterface|null $attributeSetUnassignValidator * @SuppressWarnings(PHPMD.ExcessiveParameterList) */ public function __construct( @@ -149,7 +157,8 @@ public function __construct( \Magento\Framework\Model\ResourceModel\AbstractResource $resource = null, \Magento\Framework\Data\Collection\AbstractDb $resourceCollection = null, array $data = [], - \Magento\Eav\Api\Data\AttributeExtensionFactory $eavAttributeFactory = null + ?\Magento\Eav\Api\Data\AttributeExtensionFactory $eavAttributeFactory = null, + ?AttributeSetUnassignValidatorInterface $attributeSetUnassignValidator = null ) { $this->_indexerEavProcessor = $indexerEavProcessor; $this->_productFlatIndexerProcessor = $productFlatIndexerProcessor; @@ -157,6 +166,8 @@ public function __construct( $this->attrLockValidator = $lockValidator; $this->eavAttributeFactory = $eavAttributeFactory ?: \Magento\Framework\App\ObjectManager::getInstance() ->get(\Magento\Eav\Api\Data\AttributeExtensionFactory::class); + $this->attributeSetUnassignValidator = $attributeSetUnassignValidator + ?: ObjectManager::getInstance()->get(AttributeSetUnassignValidatorInterface::class); parent::__construct( $context, $registry, @@ -917,4 +928,16 @@ protected function _getDefaultBackendModel() return $backend; } + + /** + * @inheritdoc + */ + public function deleteEntity() + { + if ($this->getEntityAttributeId()) { + $result = $this->_getResource()->getEntityAttribute($this->getEntityAttributeId()); + $result && $this->attributeSetUnassignValidator->validate($this, (int) $result['attribute_set_id']); + } + return parent::deleteEntity(); + } } diff --git a/vendor/magento/module-catalog/etc/di.xml b/vendor/magento/module-catalog/etc/di.xml index a13175fa78e90..df36733051c0d 100644 --- a/vendor/magento/module-catalog/etc/di.xml +++ b/vendor/magento/module-catalog/etc/di.xml @@ -78,6 +78,7 @@ + @@ -1351,4 +1352,24 @@ + + + Magento\Catalog\Model\Product\Attribute\Group + + + + + Magento\Catalog\Virtual\Product\Attribute\GroupFactory + + + + + Magento\Catalog\Virtual\Product\Attribute\Set + + + + + Magento\Catalog\Virtual\Product\Attribute\SetFactory + + diff --git a/vendor/magento/module-catalog/view/adminhtml/templates/catalog/product/attribute/set/main.phtml b/vendor/magento/module-catalog/view/adminhtml/templates/catalog/product/attribute/set/main.phtml index 03b0a38ca97d0..0a84b7cb247ec 100644 --- a/vendor/magento/module-catalog/view/adminhtml/templates/catalog/product/attribute/set/main.phtml +++ b/vendor/magento/module-catalog/view/adminhtml/templates/catalog/product/attribute/set/main.phtml @@ -1,11 +1,12 @@
@@ -33,12 +34,12 @@ script;
- escapeHtml(__('Groups')) ?> + escapeHtml(__('Groups')) ?>
getIsReadOnly()):?> getAddGroupButton() ?>  getDeleteGroupButton() ?> -

escapeHtml(__('Double click on a group to rename it.')) ?>

+

escapeHtml(__('Double click on a group to rename it.')) ?>

getSetsFilterHtml() ?> @@ -46,462 +47,109 @@ script;
- escapeHtml(__('Unassigned Attributes')) ?> + escapeHtml(__('Unassigned Attributes')) ?>
- getIsReadOnly() ? 'false' : 'true'); + getIsReadOnly() ? 'true' : 'false'; + $saveUrl = $escaper->escapeJs($block->getMoveUrl()); $groupTree = /* @noEscape */ $block->getGroupTreeJson(); $attributeTreeJson = /* @noEscape */ $block->getAttributeTreeJson(); - $systemAttributeWarning = $block->escapeJs( + $newGroupModalTitle = $escaper->escapeJs($escaper->escapeHtml(__('Add New Group'))); + $newGroupModalContent = $escaper->escapeJs($escaper->escapeHtml(__('Please enter a new group name.'))); + $cannotSaveAlert = $escaper->escapeJs(__('Sorry, we\'re unable to complete this request.')); + $cannotUnassignAttributeAlert = $escaper->escapeJs(__('You can\'t remove attributes from this attribute set.')); + $cannotDeleteGroupAlert = $escaper->escapeJs( __('This group contains system attributes. Please move system attributes to another group and try again.') ); - $scriptString = <<