11
11
use Magento \Cms \Api \Data \BlockInterface ;
12
12
use Magento \Framework \Api \SearchCriteriaBuilder ;
13
13
use Magento \Framework \Exception \NoSuchEntityException ;
14
- use Magento \Store \Model \StoreManagerInterface ;
15
14
use Magento \Widget \Model \Template \FilterEmulate ;
16
15
17
16
/**
@@ -28,66 +27,88 @@ class Block
28
27
* @var FilterEmulate
29
28
*/
30
29
private $ widgetFilter ;
31
-
32
- /**
33
- * @var \Magento\Framework\Api\SearchCriteriaBuilder
34
- */
35
- protected $ searchCriteriaBuilder ;
36
30
37
31
/**
38
- * @var \Magento\Store\Model\StoreManagerInterface
32
+ * @var SearchCriteriaBuilder
39
33
*/
40
- private $ storeManager ;
34
+ private $ searchCriteriaBuilder ;
41
35
42
36
/**
43
37
* @param BlockRepositoryInterface $blockRepository
44
38
* @param FilterEmulate $widgetFilter
45
39
* @param SearchCriteriaBuilder $searchCriteriaBuilder
46
- * @param StoreManagerInterface $storeManager
47
40
*/
48
41
public function __construct (
49
42
BlockRepositoryInterface $ blockRepository ,
50
43
FilterEmulate $ widgetFilter ,
51
- SearchCriteriaBuilder $ searchCriteriaBuilder = null ,
52
- StoreManagerInterface $ storeManager = null
44
+ SearchCriteriaBuilder $ searchCriteriaBuilder
53
45
) {
54
46
$ this ->blockRepository = $ blockRepository ;
55
47
$ 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);
48
+ $ this ->searchCriteriaBuilder = $ searchCriteriaBuilder ;
60
49
}
61
50
62
51
/**
63
- * Get block data
52
+ * Get block data by identifier
64
53
*
65
54
* @param string $blockIdentifier
55
+ * @param int $storeId
66
56
* @return array
67
57
* @throws NoSuchEntityException
68
58
*/
69
- public function getData (string $ blockIdentifier ): array
59
+ public function getBlockByIdentifier (string $ blockIdentifier , int $ storeId ): array
60
+ {
61
+ $ blockData = $ this ->fetchBlockData ($ blockIdentifier , BlockInterface::IDENTIFIER , $ storeId );
62
+
63
+ return $ blockData ;
64
+ }
65
+
66
+ /**
67
+ * Get block data by block_id
68
+ *
69
+ * @param int $blockId
70
+ * @param int $storeId
71
+ * @return array
72
+ * @throws NoSuchEntityException
73
+ */
74
+ public function getBlockById (int $ blockId , int $ storeId ): array
75
+ {
76
+ $ blockData = $ this ->fetchBlockData ($ blockId , BlockInterface::BLOCK_ID , $ storeId );
77
+
78
+ return $ blockData ;
79
+ }
80
+
81
+ /**
82
+ * Fetch black data by either id or identifier field
83
+ *
84
+ * @param mixed $identifier
85
+ * @param string $field
86
+ * @param int $storeId
87
+ * @return array
88
+ * @throws NoSuchEntityException
89
+ */
90
+ private function fetchBlockData ($ identifier , string $ field , int $ storeId ): array
70
91
{
71
92
$ 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 ());
93
+ ->addFilter ($ field , $ identifier )
94
+ ->addFilter ('store_id ' , $ storeId )
95
+ ->addFilter (BlockInterface::IS_ACTIVE , true )->create ();
76
96
77
- if (empty ($ block )) {
97
+ $ blockResults = $ this ->blockRepository ->getList ($ searchCriteria )->getItems ();
98
+
99
+ if (empty ($ blockResults )) {
78
100
throw new NoSuchEntityException (
79
- __ ('The CMS block with the "%1" ID doesn \'t exist. ' , $ blockIdentifier )
101
+ __ ('The CMS block with the "%1" ID doesn \'t exist. ' , $ identifier )
80
102
);
81
103
}
82
104
105
+ $ block = current ($ blockResults );
83
106
$ renderedContent = $ this ->widgetFilter ->filterDirective ($ block ->getContent ());
84
-
85
- $ blockData = [
107
+ return [
86
108
BlockInterface::BLOCK_ID => $ block ->getId (),
87
109
BlockInterface::IDENTIFIER => $ block ->getIdentifier (),
88
110
BlockInterface::TITLE => $ block ->getTitle (),
89
111
BlockInterface::CONTENT => $ renderedContent ,
90
112
];
91
- return $ blockData ;
92
113
}
93
114
}
0 commit comments