I've created a widget, this widgets has a CMS block chooser on the block class I have the following function:
public function getCmsBlock()
{
$blockId = $this->getData("block_id");
$storeId = $this->_storeManager->getStore()->getId();
$block = $this->_blockFactory
->create()
->setStoreId($storeId)
->load($blockId);
return $block;
}
The $this->_storeManager class variable is an instant of Magento\Cms\Model\BlockFactory I realised that the load function is deprecated and the setStoreId doesn't really work.
What is the alternative for loading a CMS block programatically where I would be able to set the store ID and check if the CMS is active?
2 Answers 2
Try to use Magento\Cms\Model\BlockRepository to get block by ID, like this:
/**
* @var \Magento\Cms\Model\BlockRepository
*/
protected $blockRepository;
/**
* @param \Magento\Cms\Model\BlockRepository $blockRepository
*/
public function __construct(
\Magento\Cms\Model\BlockRepository $blockRepository
) {
$this->blockRepository = $blockRepository;
}
public function getBlockById($id, $storeId)
{
$block = $this->blockRepository->getById($id)
->setStoreId($storeId);
return $block;
}
In addition you can use the method getList() to get block like this:
/**
* @var \Magento\Cms\Model\BlockRepository
*/
protected $blockRepository;
/**
* @var \Magento\Framework\Api\FilterBuilder
*/
protected $filterBuilder;
/**
* @var \Magento\Framework\Api\Search\FilterGroup
*/
protected $filterGroup;
/**
* @var \Magento\Framework\Api\SearchCriteriaInterface
*/
protected $searchCriteria;
/**
* @param \Magento\Cms\Model\BlockRepository $blockRepository
* @param \Magento\Framework\Api\SearchCriteriaInterface $criteria
* @param \Magento\Framework\Api\Search\FilterGroup $filterGroup
* @param \Magento\Framework\Api\FilterBuilder $filterBuilder
*/
public function __construct(
\Magento\Cms\Model\BlockRepository $blockRepository,
\Magento\Framework\Api\SearchCriteriaInterface $criteria,
\Magento\Framework\Api\Search\FilterGroup $filterGroup,
\Magento\Framework\Api\FilterBuilder $filterBuilder
) {
$this->blockRepository = $blockRepository;
$this->searchCriteria = $criteria;
$this->filterGroup = $filterGroup;
$this->filterBuilder = $filterBuilder;
}
/**
* @return \Magento\Cms\Model\Block|null
* @throws \Magento\Framework\Exception\NoSuchEntityException
*/
protected function getBlockData()
{
$block = null;
$storeId = 0;
$blockId = 'test-block';
$this->filterGroup->setFilters([
$this->filterBuilder->create()
->setField('store_id')
->setValue($storeId)
->setConditionType('eq'), // default value
$this->filterBuilder->create()
->setField('identifier')
->setValue($blockId),
]);
$this->searchCriteria->setFilterGroups([$this->filterGroup]);
$blocks = $this->blockRepository->getList($this->searchCriteria);
if ($blocks->getItems()) {
/** @var \Magento\Cms\Model\Block $block */
$block = $this->blockRepository->getById($blockId);
}
return $block;
}
The getList() result should look like this:
and block looks as usual:
-
@AndréFerraz my solution does not work for you?Siarhey Uchukhlebau– Siarhey Uchukhlebau2016年08月10日 09:35:17 +00:00Commented Aug 10, 2016 at 9:35
This is the working code,
$blockId = 2;
$html = '';
if ($blockId) {
$storeId = $this->_storeManager->getStore()->getId();
/** @var \Magento\Cms\Model\Block $block */
$block = $this->_blockFactory->create();
$block->setStoreId($storeId)->load($blockId);
if ($block->isActive()) {
//do your code here...
}
}
-
Like I said the load function is deprecated.André Ferraz– André Ferraz2016年08月05日 14:04:42 +00:00Commented Aug 5, 2016 at 14:04
-
Load is used in magento 2.1 this function is given from magento cms core moduleRakesh Jesadiya– Rakesh Jesadiya2016年08月05日 15:51:21 +00:00Commented Aug 5, 2016 at 15:51
-
but is deprecated, which is not recommended to be used.André Ferraz– André Ferraz2016年08月05日 23:01:07 +00:00Commented Aug 5, 2016 at 23:01