-
Notifications
You must be signed in to change notification settings - Fork 9.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #4969 from magento-qwerty/MC-18684
[Qwerty] MC-18684: [2.2.x] Remove custom layout updates from admin
- Loading branch information
Showing
104 changed files
with
5,564 additions
and
964 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
119 changes: 119 additions & 0 deletions
119
app/code/Magento/Catalog/Model/Attribute/Backend/AbstractLayoutUpdate.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,119 @@ | ||
<?php | ||
/** | ||
* Copyright © Magento, Inc. All rights reserved. | ||
* See COPYING.txt for license details. | ||
*/ | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Magento\Catalog\Model\Attribute\Backend; | ||
|
||
use Magento\Eav\Model\Entity\Attribute\Backend\AbstractBackend; | ||
use Magento\Framework\Exception\LocalizedException; | ||
use Magento\Catalog\Model\AbstractModel; | ||
|
||
/** | ||
* Custom layout file attribute. | ||
*/ | ||
abstract class AbstractLayoutUpdate extends AbstractBackend | ||
{ | ||
const VALUE_USE_UPDATE_XML = '__existing__'; | ||
|
||
const VALUE_NO_UPDATE = '__no_update__'; | ||
|
||
/** | ||
* Extract attribute value. | ||
* | ||
* @param AbstractModel $model | ||
* @return mixed | ||
*/ | ||
private function extractAttributeValue(AbstractModel $model) | ||
{ | ||
$code = $this->getAttribute()->getAttributeCode(); | ||
|
||
return $model->getData($code); | ||
} | ||
|
||
/** | ||
* Compose list of available files (layout handles) for given entity. | ||
* | ||
* @param AbstractModel $forModel | ||
* @return string[] | ||
*/ | ||
abstract protected function listAvailableValues(AbstractModel $forModel): array; | ||
|
||
/** | ||
* Extracts prepare attribute value to be saved. | ||
* | ||
* @throws LocalizedException | ||
* @param AbstractModel $model | ||
* @return string|null | ||
*/ | ||
private function prepareValue(AbstractModel $model) | ||
{ | ||
$value = $this->extractAttributeValue($model); | ||
if (!is_string($value)) { | ||
$value = null; | ||
} | ||
if ($value | ||
&& $value !== self::VALUE_USE_UPDATE_XML | ||
&& $value !== self::VALUE_NO_UPDATE | ||
&& !in_array($value, $this->listAvailableValues($model), true) | ||
) { | ||
throw new LocalizedException(__('Selected layout update is not available')); | ||
} | ||
|
||
return $value; | ||
} | ||
|
||
/** | ||
* Set value for the object. | ||
* | ||
* @param string|null $value | ||
* @param AbstractModel $forObject | ||
* @param string|null $attrCode | ||
* @return void | ||
*/ | ||
private function setAttributeValue($value, AbstractModel $forObject, $attrCode = null) | ||
{ | ||
$attrCode = $attrCode ?? $this->getAttribute()->getAttributeCode(); | ||
if ($forObject->hasData(AbstractModel::CUSTOM_ATTRIBUTES)) { | ||
$forObject->setCustomAttribute($attrCode, $value); | ||
} | ||
$forObject->setData($attrCode, $value); | ||
} | ||
|
||
/** | ||
* @inheritDoc | ||
* | ||
* @param AbstractModel $object | ||
*/ | ||
public function validate($object) | ||
{ | ||
$valid = parent::validate($object); | ||
if ($valid) { | ||
$this->prepareValue($object); | ||
} | ||
|
||
return $valid; | ||
} | ||
|
||
/** | ||
* @inheritDoc | ||
* @param AbstractModel $object | ||
* @throws LocalizedException | ||
*/ | ||
public function beforeSave($object) | ||
{ | ||
$value = $this->prepareValue($object); | ||
if ($value && ($value === self::VALUE_NO_UPDATE || $value !== self::VALUE_USE_UPDATE_XML)) { | ||
$this->setAttributeValue(null, $object, 'custom_layout_update'); | ||
} | ||
if (!$value || $value === self::VALUE_USE_UPDATE_XML || $value === self::VALUE_NO_UPDATE) { | ||
$value = null; | ||
} | ||
$this->setAttributeValue($value, $object); | ||
|
||
return $this; | ||
} | ||
} |
Oops, something went wrong.