Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
BlockRepository.php
Go to the documentation of this file.
1 <?php
7 namespace Magento\Cms\Model;
8 
12 use Magento\Cms\Model\ResourceModel\Block\CollectionFactory as BlockCollectionFactory;
20 
26 {
30  protected $resource;
31 
35  protected $blockFactory;
36 
41 
46 
50  protected $dataObjectHelper;
51 
56 
60  protected $dataBlockFactory;
61 
65  private $storeManager;
66 
70  private $collectionProcessor;
71 
83  public function __construct(
85  BlockFactory $blockFactory,
86  \Magento\Cms\Api\Data\BlockInterfaceFactory $dataBlockFactory,
87  BlockCollectionFactory $blockCollectionFactory,
88  Data\BlockSearchResultsInterfaceFactory $searchResultsFactory,
91  StoreManagerInterface $storeManager,
92  CollectionProcessorInterface $collectionProcessor = null
93  ) {
94  $this->resource = $resource;
95  $this->blockFactory = $blockFactory;
96  $this->blockCollectionFactory = $blockCollectionFactory;
97  $this->searchResultsFactory = $searchResultsFactory;
98  $this->dataObjectHelper = $dataObjectHelper;
99  $this->dataBlockFactory = $dataBlockFactory;
100  $this->dataObjectProcessor = $dataObjectProcessor;
101  $this->storeManager = $storeManager;
102  $this->collectionProcessor = $collectionProcessor ?: $this->getCollectionProcessor();
103  }
104 
112  public function save(Data\BlockInterface $block)
113  {
114  if (empty($block->getStoreId())) {
115  $block->setStoreId($this->storeManager->getStore()->getId());
116  }
117 
118  try {
119  $this->resource->save($block);
120  } catch (\Exception $exception) {
121  throw new CouldNotSaveException(__($exception->getMessage()));
122  }
123  return $block;
124  }
125 
133  public function getById($blockId)
134  {
135  $block = $this->blockFactory->create();
136  $this->resource->load($block, $blockId);
137  if (!$block->getId()) {
138  throw new NoSuchEntityException(__('The CMS block with the "%1" ID doesn\'t exist.', $blockId));
139  }
140  return $block;
141  }
142 
151  public function getList(\Magento\Framework\Api\SearchCriteriaInterface $criteria)
152  {
154  $collection = $this->blockCollectionFactory->create();
155 
156  $this->collectionProcessor->process($criteria, $collection);
157 
159  $searchResults = $this->searchResultsFactory->create();
160  $searchResults->setSearchCriteria($criteria);
161  $searchResults->setItems($collection->getItems());
162  $searchResults->setTotalCount($collection->getSize());
163  return $searchResults;
164  }
165 
173  public function delete(Data\BlockInterface $block)
174  {
175  try {
176  $this->resource->delete($block);
177  } catch (\Exception $exception) {
178  throw new CouldNotDeleteException(__($exception->getMessage()));
179  }
180  return true;
181  }
182 
191  public function deleteById($blockId)
192  {
193  return $this->delete($this->getById($blockId));
194  }
195 
202  private function getCollectionProcessor()
203  {
204  if (!$this->collectionProcessor) {
205  $this->collectionProcessor = \Magento\Framework\App\ObjectManager::getInstance()->get(
206  'Magento\Cms\Model\Api\SearchCriteria\BlockCollectionProcessor'
207  );
208  }
209  return $this->collectionProcessor;
210  }
211 }
$storeManager
__()
Definition: __.php:13
$block
Definition: block.php:8
getList(\Magento\Framework\Api\SearchCriteriaInterface $searchCriteria)
save(Data\BlockInterface $block)
__construct(ResourceBlock $resource, BlockFactory $blockFactory, \Magento\Cms\Api\Data\BlockInterfaceFactory $dataBlockFactory, BlockCollectionFactory $blockCollectionFactory, Data\BlockSearchResultsInterfaceFactory $searchResultsFactory, DataObjectHelper $dataObjectHelper, DataObjectProcessor $dataObjectProcessor, StoreManagerInterface $storeManager, CollectionProcessorInterface $collectionProcessor=null)