Skip to content

Commit 6836d4a

Browse files
authored
ENGCOM-3519: Fix for not added attribute, while added it dynamically from product form #18206
2 parents fd90f65 + dfcd77b commit 6836d4a

File tree

1 file changed

+54
-32
lines changed

1 file changed

+54
-32
lines changed

app/code/Magento/Catalog/Controller/Adminhtml/Product/AddAttributeToTemplate.php

Lines changed: 54 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,10 @@
66

77
namespace Magento\Catalog\Controller\Adminhtml\Product;
88

9+
use Magento\Backend\App\Action\Context;
910
use Magento\Catalog\Api\AttributeSetRepositoryInterface;
1011
use Magento\Catalog\Api\Data\ProductAttributeInterface;
12+
use Magento\Catalog\Controller\Adminhtml\Product;
1113
use Magento\Eav\Api\AttributeGroupRepositoryInterface;
1214
use Magento\Eav\Api\AttributeManagementInterface;
1315
use Magento\Eav\Api\AttributeRepositoryInterface;
@@ -16,19 +18,25 @@
1618
use Magento\Eav\Api\Data\AttributeInterface;
1719
use Magento\Eav\Api\Data\AttributeSetInterface;
1820
use Magento\Framework\Api\SearchCriteriaBuilder;
21+
use Magento\Framework\App\Action\HttpPostActionInterface;
22+
use Magento\Framework\Controller\Result\Json;
23+
use Magento\Framework\Controller\Result\JsonFactory;
24+
use Magento\Framework\DataObject;
1925
use Magento\Framework\Exception\LocalizedException;
26+
use Magento\Framework\App\ObjectManager;
2027
use Psr\Log\LoggerInterface;
28+
use Magento\Framework\Exception\NoSuchEntityException;
2129
use Magento\Framework\Api\ExtensionAttributesFactory;
2230

