Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix #21025 - Missing "Apply To" when editing Product attributes #27625

Open
wants to merge 12 commits into
base: 2.4-develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
Fix #21025 - Missing "Apply To" when editing Product attributes
  • Loading branch information
Bartlomiejsz committed Apr 5, 2020
commit d143eb2ad72de6586806eb5bf27526e946e00a57
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,18 @@

namespace Magento\Catalog\Block\Adminhtml\Product\Attribute\Edit\Tab;

use Magento\Backend\Block\Template\Context;
use Magento\Backend\Block\Widget\Form\Generic;
use Magento\Catalog\Model\Attribute\Source\ApplyTo;
use Magento\Catalog\Model\ResourceModel\Eav\Attribute;
use Magento\Config\Model\Config\Source\Yesno;
use Magento\Eav\Block\Adminhtml\Attribute\PropertyLocker;
use Magento\Eav\Helper\Data;
use Magento\Eav\Model\Entity\Attribute\ScopedAttributeInterface;
use Magento\Framework\App\ObjectManager;
use Magento\Framework\Data\FormFactory;
use Magento\Framework\Exception\LocalizedException;
use Magento\Framework\Registry;
use Magento\Framework\Stdlib\DateTime;

/**
Expand All @@ -29,7 +34,7 @@ class Advanced extends Generic
*
* @var Data
*/
protected $_eavData = null;
protected $_eavData;

/**
* @var Yesno
Expand All @@ -47,27 +52,38 @@ class Advanced extends Generic
private $propertyLocker;

/**
* @param \Magento\Backend\Block\Template\Context $context
* @param \Magento\Framework\Registry $registry
* @param \Magento\Framework\Data\FormFactory $formFactory
* @var ApplyTo
*/
private $applyTo;

/**
* @param Context $context
* @param Registry $registry
* @param FormFactory $formFactory
* @param Yesno $yesNo
* @param Data $eavData
* @param array $disableScopeChangeList
* @param array $data
* @param PropertyLocker|null $propertyLocker
* @param ApplyTo|null $applyTo
*/
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,
Data $eavData,
array $disableScopeChangeList = ['sku'],
array $data = []
array $data = [],
?PropertyLocker $propertyLocker = null,
?ApplyTo $applyTo = null
) {
$this->_yesNo = $yesNo;
$this->_eavData = $eavData;
$this->disableScopeChangeList = $disableScopeChangeList;
parent::__construct($context, $registry, $formFactory, $data);
$this->propertyLocker = $propertyLocker ?? ObjectManager::getInstance()->get(PropertyLocker::class);
$this->applyTo = $applyTo ?? ObjectManager::getInstance()->get(ApplyTo::class);
}

/**
Expand Down Expand Up @@ -230,22 +246,35 @@ protected function _prepareForm()
]
);

$fieldset->addField(
'apply_to',
'multiselect',
[
'name' => 'apply_to',
'label' => __('Apply To'),
'title' => __('Apply To'),
'values' => $this->applyTo->toOptionArray(),
'value' => $attributeObject->getApplyTo()
]
);

if ($attributeObject->getId()) {
$form->getElement('attribute_code')->setDisabled(1);
if (!$attributeObject->getIsUserDefined()) {
$form->getElement('is_unique')->setDisabled(1);
$form->getElement('apply_to')->setDisabled(1);
}
}

$scopes = [
\Magento\Eav\Model\Entity\Attribute\ScopedAttributeInterface::SCOPE_STORE => __('Store View'),
\Magento\Eav\Model\Entity\Attribute\ScopedAttributeInterface::SCOPE_WEBSITE => __('Website'),
\Magento\Eav\Model\Entity\Attribute\ScopedAttributeInterface::SCOPE_GLOBAL => __('Global'),
ScopedAttributeInterface::SCOPE_STORE => __('Store View'),
ScopedAttributeInterface::SCOPE_WEBSITE => __('Website'),
ScopedAttributeInterface::SCOPE_GLOBAL => __('Global'),
];

if ($attributeObject->getAttributeCode() == 'status' || $attributeObject->getAttributeCode() == 'tax_class_id'
) {
unset($scopes[\Magento\Eav\Model\Entity\Attribute\ScopedAttributeInterface::SCOPE_STORE]);
unset($scopes[ScopedAttributeInterface::SCOPE_STORE]);
}

$fieldset->addField(
Expand All @@ -266,7 +295,7 @@ protected function _prepareForm()
$form->getElement('is_global')->setDisabled(1);
}
$this->setForm($form);
$this->getPropertyLocker()->lock($form);
$this->propertyLocker->lock($form);
return $this;
}

Expand All @@ -291,19 +320,6 @@ private function getAttributeObject()
return $this->_coreRegistry->registry('entity_attribute');
}

/**
* Get property locker
*
* @return PropertyLocker
*/
private function getPropertyLocker()
{
if (null === $this->propertyLocker) {
$this->propertyLocker = ObjectManager::getInstance()->get(PropertyLocker::class);
}
return $this->propertyLocker;
}

