|
| 1 | +<?php |
| 2 | +/************************************************************************ |
| 3 | + * |
| 4 | + * Copyright 2024 Adobe |
| 5 | + * All Rights Reserved. |
| 6 | + * |
| 7 | + * NOTICE: All information contained herein is, and remains |
| 8 | + * the property of Adobe and its suppliers, if any. The intellectual |
| 9 | + * and technical concepts contained herein are proprietary to Adobe |
| 10 | + * and its suppliers and are protected by all applicable intellectual |
| 11 | + * property laws, including trade secret and copyright laws. |
| 12 | + * Dissemination of this information or reproduction of this material |
| 13 | + * is strictly forbidden unless prior written permission is obtained |
| 14 | + * from Adobe. |
| 15 | + * ************************************************************************ |
| 16 | + */ |
| 17 | +declare(strict_types=1); |
| 18 | + |
| 19 | +namespace Magento\Theme\Observer; |
| 20 | + |
| 21 | +use Magento\Framework\App\Config\ScopeConfigInterface; |
| 22 | +use Magento\Framework\Event\Observer; |
| 23 | +use Magento\Framework\Event\ObserverInterface; |
| 24 | +use Magento\Store\Model\ScopeInterface; |
| 25 | +use Magento\Store\Model\StoreManager; |
| 26 | +use Magento\Store\Model\StoreManagerInterface; |
| 27 | +use Magento\Theme\Api\DesignConfigRepositoryInterface; |
| 28 | + |
| 29 | +class MoveStoreLevelDesignConfigToWebsiteScopeOnSingleStoreMode implements ObserverInterface |
| 30 | +{ |
| 31 | + /** |
| 32 | + * @param ScopeConfigInterface $scopeConfig |
| 33 | + * @param DesignConfigRepositoryInterface $designConfigRepository |
| 34 | + * @param StoreManagerInterface $storeManager |
| 35 | + */ |
| 36 | + public function __construct( |
| 37 | + private readonly ScopeConfigInterface $scopeConfig, |
| 38 | + private readonly DesignConfigRepositoryInterface $designConfigRepository, |
| 39 | + private readonly StoreManagerInterface $storeManager |
| 40 | + ) { |
| 41 | + } |
| 42 | + |
| 43 | + /** |
| 44 | + * @inheritDoc |
| 45 | + */ |
| 46 | + public function execute(Observer $observer) |
| 47 | + { |
| 48 | + $changedPaths = (array)$observer->getEvent()->getChangedPaths(); |
| 49 | + if (in_array(StoreManager::XML_PATH_SINGLE_STORE_MODE_ENABLED, $changedPaths, true) |
| 50 | + && $this->scopeConfig->getValue(StoreManager::XML_PATH_SINGLE_STORE_MODE_ENABLED) |
| 51 | + ) { |
| 52 | + $store = $this->storeManager->getDefaultStoreView(); |
| 53 | + if ($store) { |
| 54 | + $websiteId = $store->getWebsiteId(); |
| 55 | + $storeId = $store->getId(); |
| 56 | + $designConfig = $this->designConfigRepository->getByScope(ScopeInterface::SCOPE_STORES, $storeId); |
| 57 | + // Copy design config from store scope to website scope |
| 58 | + $designConfig->setScope(ScopeInterface::SCOPE_WEBSITES); |
| 59 | + $designConfig->setScopeId($websiteId); |
| 60 | + $this->designConfigRepository->save($designConfig); |
| 61 | + // At this point store design config the same as website design config. |
| 62 | + // By saving store design config, we will preserve inheritance from website scope. |
| 63 | + $designConfig->setScope(ScopeInterface::SCOPE_STORES); |
| 64 | + $designConfig->setScopeId($storeId); |
| 65 | + $this->designConfigRepository->save($designConfig); |
| 66 | + } |
| 67 | + } |
| 68 | + } |
| 69 | +} |
0 commit comments