resource = $resource; $this->blockFactory = $blockFactory; $this->blockCollectionFactory = $blockCollectionFactory; $this->searchResultsFactory = $searchResultsFactory; $this->dataObjectHelper = $dataObjectHelper; $this->dataBlockFactory = $dataBlockFactory; $this->dataObjectProcessor = $dataObjectProcessor; $this->storeManager = $storeManager; $this->collectionProcessor = $collectionProcessor ?: $this->getCollectionProcessor(); } /** * Save Block data * * @param \Magento\Cms\Api\Data\BlockInterface $block * @return Block * @throws CouldNotSaveException */ public function save(Data\BlockInterface $block) { if (empty($block->getStoreId())) { $block->setStoreId($this->storeManager->getStore()->getId()); } try { $this->resource->save($block); } catch (\Exception $exception) { throw new CouldNotSaveException(__($exception->getMessage())); } return $block; } /** * Load Block data by given Block Identity * * @param string $blockId * @return Block * @throws \Magento\Framework\Exception\NoSuchEntityException */ public function getById($blockId) { $block = $this->blockFactory->create(); $this->resource->load($block, $blockId); if (!$block->getId()) { throw new NoSuchEntityException(__('The CMS block with the "%1" ID doesn\'t exist.', $blockId)); } return $block; } /** * Load Block data collection by given search criteria * * @SuppressWarnings(PHPMD.CyclomaticComplexity) * @SuppressWarnings(PHPMD.NPathComplexity) * @param \Magento\Framework\Api\SearchCriteriaInterface $criteria * @return \Magento\Cms\Api\Data\BlockSearchResultsInterface */ public function getList(\Magento\Framework\Api\SearchCriteriaInterface $criteria) { /** @var \Magento\Cms\Model\ResourceModel\Block\Collection $collection */ $collection = $this->blockCollectionFactory->create(); $this->collectionProcessor->process($criteria, $collection); /** @var Data\BlockSearchResultsInterface $searchResults */ $searchResults = $this->searchResultsFactory->create(); $searchResults->setSearchCriteria($criteria); $searchResults->setItems($collection->getItems()); $searchResults->setTotalCount($collection->getSize()); return $searchResults; } /** * Delete Block * * @param \Magento\Cms\Api\Data\BlockInterface $block * @return bool * @throws CouldNotDeleteException */ public function delete(Data\BlockInterface $block) { try { $this->resource->delete($block); } catch (\Exception $exception) { throw new CouldNotDeleteException(__($exception->getMessage())); } return true; } /** * Delete Block by given Block Identity * * @param string $blockId * @return bool * @throws CouldNotDeleteException * @throws NoSuchEntityException */ public function deleteById($blockId) { return $this->delete($this->getById($blockId)); } /** * Retrieve collection processor * * @deprecated 102.0.0 * @return CollectionProcessorInterface */ private function getCollectionProcessor() { if (!$this->collectionProcessor) { $this->collectionProcessor = \Magento\Framework\App\ObjectManager::getInstance()->get( 'Magento\Cms\Model\Api\SearchCriteria\BlockCollectionProcessor' ); } return $this->collectionProcessor; } }