Customlayoutupdate.php 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Catalog\Model\Attribute\Backend;
  7. use Magento\Framework\View\Model\Layout\Update\ValidatorFactory;
  8. use Magento\Eav\Model\Entity\Attribute\Exception;
  9. /**
  10. * Layout update attribute backend
  11. *
  12. * @api
  13. *
  14. * @SuppressWarnings(PHPMD.LongVariable)
  15. * @since 100.0.2
  16. */
  17. class Customlayoutupdate extends \Magento\Eav\Model\Entity\Attribute\Backend\AbstractBackend
  18. {
  19. /**
  20. * Layout update validator factory
  21. *
  22. * @var ValidatorFactory
  23. */
  24. protected $_layoutUpdateValidatorFactory;
  25. /**
  26. * Construct the custom layout update class
  27. *
  28. * @param ValidatorFactory $layoutUpdateValidatorFactory
  29. */
  30. public function __construct(ValidatorFactory $layoutUpdateValidatorFactory)
  31. {
  32. $this->_layoutUpdateValidatorFactory = $layoutUpdateValidatorFactory;
  33. }
  34. /**
  35. * Validate the custom layout update
  36. *
  37. * @param \Magento\Framework\DataObject $object
  38. * @return bool
  39. * @throws Exception
  40. */
  41. public function validate($object)
  42. {
  43. $attributeName = $this->getAttribute()->getName();
  44. $xml = trim($object->getData($attributeName));
  45. if (!$this->getAttribute()->getIsRequired() && empty($xml)) {
  46. return true;
  47. }
  48. /** @var $validator \Magento\Framework\View\Model\Layout\Update\Validator */
  49. $validator = $this->_layoutUpdateValidatorFactory->create();
  50. if (!$validator->isValid($xml)) {
  51. $messages = $validator->getMessages();
  52. //Add first message to exception
  53. $message = array_shift($messages);
  54. $eavExc = new Exception(__($message));
  55. $eavExc->setAttributeCode($attributeName);
  56. throw $eavExc;
  57. }
  58. return true;
  59. }
  60. }