Get CMS Static Block collection by identifier in Magento 2.

You can get CMS Static Blocks collection by an identifier in Magento 2 by calling BlockRepositoryInterface interface.

Magento\Cms\Api\BlockRepositoryInterface used for getting CMS Static Blocks in Magento 2.

BlockRepositoryInterface is used when you need to fetch a collection of CMS Static blocks or get specific CMS static block data.

You need to instantiate BlockRepositoryInterface in __construct() method of Class.

I wil give you demo using Block Class,

Get CMS Static Block by identifier, You need to pass identifier in searchCriteriaBuilder addFilter() method,
Let’s assume CMS Static Block identifier is footer_links_block,

$searchCriteria = $this->searchCriteriaBuilder->addFilter(‘identifier’, ‘footer_links_block’,’eq’)->create();
We have used equal conditions, You can set any conditions as third parameter in above function as per your needs.

List of Conditions are, ‘eq’,’neq’,’like’,’nlike’,’in’,’nin’,’notnull’,’null’,’gt’,’lt’,’gteq’,’lteq’,’finset’

From Template file, You can access all the Static Block by an iterating loa op over a collection.

Based on above Code snippet you get all the Cms Static Blocks from store. You can also get specific Static Blocks based on your custom condition by refer link.

One Reply to “Get CMS Static Block collection by identifier in Magento 2.”

Leave a Reply

Your email address will not be published. Required fields are marked *