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 \CmsGraphQl \Plugin ;
9
+
10
+ use Magento \Catalog \Model \Product ;
11
+ use Magento \Framework \Message \MessageInterface ;
12
+
13
+ /**
14
+ * Load necessary design files for GraphQL
15
+ */
16
+ class DesignLoader
17
+ {
18
+ /**
19
+ * @var \Magento\Framework\View\DesignLoader
20
+ */
21
+ protected $ designLoader ;
22
+
23
+ /**
24
+ * @var \Magento\Framework\Message\ManagerInterface
25
+ */
26
+ protected $ messageManager ;
27
+
28
+ /**
29
+ * @param \Magento\Framework\View\DesignLoader $designLoader
30
+ * @param \Magento\Framework\Message\ManagerInterface $messageManager
31
+ */
32
+ public function __construct (
33
+ \Magento \Framework \View \DesignLoader $ designLoader ,
34
+ \Magento \Framework \Message \ManagerInterface $ messageManager
35
+ ) {
36
+ $ this ->designLoader = $ designLoader ;
37
+ $ this ->messageManager = $ messageManager ;
38
+ }
39
+
40
+ /**
41
+ * Before create load the design files
42
+ *
43
+ * @param \Magento\Catalog\Block\Product\ImageFactory $subject
44
+ * @param Product $product
45
+ * @param string $imageId
46
+ * @param array|null $attributes
47
+ *
48
+ * @SuppressWarnings(PHPMD.UnusedFormalParameter)
49
+ */
50
+ public function beforeCreate (
51
+ \Magento \Catalog \Block \Product \ImageFactory $ subject ,
52
+ Product $ product ,
53
+ string $ imageId ,
54
+ array $ attributes = null
55
+ ) {
56
+ try {
57
+ $ this ->designLoader ->load ();
58
+ } catch (\Magento \Framework \Exception \LocalizedException $ e ) {
59
+ if ($ e ->getPrevious () instanceof \Magento \Framework \Config \Dom \ValidationException) {
60
+ /** @var MessageInterface $message */
61
+ $ message = $ this ->messageManager
62
+ ->createMessage (MessageInterface::TYPE_ERROR )
63
+ ->setText ($ e ->getMessage ());
64
+ $ this ->messageManager ->addUniqueMessages ([$ message ]);
65
+ }
66
+ }
67
+ }
68
+ }
0 commit comments