3
3
* Copyright © Magento, Inc. All rights reserved.
4
4
* See COPYING.txt for license details.
5
5
*/
6
+
7
+ declare (strict_types=1 );
8
+
6
9
namespace Magento \Cms \Block \Widget ;
7
10
11
+ use Magento \Framework \DataObject \IdentityInterface ;
12
+ use Magento \Framework \Exception \NoSuchEntityException ;
13
+ use Magento \Cms \Model \Block as CmsBlock ;
14
+ use Magento \Widget \Block \BlockInterface ;
15
+
8
16
/**
9
17
* Cms Static Block Widget
10
18
*
11
- * @author Magento Core Team <[email protected] >
19
+ * @author Magento Core Team <[email protected] >
12
20
*/
13
- class Block extends \Magento \Framework \View \Element \Template implements \ Magento \ Widget \ Block \ BlockInterface
21
+ class Block extends \Magento \Framework \View \Element \Template implements BlockInterface, IdentityInterface
14
22
{
15
23
/**
16
24
* @var \Magento\Cms\Model\Template\FilterProvider
@@ -31,6 +39,11 @@ class Block extends \Magento\Framework\View\Element\Template implements \Magento
31
39
*/
32
40
protected $ _blockFactory ;
33
41
42
+ /**
43
+ * @var CmsBlock
44
+ */
45
+ private $ block ;
46
+
34
47
/**
35
48
* @param \Magento\Framework\View\Element\Template\Context $context
36
49
* @param \Magento\Cms\Model\Template\FilterProvider $filterProvider
@@ -49,8 +62,9 @@ public function __construct(
49
62
}
50
63
51
64
/**
52
- * Prepare block text and determine whether block output enabled or not
53
- * Prevent blocks recursion if needed
65
+ * Prepare block text and determine whether block output enabled or not.
66
+ *
67
+ * Prevent blocks recursion if needed.
54
68
*
55
69
* @return $this
56
70
*/
@@ -65,19 +79,63 @@ protected function _beforeToHtml()
65
79
}
66
80
self ::$ _widgetUsageMap [$ blockHash ] = true ;
67
81
68
- if ($ blockId ) {
69
- $ storeId = $ this ->_storeManager ->getStore ()->getId ();
70
- /** @var \Magento\Cms\Model\Block $block */
71
- $ block = $ this ->_blockFactory ->create ();
72
- $ block ->setStoreId ($ storeId )->load ($ blockId );
73
- if ($ block ->isActive ()) {
82
+ $ block = $ this ->getBlock ();
83
+
84
+ if ($ block && $ block ->isActive ()) {
85
+ try {
86
+ $ storeId = $ this ->_storeManager ->getStore ()->getId ();
74
87
$ this ->setText (
75
88
$ this ->_filterProvider ->getBlockFilter ()->setStoreId ($ storeId )->filter ($ block ->getContent ())
76
89
);
90
+ } catch (NoSuchEntityException $ e ) {
77
91
}
78
92
}
79
-
80
93
unset(self ::$ _widgetUsageMap [$ blockHash ]);
81
94
return $ this ;
82
95
}
96
+
97
+ /**
98
+ * Get identities of the Cms Block
99
+ *
100
+ * @return array
101
+ */
102
+ public function getIdentities ()
103
+ {
104
+ $ block = $ this ->getBlock ();
105
+
106
+ if ($ block ) {
107
+ return $ block ->getIdentities ();
108
+ }
109
+
110
+ return [];
111
+ }
112
+
113
+ /**
114
+ * Get block
115
+ *
116
+ * @return CmsBlock|null
117
+ */
118
+ private function getBlock (): ?CmsBlock
119
+ {
120
+ if ($ this ->block ) {
121
+ return $ this ->block ;
122
+ }
123
+
124
+ $ blockId = $ this ->getData ('block_id ' );
125
+
126
+ if ($ blockId ) {
127
+ try {
128
+ $ storeId = $ this ->_storeManager ->getStore ()->getId ();
129
+ /** @var \Magento\Cms\Model\Block $block */
130
+ $ block = $ this ->_blockFactory ->create ();
131
+ $ block ->setStoreId ($ storeId )->load ($ blockId );
132
+ $ this ->block = $ block ;
133
+
134
+ return $ block ;
135
+ } catch (NoSuchEntityException $ e ) {
136
+ }
137
+ }
138
+
139
+ return null ;
140
+ }
83
141
}
0 commit comments