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
+
8
15
/**
9
16
* Cms Static Block Widget
10
17
*
11
- * @author Magento Core Team <core@magentocommerce.com>
18
+ * @author Magento Core Team <core@magentocommerce.com>
12
19
*/
13
- class Block extends \Magento \Framework \View \Element \Template implements \ Magento \ Widget \ Block \BlockInterface
20
+ class Block extends \Magento \Framework \View \Element \Template implements IdentityInterface
14
21
{
15
22
/**
16
23
* @var \Magento\Cms\Model\Template\FilterProvider
@@ -31,6 +38,11 @@ class Block extends \Magento\Framework\View\Element\Template implements \Magento
31
38
*/
32
39
protected $ _blockFactory ;
33
40
41
+ /**
42
+ * @var CmsBlock
43
+ */
44
+ private $ block ;
45
+
34
46
/**
35
47
* @param \Magento\Framework\View\Element\Template\Context $context
36
48
* @param \Magento\Cms\Model\Template\FilterProvider $filterProvider
@@ -65,19 +77,61 @@ protected function _beforeToHtml()
65
77
}
66
78
self ::$ _widgetUsageMap [$ blockHash ] = true ;
67
79
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 ()) {
80
+ $ block = $ this ->getBlock ();
81
+
82
+ if ($ block && $ block ->isActive ()) {
83
+ try {
84
+ $ storeId = $ this ->_storeManager ->getStore ()->getId ();
74
85
$ this ->setText (
75
86
$ this ->_filterProvider ->getBlockFilter ()->setStoreId ($ storeId )->filter ($ block ->getContent ())
76
87
);
88
+ } catch (NoSuchEntityException $ e ) {
77
89
}
78
90
}
79
-
80
91
unset(self ::$ _widgetUsageMap [$ blockHash ]);
81
92
return $ this ;
82
93
}
94
+
95
+ /**
96
+ * Get identities of the Cms Block
97
+ *
98
+ * @return array
99
+ */
100
+ public function getIdentities ()
101
+ {
102
+ $ block = $ this ->getBlock ();
103
+
104
+ if ($ block ) {
105
+ return $ block ->getIdentities ();
106
+ }
107
+
108
+ return [];
109
+ }
110
+
111
+ /**
112
+ * @return CmsBlock|null
113
+ */
114
+ private function getBlock (): ?CmsBlock
115
+ {
116
+ if ($ this ->block ) {
117
+ return $ this ->block ;
118
+ }
119
+
120
+ $ blockId = $ this ->getData ('block_id ' );
121
+
122
+ if ($ blockId ) {
123
+ try {
124
+ $ storeId = $ this ->_storeManager ->getStore ()->getId ();
125
+ /** @var \Magento\Cms\Model\Block $block */
126
+ $ block = $ this ->_blockFactory ->create ();
127
+ $ block ->setStoreId ($ storeId )->load ($ blockId );
128
+ $ this ->block = $ block ;
129
+
130
+ return $ block ;
131
+ } catch (NoSuchEntityException $ e ) {
132
+ }
133
+ }
134
+
135
+ return null ;
136
+ }
83
137
}
0 commit comments