2331
/**
2432
* Class AddAttributeToTemplate
2533
*
2634
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
2735
*/
28-
class AddAttributeToTemplate extends \Magento\Catalog\Controller\Adminhtml\Product
36+
class AddAttributeToTemplate extends Product implements HttpPostActionInterface
2937
{
3038
/**
31-
* @var \Magento\Framework\Controller\Result\JsonFactory
39+
* @var JsonFactory
3240
*/
3341
protected $resultJsonFactory;
3442

@@ -75,33 +83,34 @@ class AddAttributeToTemplate extends \Magento\Catalog\Controller\Adminhtml\Produ
7583
/**
7684
* Constructor
7785
*
78-
* @param \Magento\Backend\App\Action\Context $context
86+
* @param Context $context
7987
* @param Builder $productBuilder
80-
* @param \Magento\Framework\Controller\Result\JsonFactory $resultJsonFactory
81-
* @param \Magento\Eav\Api\Data\AttributeGroupInterfaceFactory|null $attributeGroupFactory
88+
* @param JsonFactory $resultJsonFactory
89+
* @param AttributeGroupInterfaceFactory|null $attributeGroupFactory
8290
* @SuppressWarnings(PHPMD.ExcessiveParameterList)
91+
* @SuppressWarnings(PHPMD.LongVariable)
8392
*/
8493
public function __construct(
85-
\Magento\Backend\App\Action\Context $context,
86-
\Magento\Catalog\Controller\Adminhtml\Product\Builder $productBuilder,
87-
\Magento\Framework\Controller\Result\JsonFactory $resultJsonFactory,
88-
\Magento\Eav\Api\Data\AttributeGroupInterfaceFactory $attributeGroupFactory = null
94+
Context $context,
95+
Builder $productBuilder,
96+
JsonFactory $resultJsonFactory,
97+
AttributeGroupInterfaceFactory $attributeGroupFactory = null
8998
) {
9099
parent::__construct($context, $productBuilder);
91100
$this->resultJsonFactory = $resultJsonFactory;
92-
$this->attributeGroupFactory = $attributeGroupFactory ?: \Magento\Framework\App\ObjectManager::getInstance()
93-
->get(\Magento\Eav\Api\Data\AttributeGroupInterfaceFactory::class);
101+
$this->attributeGroupFactory = $attributeGroupFactory ?: ObjectManager::getInstance()
102+
->get(AttributeGroupInterfaceFactory::class);
94103
}
95104

96105
/**
97106
* Add attribute to attribute set
98107
*
99-
* @return \Magento\Framework\Controller\Result\Json
108+
* @return Json
100109
*/
101110
public function execute()
102111
{
103112
$request = $this->getRequest();
104-
$response = new \Magento\Framework\DataObject();
113+
$response = new DataObject();
105114
$response->setError(false);
106115

107116
try {
@@ -124,12 +133,12 @@ public function execute()
124133
->getItems();
125134

126135
if (!$attributeGroupItems) {
127-
throw new \Magento\Framework\Exception\NoSuchEntityException;
136+
throw new NoSuchEntityException;
128137
}
129138

130139
/** @var AttributeGroupInterface $attributeGroup */
131140
$attributeGroup = reset($attributeGroupItems);
132-
} catch (\Magento\Framework\Exception\NoSuchEntityException $e) {
141+
} catch (NoSuchEntityException $e) {
133142
/** @var AttributeGroupInterface $attributeGroup */
134143
$attributeGroup = $this->attributeGroupFactory->create();
135144
}
@@ -176,101 +185,114 @@ public function execute()
176185
* Adding basic filters
177186
*
178187
* @return SearchCriteriaBuilder
179-
* @throws \Magento\Framework\Exception\LocalizedException
188+
* @throws LocalizedException
180189
*/
181190
private function getBasicAttributeSearchCriteriaBuilder()
182191
{
183-
$attributeIds = (array)$this->getRequest()->getParam('attributeIds', []);
192+
$attributeIds = (array) $this->getRequest()->getParam('attributeIds', []);
184193

185194
if (empty($attributeIds['selected'])) {
186195
throw new LocalizedException(__('Attributes were missing and must be specified.'));
187196
}
188197

189198
return $this->getSearchCriteriaBuilder()
190-
->addFilter('attribute_set_id', new \Zend_Db_Expr('null'), 'is')
191199
->addFilter('attribute_id', [$attributeIds['selected']], 'in');
192200
}
193201

194202
/**
203+
* Get AttributeRepositoryInterface
204+
*
195205
* @return AttributeRepositoryInterface
196206
*/
197207
private function getAttributeRepository()
198208
{
199209
if (null === $this->attributeRepository) {
200-
$this->attributeRepository = \Magento\Framework\App\ObjectManager::getInstance()
201-
->get(\Magento\Eav\Api\AttributeRepositoryInterface::class);
210+
$this->attributeRepository = ObjectManager::getInstance()
211+
->get(AttributeRepositoryInterface::class);
202212
}
203213
return $this->attributeRepository;
204214
}
205215

206216
/**
217+
* Get AttributeSetRepositoryInterface
218+
*
207219
* @return AttributeSetRepositoryInterface
208220
*/
209221
private function getAttributeSetRepository()
210222
{
211223
if (null === $this->attributeSetRepository) {
212-
$this->attributeSetRepository = \Magento\Framework\App\ObjectManager::getInstance()
213-
->get(\Magento\Catalog\Api\AttributeSetRepositoryInterface::class);
224+
$this->attributeSetRepository = ObjectManager::getInstance()
225+
->get(AttributeSetRepositoryInterface::class);
214226
}
215227
return $this->attributeSetRepository;
216228
}
217229

218230
/**
231+
* Get AttributeGroupInterface
232+
*
219233
* @return AttributeGroupRepositoryInterface
220234
*/
221235
private function getAttributeGroupRepository()
222236
{
223237
if (null === $this->attributeGroupRepository) {
224-
$this->attributeGroupRepository = \Magento\Framework\App\ObjectManager::getInstance()
225-
->get(\Magento\Eav\Api\AttributeGroupRepositoryInterface::class);
238+
$this->attributeGroupRepository = ObjectManager::getInstance()
239+
->get(AttributeGroupRepositoryInterface::class);
226240
}
227241
return $this->attributeGroupRepository;
228242
}
229243

230244
/**
245+
* Get SearchCriteriaBuilder
246+
*
231247
* @return SearchCriteriaBuilder
232248
*/
233249
private function getSearchCriteriaBuilder()
234250
{
235251
if (null === $this->searchCriteriaBuilder) {
236-
$this->searchCriteriaBuilder = \Magento\Framework\App\ObjectManager::getInstance()
237-
->get(\Magento\Framework\Api\SearchCriteriaBuilder::class);
252+
$this->searchCriteriaBuilder = ObjectManager::getInstance()
253+
->get(SearchCriteriaBuilder::class);
238254
}
239255
return $this->searchCriteriaBuilder;
240256
}
241257

242258
/**
259+
* Get AttributeManagementInterface
260+
*
243261
* @return AttributeManagementInterface
244262
*/
245263
private function getAttributeManagement()
246264
{
247265
if (null === $this->attributeManagement) {
248-
$this->attributeManagement = \Magento\Framework\App\ObjectManager::getInstance()
249-
->get(\Magento\Eav\Api\AttributeManagementInterface::class);
266+
$this->attributeManagement = ObjectManager::getInstance()
267+
->get(AttributeManagementInterface::class);
250268
}
251269
return $this->attributeManagement;
252270
}
253271

254272
/**
273+
* Get LoggerInterface
274+
*
255275
* @return LoggerInterface
256276
*/
257277
private function getLogger()
258278
{
259279
if (null === $this->logger) {
260-
$this->logger = \Magento\Framework\App\ObjectManager::getInstance()
261-
->get(\Psr\Log\LoggerInterface::class);
280+
$this->logger = ObjectManager::getInstance()
281+
->get(LoggerInterface::class);
262282
}
263283
return $this->logger;
264284
}
265285

266286
/**
287+
* Get ExtensionAttributesFactory.
288+
*
267289
* @return ExtensionAttributesFactory
268290
*/
269291
private function getExtensionAttributesFactory()
270292
{
271293
if (null === $this->extensionAttributesFactory) {
272-
$this->extensionAttributesFactory = \Magento\Framework\App\ObjectManager::getInstance()
273-
->get(\Magento\Framework\Api\ExtensionAttributesFactory::class);
294+
$this->extensionAttributesFactory = ObjectManager::getInstance()
295+
->get(ExtensionAttributesFactory::class);
274296
}
275297
return $this->extensionAttributesFactory;
276298
}

0 commit comments

Comments
 (0)