2

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

3

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:

getList result

and block looks as usual:

block

1
  • @AndréFerraz my solution does not work for you? Commented Aug 10, 2016 at 9:35
1

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...
            }
        }
3
  • Like I said the load function is deprecated. Commented Aug 5, 2016 at 14:04
  • Load is used in magento 2.1 this function is given from magento cms core module Commented Aug 5, 2016 at 15:51
  • but is deprecated, which is not recommended to be used. Commented Aug 5, 2016 at 23:01

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.