123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566 |
- <?php
- /**
- * Copyright © Magento, Inc. All rights reserved.
- * See COPYING.txt for license details.
- */
- namespace Magento\Catalog\Model\Attribute\Backend;
- use Magento\Framework\View\Model\Layout\Update\ValidatorFactory;
- use Magento\Eav\Model\Entity\Attribute\Exception;
- /**
- * Layout update attribute backend
- *
- * @api
- *
- * @SuppressWarnings(PHPMD.LongVariable)
- * @since 100.0.2
- */
- class Customlayoutupdate extends \Magento\Eav\Model\Entity\Attribute\Backend\AbstractBackend
- {
- /**
- * Layout update validator factory
- *
- * @var ValidatorFactory
- */
- protected $_layoutUpdateValidatorFactory;
- /**
- * Construct the custom layout update class
- *
- * @param ValidatorFactory $layoutUpdateValidatorFactory
- */
- public function __construct(ValidatorFactory $layoutUpdateValidatorFactory)
- {
- $this->_layoutUpdateValidatorFactory = $layoutUpdateValidatorFactory;
- }
- /**
- * Validate the custom layout update
- *
- * @param \Magento\Framework\DataObject $object
- * @return bool
- * @throws Exception
- */
- public function validate($object)
- {
- $attributeName = $this->getAttribute()->getName();
- $xml = trim($object->getData($attributeName));
- if (!$this->getAttribute()->getIsRequired() && empty($xml)) {
- return true;
- }
- /** @var $validator \Magento\Framework\View\Model\Layout\Update\Validator */
- $validator = $this->_layoutUpdateValidatorFactory->create();
- if (!$validator->isValid($xml)) {
- $messages = $validator->getMessages();
- //Add first message to exception
- $message = array_shift($messages);
- $eavExc = new Exception(__($message));
- $eavExc->setAttributeCode($attributeName);
- throw $eavExc;
- }
- return true;
- }
- }
|