|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Copyright © Magento, Inc. All rights reserved. |
| 4 | + * See COPYING.txt for license details. |
| 5 | + */ |
| 6 | +declare(strict_types=1); |
| 7 | + |
| 8 | +namespace Magento\CatalogGraphQl\Plugin; |
| 9 | + |
| 10 | +use Magento\Catalog\Model\Product; |
| 11 | +use Magento\Framework\Message\MessageInterface; |
| 12 | +use Magento\Framework\View\DesignLoader as ViewDesignLoader; |
| 13 | +use Magento\Framework\Message\ManagerInterface; |
| 14 | +use Magento\Catalog\Block\Product\ImageFactory; |
| 15 | +use Magento\Framework\App\AreaList; |
| 16 | +use Magento\Framework\App\State; |
| 17 | + |
| 18 | +/** |
| 19 | + * Load necessary design files for GraphQL |
| 20 | + */ |
| 21 | +class DesignLoader |
| 22 | +{ |
| 23 | + /** |
| 24 | + * @var DesignLoader |
| 25 | + */ |
| 26 | + private $designLoader; |
| 27 | + |
| 28 | + /** |
| 29 | + * @var ManagerInterface |
| 30 | + */ |
| 31 | + private ManagerInterface $messageManager; |
| 32 | + |
| 33 | + /** |
| 34 | + * Application |
| 35 | + * |
| 36 | + * @var AreaList |
| 37 | + */ |
| 38 | + private AreaList $areaList; |
| 39 | + |
| 40 | + /** |
| 41 | + * Layout |
| 42 | + * |
| 43 | + * @var State |
| 44 | + */ |
| 45 | + private State $appState; |
| 46 | + |
| 47 | + /** |
| 48 | + * @param ViewDesignLoader $designLoader |
| 49 | + * @param ManagerInterface $messageManager |
| 50 | + */ |
| 51 | + public function __construct( |
| 52 | + ViewDesignLoader $designLoader, |
| 53 | + ManagerInterface $messageManager, |
| 54 | + AreaList $areaList, |
| 55 | + State $appState |
| 56 | + ) { |
| 57 | + $this->designLoader = $designLoader; |
| 58 | + $this->messageManager = $messageManager; |
| 59 | + $this->areaList = $areaList; |
| 60 | + $this->appState = $appState; |
| 61 | + } |
| 62 | + |
| 63 | + /** |
| 64 | + * Before create load the design files |
| 65 | + * |
| 66 | + * @param ImageFactory $subject |
| 67 | + * @param Product $product |
| 68 | + * @param string $imageId |
| 69 | + * @param array|null $attributes |
| 70 | + * |
| 71 | + * @SuppressWarnings(PHPMD.UnusedFormalParameter) |
| 72 | + */ |
| 73 | + public function beforeCreate( |
| 74 | + ImageFactory $subject, |
| 75 | + Product $product, |
| 76 | + string $imageId, |
| 77 | + array $attributes = null |
| 78 | + ) { |
| 79 | + try { |
| 80 | + $area = $this->areaList->getArea($this->appState->getAreaCode()); |
| 81 | + $area->load(\Magento\Framework\App\Area::PART_DESIGN); |
| 82 | + } catch (\Magento\Framework\Exception\LocalizedException $e) { |
| 83 | + if ($e->getPrevious() instanceof \Magento\Framework\Config\Dom\ValidationException) { |
| 84 | + /** @var MessageInterface $message */ |
| 85 | + $message = $this->messageManager |
| 86 | + ->createMessage(MessageInterface::TYPE_ERROR) |
| 87 | + ->setText($e->getMessage()); |
| 88 | + $this->messageManager->addUniqueMessages([$message]); |
| 89 | + } |
| 90 | + } |
| 91 | + } |
| 92 | +} |
0 commit comments