You can get all the CMS Static Blocks collection in Magento 2 by using interface, Magento\Cms\Api\BlockRepositoryInterface.
Magento\Cms\Api\BlockRepositoryInterface is used for getting CMS Static Blocks related data in Magento 2.
You need to instantiate Magento\Cms\Api\BlockRepositoryInterface in __construct() method of Class
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
<?php namespace VendorName\ModuleName\Block; class Filename extends \Magento\Framework\View\Element\Template { public function __construct( \Magento\Framework\View\Element\Template\Context $context, \Magento\Cms\Api\BlockRepositoryInterface $blockRepository, \Magento\Framework\Api\SearchCriteriaBuilder $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.
1 2 3 4 5 6 7 |
<?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.
[crayon-63e0aaa22c529755468543/] Using above fucntion Images can be imported directly from...
Override view block using di.xml and add the below code...
You can check a list of called layout XML for...
Follow the below steps to install and set up PWA...
If you want to remove all leading zero's from order,...
Let our Magento expert connect to discuss your requirement.
We offer Magento
certified developers.
Our Magento clientele
is 500+.
We sign NDA for the
security of your projects.
We’ve performed 100+
Magento migration projects.
Free quotation
on your project.
Three months warranty on
code developed by us.