9
9
10
10
use Magento \Cms \Api \BlockRepositoryInterface ;
11
11
use Magento \Cms \Api \Data \BlockInterface ;
12
+ use Magento \Framework \Api \SearchCriteriaBuilder ;
12
13
use Magento \Framework \Exception \NoSuchEntityException ;
14
+ use Magento \Store \Model \StoreManagerInterface ;
13
15
use Magento \Widget \Model \Template \FilterEmulate ;
14
16
15
17
/**
@@ -26,17 +28,35 @@ class Block
26
28
* @var FilterEmulate
27
29
*/
28
30
private $ widgetFilter ;
31
+
32
+ /**
33
+ * @var \Magento\Framework\Api\SearchCriteriaBuilder
34
+ */
35
+ protected $ searchCriteriaBuilder ;
36
+
37
+ /**
38
+ * @var \Magento\Store\Model\StoreManagerInterface
39
+ */
40
+ private $ storeManager ;
29
41
30
42
/**
31
43
* @param BlockRepositoryInterface $blockRepository
32
44
* @param FilterEmulate $widgetFilter
45
+ * @param SearchCriteriaBuilder $searchCriteriaBuilder
46
+ * @param StoreManagerInterface $storeManager
33
47
*/
34
48
public function __construct (
35
49
BlockRepositoryInterface $ blockRepository ,
36
- FilterEmulate $ widgetFilter
50
+ FilterEmulate $ widgetFilter ,
51
+ SearchCriteriaBuilder $ searchCriteriaBuilder = null ,
52
+ StoreManagerInterface $ storeManager = null
37
53
) {
38
54
$ this ->blockRepository = $ blockRepository ;
39
55
$ this ->widgetFilter = $ widgetFilter ;
56
+ $ this ->searchCriteriaBuilder = $ searchCriteriaBuilder ?: \Magento \Framework
57
+ \App \ObjectManager::getInstance ()->get (SearchCriteriaBuilder::class);
58
+ $ this ->storeManager = $ storeManager ?: \Magento \Framework
59
+ \App \ObjectManager::getInstance ()->get (StoreManagerInterface::class);
40
60
}
41
61
42
62
/**
@@ -48,9 +68,13 @@ public function __construct(
48
68
*/
49
69
public function getData (string $ blockIdentifier ): array
50
70
{
51
- $ block = $ this ->blockRepository ->getById ($ blockIdentifier );
71
+ $ searchCriteria = $ this ->searchCriteriaBuilder
72
+ ->addFilter ('identifier ' , $ blockIdentifier , 'eq ' )
73
+ ->addFilter ('store_id ' , $ this ->storeManager ->getStore ()->getId (), 'eq ' )
74
+ ->addFilter ('is_active ' , true , 'eq ' )->create ();
75
+ $ block = current ($ this ->blockRepository ->getList ($ searchCriteria )->getItems ());
52
76
53
- if (false === $ block-> isActive ( )) {
77
+ if (empty ( $ block )) {
54
78
throw new NoSuchEntityException (
55
79
__ ('The CMS block with the "%1" ID doesn \'t exist. ' , $ blockIdentifier )
56
80
);
0 commit comments