Today, we’re going to teach you how to get CMS page collection by identifier in Magento 2.
Now, in order to CMS page collection, we will first need to inject \Magento\Cms\Api\PageRepositoryInterface into the construct.
The Page Repository Interface is basically an interface that allows us to get the collection of CMS page, get CMS page data, and even delete CMS page through specific CMS page ID.
Apart from this, we will also have to inject \Magento\Framework\Api\SearchCriteriaBuilder class for adding a filter by identifier of the CMS page.
Once you’ve done that, it’s time to get into the real process get CMS page by identifier in Magento 2.
Steps to Get CMS Page Collection by Identifier in Magento 2
Before we get started, make sure you’ve created a new module. If you don’t know how to create a new module, you can refer to our tutorial on how to create a simple module in Magento 2.
Now, let’s get started!
Step #1
The first step of the process is to create a block file named CmsPageData.php and copy the following code into the file.
<?php /** * Created By : MageDelight Pvt. Ltd. */ namespace MD\CmsPageModule\Block; class CmsPageData extends \Magento\Framework\View\Element\Template { /** * @var \Magento\Cms\Api\PageRepositoryInterface */ protected $pageRepositoryInterface; /** * @var \Magento\Framework\Api\SearchCriteriaBuilder */ protected $searchCriteriaBuilder; /** * @param \Magento\Framework\View\Element\Template\Context $context * @param \Magento\Cms\Api\PageRepositoryInterface $pageRepositoryInterface * @param \Magento\Framework\Api\SfearchCriteriaBuilder $searchCriteriaBuilder * @param array $data */ public function __construct( \Magento\Framework\View\Element\Template\Context $context, \Magento\Cms\Api\PageRepositoryInterface $pageRepositoryInterface, \Magento\Framework\Api\SearchCriteriaBuilder $searchCriteriaBuilder, array $data = [] ) { $this->pageRepositoryInterface = $pageRepositoryInterface; $this->searchCriteriaBuilder = $searchCriteriaBuilder; parent::__construct($context, $data); } /** * Return CMS Page Details by URL Key * * @param string $urlKey * @return string */ public function getCmsPageDetails($urlKey) { if(!empty($urlKey)) { $searchCriteria = $this->searchCriteriaBuilder->addFilter('identifier', $urlKey,'eq')->create(); $pages = $this->pageRepositoryInterface->getList($searchCriteria)->getItems(); return $pages; } else { return 'Page URL Key is invalid'; } } }
Step #2
Next, we need to add the above-created block into our layout XML file for accessing function in the phtml file. Then, copy the following code into the file.
<?php /** * Created By : MageDelight Pvt. Ltd. */ $urlKey = "privacy-policy-cookie-restriction-mode"; // CMS Page URL Key $cmsPageObj = $block->getCmsPageDetails($urlKey); if(count($cmsPageObj) > 0) { foreach ($cmsPageObj as $key => $value) { echo $value->getTitle()."<br/>"; // Cms Page Title echo $value->getContent()."<br/>"; // Cms Page Content } } ?>
And it’s done.
Also read: How to Create Custom CMS Page Layout XML File in Magento 2?
Final Words…
With this, we’ve come to the end of this tutorial. We hope you found this tutorial helpful.
If you have any questions, please ask them in the comments below. And if you need our professional assistance with Magento Web Development, we're always happy to help.