We sacrifice by not doing any other technology, so that you get the best of Magento.

We sacrifice by not doing any other technology, so that you get the best of Magento.

    You can get all the CMS Static Blocks collection in Magento 2 by using interface, MagentoCmsApiBlockRepositoryInterface.

    MagentoCmsApiBlockRepositoryInterface is used for getting CMS Static Blocks related data in Magento 2.

    You need to instantiate MagentoCmsApiBlockRepositoryInterface in __construct() method of Class

    <?php
    namespace VendorNameModuleNameBlock;
    
    class Filename extends MagentoFrameworkViewElementTemplate
    {
        public function __construct(
            MagentoFrameworkViewElementTemplateContext $context,
            MagentoCmsApiBlockRepositoryInterface $blockRepository,
            MagentoFrameworkApiSearchCriteriaBuilder $searchCriteriaBuilder,
            array $data = []
        ) {
            $this->blockRepository = $blockRepository;
            $this->searchCriteriaBuilder = $searchCriteriaBuilder;
            parent::__construct($context, $data);
        }
    
      	/* Get Cms Blocks Collection from store. */
        public function getCmsBlock() {
            $searchCriteria = $this->searchCriteriaBuilder->create();
            $cmsBlocks = $this->blockRepository->getList($searchCriteria)->getItems();
            return $cmsBlocks;
        }

    From Template file, you can access all the Static Blocks by iterating over a loop on a collection.

    <?php
    	$cmsBlocks = $block->getCmsBlock();
    	foreach($cmsBlocks as $cmsBlock) {
    	    echo $cmsBlock->getId(); // get Id
    	    echo $cmsBlock->getTitle(); // get Title of CMS Static Block
    	}
    ?>

    After using the above code snippet you can get all the Cms Static Blocks from a store.

    Fill the below form if you need any Magento relate help/advise/consulting.

    With Only Agency that provides a 24/7 emergency support.

      Get a Free Quote