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 \LocalizedException ;
13
+ use Magento \Framework \Exception \NoSuchEntityException ;
14
+ use Magento \Widget \Block \BlockInterface ;
15
+ use Magento \Cms \Model \Block as CmsBlock ;
16
+
8
17
/**
9
18
* Cms Static Block Widget
10
19
*
11
- * @author Magento Core Team <core@magentocommerce.com>
20
+ * @author Magento Core Team <core@magentocommerce.com>
12
21
*/
13
- class Block extends \Magento \Framework \View \Element \Template implements \ Magento \ Widget \ Block \ BlockInterface
22
+ class Block extends \Magento \Framework \View \Element \Template implements BlockInterface, IdentityInterface
14
23
{
15
24
/**
16
25
* @var \Magento\Cms\Model\Template\FilterProvider
@@ -31,6 +40,11 @@ class Block extends \Magento\Framework\View\Element\Template implements \Magento
31
40
*/
32
41
protected $ _blockFactory ;
33
42
43
+ /**
44
+ * @var CmsBlock
45
+ */
46
+ private $ block ;
47
+
34
48
/**
35
49
* @param \Magento\Framework\View\Element\Template\Context $context
36
50
* @param \Magento\Cms\Model\Template\FilterProvider $filterProvider
@@ -65,19 +79,61 @@ 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
+ * @return CmsBlock|null
115
+ */
116
+ private function getBlock (): ?CmsBlock
117
+ {
118
+ if ($ this ->block ) {
119
+ return $ this ->block ;
120
+ }
121
+
122
+ $ blockId = $ this ->getData ('block_id ' );
123
+
124
+ if ($ blockId ) {
125
+ try {
126
+ $ storeId = $ this ->_storeManager ->getStore ()->getId ();
127
+ /** @var \Magento\Cms\Model\Block $block */
128
+ $ block = $ this ->_blockFactory ->create ();
129
+ $ block ->setStoreId ($ storeId )->load ($ blockId );
130
+ $ this ->block = $ block ;
131
+
132
+ return $ block ;
133
+ } catch (NoSuchEntityException $ e ) {
134
+ }
135
+ }
136
+
137
+ return null ;
138
+ }
83
139
}
0 commit comments