Skip to content

Commit 0347c1d

Browse files
author
Oleksandr Gorkun
committed
MC-18685: Remove custom layout updates from admin
1 parent 058fe1a commit 0347c1d

File tree

9 files changed

+137
-108
lines changed

9 files changed

+137
-108
lines changed
Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
7+
declare(strict_types=1);
8+
9+
namespace Magento\Catalog\Model\Attribute\Source;
10+
11+
use Magento\Eav\Model\Entity\Attribute\Source\AbstractSource;
12+
use Magento\Eav\Model\Entity\Attribute\Source\SpecificSourceInterface;
13+
use Magento\Framework\Api\CustomAttributesDataInterface;
14+
use Magento\Catalog\Model\Attribute\Backend\AbstractLayoutUpdate as Backend;
15+
use Magento\Framework\Model\AbstractExtensibleModel;
16+
17+
/**
18+
* List of layout updates available for a catalog entity.
19+
*/
20+
abstract class AbstractLayoutUpdate extends AbstractSource implements SpecificSourceInterface
21+
{
22+
/**
23+
* @var string[]
24+
*/
25+
private $optionsText;
26+
27+
/**
28+
* @inheritDoc
29+
*/
30+
public function getAllOptions()
31+
{
32+
$default = Backend::VALUE_NO_UPDATE;
33+
$defaultText = 'No update';
34+
$this->optionsText[$default] = $defaultText;
35+
36+
return [['label' => $defaultText, 'value' => $default]];
37+
}
38+
39+
/**
40+
* @inheritDoc
41+
*/
42+
public function getOptionText($value)
43+
{
44+
if (is_scalar($value) && array_key_exists($value, $this->optionsText)) {
45+
return $this->optionsText[$value];
46+
}
47+
48+
return false;
49+
}
50+
51+
/**
52+
* Extract attribute value.
53+
*
54+
* @param CustomAttributesDataInterface|AbstractExtensibleModel $entity
55+
* @return mixed
56+
*/
57+
private function extractAttributeValue(CustomAttributesDataInterface $entity)
58+
{
59+
$attrCode = 'custom_layout_update';
60+
if ($entity instanceof AbstractExtensibleModel
61+
&& !$entity->hasData(CustomAttributesDataInterface::CUSTOM_ATTRIBUTES)
62+
) {
63+
//Custom attributes were not loaded yet, using data array
64+
return $entity->getData($attrCode);
65+
}
66+
//Fallback to customAttribute method
67+
$attr = $entity->getCustomAttribute($attrCode);
68+
69+
return $attr ? $attr->getValue() : null;
70+
}
71+
72+
/**
73+
* List available layout update options for the entity.
74+
*
75+
* @param CustomAttributesDataInterface $entity
76+
* @return string[]
77+
*/
78+
abstract protected function listAvailableOptions(CustomAttributesDataInterface $entity): array;
79+
80+
/**
81+
* @inheritDoc
82+
*
83+
* @param CustomAttributesDataInterface|AbstractExtensibleModel $entity
84+
*/
85+
public function getOptionsFor(CustomAttributesDataInterface $entity): array
86+
{
87+
$options = $this->getAllOptions();
88+
if ($this->extractAttributeValue($entity)) {
89+
$existingValue = Backend::VALUE_USE_UPDATE_XML;
90+
$existingLabel = 'Use existing';
91+
$options[] = ['label' => $existingLabel, 'value' => $existingValue];
92+
$this->optionsText[$existingValue] = $existingLabel;
93+
}
94+
foreach ($this->listAvailableOptions($entity) as $handle) {
95+
$options[] = ['label' => $handle, 'value' => $handle];
96+
$this->optionsText[$handle] = $handle;
97+
}
98+
99+
return $options;
100+
}
101+
}

