2

What code would be required to get a complete list of cms blocks in Magento 2. What I mean is block collection like in Magento 1

I would try to keep to repository interfaces like this following:

$objectManager->get(\Magento\Cms\Api\BlockRepositoryInterface::class)

But I would need to also have Magento\Framework\Api\SearchCriteriaInterface passed and it starts to become over the top for just getting the block collection.

What is best and correct method for this simple job?

1 Answer 1

5

below is the one of way

<?php
namespace Sugarcode\Test\Block;

class Test extends \Magento\Framework\View\Element\Template
{
    protected $blockColFactory;

    public function __construct(
        \Magento\Backend\Block\Template\Context $context,
        \Magento\Cms\Model\ResourceModel\Block\CollectionFactory $blockColFactory,
        array $data = []
    ) {
        $this->blockColFactory = $blockColFactory;
        parent::__construct($context, $data);
    }



    public function _prepareLayout()
    {
        return parent::_prepareLayout();
    }
    public function getBlockCOl()
    {
        $data = $this->blockColFactory->create();

        return $data;

    }
}

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.