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.
If you want get query string params in controller file,...
Create di.xml and add the below code Magemonkey/Redirect/etc/frontend/di.xml [crayon-62838fdc54978914457007/] Create...
You can try below code to change local date to...
Step 1: First you need to add registration.php file in...
Step1 : Override message.js in current theme file on the...