app/code/Magento/Catalog/Model/Category/Attribute/LayoutUpdateManager.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,7 @@ function (string $handle) use ($category) : ?string {
121121
*/
122122
private function extractAttributeValue(CategoryInterface $category)
123123
{
124-
if ($category instanceof Category && $category->hasData('custom_layout_update_file')) {
124+
if ($category instanceof Category && !$category->hasData(CategoryInterface::CUSTOM_ATTRIBUTES)) {
125125
return $category->getData('custom_layout_update_file');
126126
}
127127
if ($attr = $category->getCustomAttribute('custom_layout_update_file')) {

app/code/Magento/Catalog/Model/Category/Attribute/Source/LayoutUpdate.php

Lines changed: 4 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -8,23 +8,15 @@
88

99
namespace Magento\Catalog\Model\Category\Attribute\Source;
1010

11-
use Magento\Catalog\Api\Data\CategoryInterface;
1211
use Magento\Catalog\Model\Category\Attribute\LayoutUpdateManager;
13-
use Magento\Eav\Model\Entity\Attribute\Source\AbstractSource;
14-
use Magento\Eav\Model\Entity\Attribute\Source\SpecificSourceInterface;
1512
use Magento\Framework\Api\CustomAttributesDataInterface;
16-
use Magento\Catalog\Model\Product\Attribute\Backend\LayoutUpdate as Backend;
13+
use Magento\Catalog\Model\Attribute\Source\AbstractLayoutUpdate;
1714

1815
/**
1916
* List of layout updates available for a category.
2017
*/
21-
class LayoutUpdate extends AbstractSource implements SpecificSourceInterface
18+
class LayoutUpdate extends AbstractLayoutUpdate
2219
{
23-
/**
24-
* @var string[]
25-
*/
26-
private $optionsText;
27-
2820
/**
2921
* @var LayoutUpdateManager
3022
*/
@@ -41,46 +33,8 @@ public function __construct(LayoutUpdateManager $manager)
4133
/**
4234
* @inheritDoc
4335
*/
44-
public function getAllOptions()
45-
{
46-
$default = Backend::VALUE_NO_UPDATE;
47-
$defaultText = 'No update';
48-
$this->optionsText[$default] = $defaultText;
49-
50-
return [['label' => $defaultText, 'value' => $default]];
51-
}
52-
53-
/**
54-
* @inheritDoc
55-
*/
56-
public function getOptionText($value)
36+
protected function listAvailableOptions(CustomAttributesDataInterface $entity): array
5737
{
58-
if (is_scalar($value) && array_key_exists($value, $this->optionsText)) {
59-
return $this->optionsText[$value];
60-
}
61-
62-
return false;
63-
}
64-
65-
/**
66-
* @inheritDoc
67-
*
68-
* @param CategoryInterface $entity
69-
*/
70-
public function getOptionsFor(CustomAttributesDataInterface $entity): array
71-
{
72-
$options = $this->getAllOptions();
73-
if ($entity->getCustomAttribute('custom_layout_update')) {
74-
$existingValue = Backend::VALUE_USE_UPDATE_XML;
75-
$existingLabel = 'Use existing';
76-
$options[] = ['label' => $existingLabel, 'value' => $existingValue];
77-
$this->optionsText[$existingValue] = $existingLabel;
78-
}
79-
foreach ($this->manager->fetchAvailableFiles($entity) as $handle) {
80-
$options[] = ['label' => $handle, 'value' => $handle];
81-
$this->optionsText[$handle] = $handle;
82-
}
83-
84-
return $options;
38+
return $this->manager->fetchAvailableFiles($entity);
8539
}
8640
}

app/code/Magento/Catalog/Model/Category/DataProvider.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -386,7 +386,7 @@ public function getAttributesMeta(Type $entityType)
386386
if ($source instanceof SpecificSourceInterface && $currentCategory) {
387387
$options = $source->getOptionsFor($currentCategory);
388388
} else {
389-
$options = $attribute->getSource()->getAllOptions();
389+
$options = $source->getAllOptions();
390390
}
391391
foreach ($options as &$option) {
392392
$option['__disableTmpl'] = true;

app/code/Magento/Catalog/Model/Product/Attribute/LayoutUpdateManager.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,7 @@ function (string $handle) use ($identifier) : ?string {
133133
*/
134134
private function extractAttributeValue(ProductInterface $product)
135135
{
136-
if ($product instanceof Product && $product->hasData('custom_layout_update_file')) {
136+
if ($product instanceof Product && !$product->hasData(ProductInterface::CUSTOM_ATTRIBUTES)) {
137137
return $product->getData('custom_layout_update_file');
138138
}
139139
if ($attr = $product->getCustomAttribute('custom_layout_update_file')) {

app/code/Magento/Catalog/Model/Product/Attribute/Source/LayoutUpdate.php

Lines changed: 4 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -8,22 +8,15 @@
88

99
namespace Magento\Catalog\Model\Product\Attribute\Source;
1010

11+
use Magento\Catalog\Model\Attribute\Source\AbstractLayoutUpdate;
1112
use Magento\Catalog\Model\Product\Attribute\LayoutUpdateManager;
12-
use Magento\Eav\Model\Entity\Attribute\Source\AbstractSource;
13-
use Magento\Eav\Model\Entity\Attribute\Source\SpecificSourceInterface;
1413
use Magento\Framework\Api\CustomAttributesDataInterface;
15-
use Magento\Catalog\Model\Product\Attribute\Backend\LayoutUpdate as Backend;
1614

1715
/**
1816
* List of layout updates available for a product.
1917
*/
20-
class LayoutUpdate extends AbstractSource implements SpecificSourceInterface
18+
class LayoutUpdate extends AbstractLayoutUpdate
2119
{
22-
/**
23-
* @var string[]
24-
*/
25-
private $optionsText;
26-
2720
/**
2821
* @var LayoutUpdateManager
2922
*/
@@ -40,44 +33,8 @@ public function __construct(LayoutUpdateManager $manager)
4033
/**
4134
* @inheritDoc
4235
*/
43-
public function getAllOptions()
44-
{
45-
$default = Backend::VALUE_NO_UPDATE;
46-
$defaultText = 'No update';
47-
$this->optionsText[$default] = $defaultText;
48-
49-
return [['label' => $defaultText, 'value' => $default]];
50-
}
51-
52-
/**
53-
* @inheritDoc
54-
*/
55-
public function getOptionText($value)
36+
protected function listAvailableOptions(CustomAttributesDataInterface $entity): array
5637
{
57-
if (is_scalar($value) && array_key_exists($value, $this->optionsText)) {
58-
return $this->optionsText[$value];
59-
}
60-
61-
return false;
62-
}
63-
64-
/**
65-
* @inheritDoc
66-
*/
67-
public function getOptionsFor(CustomAttributesDataInterface $entity): array
68-
{
69-
$options = $this->getAllOptions();
70-
if ($entity->getCustomAttribute('custom_layout_update')) {
71-
$existingValue = Backend::VALUE_USE_UPDATE_XML;
72-
$existingLabel = 'Use existing';
73-
$options[] = ['label' => $existingLabel, 'value' => $existingValue];
74-
$this->optionsText[$existingValue] = $existingLabel;
75-
}
76-
foreach ($this->manager->fetchAvailableFiles($entity) as $handle) {
77-
$options[] = ['label' => $handle, 'value' => $handle];
78-
$this->optionsText[$handle] = $handle;
79-
}
80-
81-
return $options;
38+
return $this->manager->fetchAvailableFiles($entity);
8239
}
8340
}

app/code/Magento/Catalog/Ui/DataProvider/Product/Form/Modifier/Eav.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -697,7 +697,7 @@ public function setupAttributeMeta(ProductAttributeInterface $attribute, $groupC
697697
if ($source instanceof SpecificSourceInterface) {
698698
$options = $source->getOptionsFor($product);
699699
} else {
700-
$options = $attributeModel->getSource()->getAllOptions(true, true);
700+
$options = $source->getAllOptions(true, true);
701701
}
702702
foreach ($options as &$option) {
703703
$option['__disableTmpl'] = true;

app/code/Magento/Catalog/Ui/DataProvider/Product/Form/Modifier/LayoutUpdate.php

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,9 @@
88

99
namespace Magento\Catalog\Ui\DataProvider\Product\Form\Modifier;
1010

11+
use Magento\Catalog\Api\Data\ProductInterface;
1112
use Magento\Catalog\Model\Locator\LocatorInterface;
13+
use Magento\Catalog\Model\Product;
1214
use Magento\Ui\DataProvider\Modifier\ModifierInterface;
1315

1416
/**
@@ -29,18 +31,33 @@ public function __construct(LocatorInterface $locator)
2931
$this->locator = $locator;
3032
}
3133

34+
/**
35+
* Extract custom layout value.
36+
*
37+
* @param ProductInterface|Product $product
38+
* @return mixed
39+
*/
40+
private function extractLayoutUpdate(ProductInterface $product)
41+
{
42+
if ($product instanceof Product && !$product->hasData(Product::CUSTOM_ATTRIBUTES)) {
43+
return $product->getData('custom_layout_update');
44+
}
45+
46+
$attr = $product->getCustomAttribute('custom_layout_update');
47+
48+
return $attr ? $attr->getValue() : null;
49+
}
50+
3251
/**
3352
* @inheritdoc
3453
* @since 101.1.0
3554
*/
3655
public function modifyData(array $data)
3756
{
3857
$product = $this->locator->getProduct();
39-
if ($oldLayout = $product->getCustomAttribute('custom_layout_update')) {
40-
if ($oldLayout->getValue()) {
41-
$data[$product->getId()][AbstractModifier::DATA_SOURCE_DEFAULT]['custom_layout_update_file']
42-
= \Magento\Catalog\Model\Product\Attribute\Backend\LayoutUpdate::VALUE_USE_UPDATE_XML;
43-
}
58+
if ($oldLayout = $this->extractLayoutUpdate($product)) {
59+
$data[$product->getId()][AbstractModifier::DATA_SOURCE_DEFAULT]['custom_layout_update_file']
60+
= \Magento\Catalog\Model\Product\Attribute\Backend\LayoutUpdate::VALUE_USE_UPDATE_XML;
4461
}
4562

4663
return $data;

app/code/Magento/Catalog/etc/di.xml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1176,12 +1176,12 @@
11761176
</argument>
11771177
</arguments>
11781178
</type>
1179-
<type name="Magento\Catalog\Model\Product\Attribute">
1179+
<type name="Magento\Catalog\Model\Product\Attribute\LayoutUpdateManager">
11801180
<arguments>
11811181
<argument name="themeFactory" xsi:type="object">Magento\Framework\View\Design\Theme\FlyweightFactory\Proxy</argument>
11821182
</arguments>
11831183
</type>
1184-
<type name="Magento\Catalog\Model\Category\Attribute">
1184+
<type name="Magento\Catalog\Model\Category\Attribute\LayoutUpdateManager">
11851185
<arguments>
11861186
<argument name="themeFactory" xsi:type="object">Magento\Framework\View\Design\Theme\FlyweightFactory\Proxy</argument>
11871187
</arguments>

0 commit comments

Comments
 (0)