![]() Server : Apache System : Linux server2.corals.io 4.18.0-348.2.1.el8_5.x86_64 #1 SMP Mon Nov 15 09:17:08 EST 2021 x86_64 User : corals ( 1002) PHP Version : 7.4.33 Disable Function : exec,passthru,shell_exec,system Directory : /home/corals/old/vendor/magento/module-media-content-cms/Observer/ |
<?php /** * Copyright © Magento, Inc. All rights reserved. * See COPYING.txt for license details. */ declare(strict_types=1); namespace Magento\MediaContentCms\Observer; use Magento\Cms\Model\Block as CmsBlock; use Magento\Framework\Event\Observer; use Magento\Framework\Event\ObserverInterface; use Magento\MediaContentApi\Api\UpdateContentAssetLinksInterface; use Magento\MediaContentApi\Api\Data\ContentIdentityInterfaceFactory; use Magento\MediaContentApi\Model\Config; /** * Observe cms_block_save_after event and run processing relation between cms block content and media asset */ class Block implements ObserverInterface { private const CONTENT_TYPE = 'cms_block'; private const TYPE = 'entityType'; private const ENTITY_ID = 'entityId'; private const FIELD = 'field'; /** * @var UpdateContentAssetLinksInterface */ private $updateContentAssetLinks; /** * @var Config */ private $config; /** * @var array */ private $fields; /** * @var ContentIdentityInterfaceFactory */ private $contentIdentityFactory; /** * @param ContentIdentityInterfaceFactory $contentIdentityFactory * @param UpdateContentAssetLinksInterface $updateContentAssetLinks * @param Config $config * @param array $fields */ public function __construct( ContentIdentityInterfaceFactory $contentIdentityFactory, UpdateContentAssetLinksInterface $updateContentAssetLinks, Config $config, array $fields ) { $this->contentIdentityFactory = $contentIdentityFactory; $this->config = $config; $this->updateContentAssetLinks = $updateContentAssetLinks; $this->fields = $fields; } /** * Retrieve the saved block and pass it to the model processor to save content - asset relations * * @param Observer $observer */ public function execute(Observer $observer): void { if (!$this->config->isEnabled()) { return; } $model = $observer->getEvent()->getData('object'); if ($model instanceof CmsBlock) { foreach ($this->fields as $field) { if (!$model->dataHasChangedFor($field)) { continue; } $this->updateContentAssetLinks->execute( $this->contentIdentityFactory->create( [ self::TYPE => self::CONTENT_TYPE, self::FIELD => $field, self::ENTITY_ID => (string) $model->getId(), ] ), (string) $model->getData($field) ); } } } }