wysiwygValidator = $wysiwygValidator ?? ObjectManager::getInstance()->get(WYSIWYGValidatorInterface::class); $this->compositeUrlValidator = $compositeUrlValidator ?? ObjectManager::getInstance()->get(CompositeUrlKey::class); } /** * Construct. * * @return void */ protected function _construct() { $this->_init(\Magento\Cms\Model\ResourceModel\Block::class); } /** * Prevent blocks recursion * * @return AbstractModel * @throws \Magento\Framework\Exception\LocalizedException */ public function beforeSave() { if ($this->hasDataChanges()) { $this->setUpdateTime(null); } $needle = 'block_id="' . $this->getId() . '"'; $content = ($this->getContent() !== null) ? $this->getContent() : ''; if (strpos($content, $needle) !== false) { throw new \Magento\Framework\Exception\LocalizedException( __('Make sure that static block content does not reference the block itself.') ); } $errors = $this->compositeUrlValidator->validate($this->getIdentifier()); if (!empty($errors)) { throw new LocalizedException($errors[0]); } parent::beforeSave(); //Validating HTML content. if ($content && $content !== $this->getOrigData(self::CONTENT)) { try { $this->wysiwygValidator->validate($content); } catch (ValidationException $exception) { throw new ValidationException( __('Content field contains restricted HTML elements. %1', $exception->getMessage()), $exception ); } } return $this; } /** * Get identities * * @return array */ public function getIdentities() { return [self::CACHE_TAG . '_' . $this->getId(), self::CACHE_TAG . '_' . $this->getIdentifier()]; } /** * Retrieve block id * * @return int */ public function getId() { return $this->getData(self::BLOCK_ID); } /** * Retrieve block identifier * * @return string */ public function getIdentifier() { return (string)$this->getData(self::IDENTIFIER); } /** * Retrieve block title * * @return string */ public function getTitle() { return $this->getData(self::TITLE); } /** * Retrieve block content * * @return string */ public function getContent() { return $this->getData(self::CONTENT); } /** * Retrieve block creation time * * @return string */ public function getCreationTime() { return $this->getData(self::CREATION_TIME); } /** * Retrieve block update time * * @return string */ public function getUpdateTime() { return $this->getData(self::UPDATE_TIME); } /** * Is active * * @return bool */ public function isActive() { return (bool)$this->getData(self::IS_ACTIVE); } /** * Set ID * * @param int $id * @return BlockInterface */ public function setId($id) { return $this->setData(self::BLOCK_ID, $id); } /** * Set identifier * * @param string $identifier * @return BlockInterface */ public function setIdentifier($identifier) { return $this->setData(self::IDENTIFIER, $identifier); } /** * Set title * * @param string $title * @return BlockInterface */ public function setTitle($title) { return $this->setData(self::TITLE, $title); } /** * Set content * * @param string $content * @return BlockInterface */ public function setContent($content) { return $this->setData(self::CONTENT, $content); } /** * Set creation time * * @param string $creationTime * @return BlockInterface */ public function setCreationTime($creationTime) { return $this->setData(self::CREATION_TIME, $creationTime); } /** * Set update time * * @param string $updateTime * @return BlockInterface */ public function setUpdateTime($updateTime) { return $this->setData(self::UPDATE_TIME, $updateTime); } /** * Set is active * * @param bool|int $isActive * @return BlockInterface */ public function setIsActive($isActive) { return $this->setData(self::IS_ACTIVE, $isActive); } /** * Receive page store ids * * @return int[] */ public function getStores() { return $this->hasData('stores') ? $this->getData('stores') : $this->getData('store_id'); } /** * Prepare block's statuses. * * @return array */ public function getAvailableStatuses() { return [self::STATUS_ENABLED => __('Enabled'), self::STATUS_DISABLED => __('Disabled')]; } }