/**
* Get localized date default value
*
Expand Down
Original file line number Diff line number Diff line change
@@ -1,26 +1,25 @@
<?php
/**
*
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);

namespace Magento\Catalog\Controller\Adminhtml\Product\Attribute;

use Magento\Framework\App\Action\HttpPostActionInterface as HttpPostActionInterface;
use Magento\Backend\App\Action\Context;
use Magento\Backend\Model\View\Result\Redirect;
use Magento\Catalog\Api\Data\ProductAttributeInterface;
use Magento\Catalog\Controller\Adminhtml\Product\Attribute;
use Magento\Catalog\Helper\Product;
use Magento\Catalog\Model\Product\Attribute\Frontend\Inputtype\Presentation;
use Magento\Framework\Serialize\Serializer\FormData;
use Magento\Catalog\Model\Product\AttributeSet\BuildFactory;
use Magento\Catalog\Model\ResourceModel\Eav\AttributeFactory;
use Magento\Eav\Model\Adminhtml\System\Config\Source\Inputtype\Validator;
use Magento\Eav\Model\Adminhtml\System\Config\Source\Inputtype\ValidatorFactory;
use Magento\Eav\Model\Entity\Attribute\Set;
use Magento\Eav\Model\ResourceModel\Entity\Attribute\Group\CollectionFactory;
use Magento\Framework\App\Action\HttpPostActionInterface;
use Magento\Framework\App\ObjectManager;
use Magento\Framework\Cache\FrontendInterface;
use Magento\Framework\Controller\Result\Json;
Expand All @@ -29,6 +28,7 @@
use Magento\Framework\Exception\LocalizedException;
use Magento\Framework\Filter\FilterManager;
use Magento\Framework\Registry;
use Magento\Framework\Serialize\Serializer\FormData;
use Magento\Framework\View\LayoutFactory;
use Magento\Framework\View\Result\PageFactory;

Expand Down Expand Up @@ -256,6 +256,8 @@ public function execute()
if (!$model->getIsUserDefined() && $model->getId()) {
// Unset attribute field for system attributes
unset($data['apply_to']);
} elseif (!isset($data['apply_to'])) {
$data['apply_to'] = [];
}

$model->addData($data);
Expand Down
45 changes: 45 additions & 0 deletions app/code/Magento/Catalog/Model/Attribute/Source/ApplyTo.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);

namespace Magento\Catalog\Model\Attribute\Source;

use Magento\Catalog\Api\ProductTypeListInterface;
use Magento\Framework\Data\OptionSourceInterface;

class ApplyTo implements OptionSourceInterface
{
/**
* @var ProductTypeListInterface
*/
private $productTypeList;

/**
* @param ProductTypeListInterface $productTypeList
*/
public function __construct(
ProductTypeListInterface $productTypeList
) {
$this->productTypeList = $productTypeList;
}

/**
* @inheritDoc
*/
public function toOptionArray()
{
$result = [];

foreach ($this->productTypeList->getProductTypes() as $productType) {
$result[] = [
'value' => $productType->getName(),
'label' => $productType->getLabel()
];
}

return $result;
}
}
Loading