|
13 | 13 | use Magento\Framework\DataObject\IdentityInterface;
|
14 | 14 | use Magento\Framework\Exception\LocalizedException;
|
15 | 15 | use Magento\Framework\Model\AbstractModel;
|
| 16 | +use Magento\Framework\Validation\ValidationException; |
| 17 | +use Magento\Framework\Validator\HTML\WYSIWYGValidatorInterface; |
16 | 18 |
|
17 | 19 | /**
|
18 | 20 | * Cms Page Model
|
@@ -64,25 +66,34 @@ class Page extends AbstractModel implements PageInterface, IdentityInterface
|
64 | 66 | */
|
65 | 67 | private $customLayoutRepository;
|
66 | 68 |
|
| 69 | + /** |
| 70 | + * @var WYSIWYGValidatorInterface |
| 71 | + */ |
| 72 | + private $wysiwygValidator; |
| 73 | + |
67 | 74 | /**
|
68 | 75 | * @param \Magento\Framework\Model\Context $context
|
69 | 76 | * @param \Magento\Framework\Registry $registry
|
70 | 77 | * @param \Magento\Framework\Model\ResourceModel\AbstractResource|null $resource
|
71 | 78 | * @param \Magento\Framework\Data\Collection\AbstractDb|null $resourceCollection
|
72 | 79 | * @param array $data
|
73 | 80 | * @param CustomLayoutRepository|null $customLayoutRepository
|
| 81 | + * @param WYSIWYGValidatorInterface|null $wysiwygValidator |
74 | 82 | */
|
75 | 83 | public function __construct(
|
76 | 84 | \Magento\Framework\Model\Context $context,
|
77 | 85 | \Magento\Framework\Registry $registry,
|
78 | 86 | \Magento\Framework\Model\ResourceModel\AbstractResource $resource = null,
|
79 | 87 | \Magento\Framework\Data\Collection\AbstractDb $resourceCollection = null,
|
80 | 88 | array $data = [],
|
81 |
| - ?CustomLayoutRepository $customLayoutRepository = null |
| 89 | + ?CustomLayoutRepository $customLayoutRepository = null, |
| 90 | + ?WYSIWYGValidatorInterface $wysiwygValidator = null |
82 | 91 | ) {
|
83 | 92 | parent::__construct($context, $registry, $resource, $resourceCollection, $data);
|
84 | 93 | $this->customLayoutRepository = $customLayoutRepository
|
85 | 94 | ?? ObjectManager::getInstance()->get(CustomLayoutRepository::class);
|
| 95 | + $this->wysiwygValidator = $wysiwygValidator |
| 96 | + ?? ObjectManager::getInstance()->get(WYSIWYGValidatorInterface::class); |
86 | 97 | }
|
87 | 98 |
|
88 | 99 | /**
|
@@ -615,6 +626,26 @@ public function beforeSave()
|
615 | 626 | $this->setData('layout_update_selected', $layoutUpdate);
|
616 | 627 | $this->customLayoutRepository->validateLayoutSelectedFor($this);
|
617 | 628 |
|
| 629 | + //Validating Content HTML. |
| 630 | + $oldValue = null; |
| 631 | + if ($this->getId()) { |
| 632 | + if ($this->getOrigData()) { |
| 633 | + $oldValue = $this->getOrigData(self::CONTENT); |
| 634 | + } elseif (array_key_exists(self::CONTENT, $this->getStoredData())) { |
| 635 | + $oldValue = $this->getStoredData()[self::CONTENT]; |
| 636 | + } |
| 637 | + } |
| 638 | + if ($this->getContent() && $this->getContent() !== $oldValue) { |
| 639 | + try { |
| 640 | + $this->wysiwygValidator->validate($this->getContent()); |
| 641 | + } catch (ValidationException $exception) { |
| 642 | + throw new ValidationException( |
| 643 | + __('Content HTML contains restricted elements. %1', $exception->getMessage()), |
| 644 | + $exception |
| 645 | + ); |
| 646 | + } |
| 647 | + } |
| 648 | + |
618 | 649 | return parent::beforeSave();
|
619 | 650 | }
|
620 | 651 |
|
|
0 commit comments