Magento, Magento 2

Magento 2 : Get CMS Page Collection by Identifier

Magento 2 Get CMS Page Collection by Identifier

In this tutorial, Today we will learn about how to get CMS page collection by identifier in Magento 2.

For that, You need to inject \Magento\Cms\Api\PageRepositoryInterface into your construct. PageRepositoryInterface is an interface that is used when you need to get the collection of CMS page, Delete CMS page, Delete or get CMS Page data by specific CMS page ID, etc.

We need to also inject \Magento\Framework\Api\SearchCriteriaBuilder class into your construct to add filter by identifier of cms page.

Let’s follow the steps :

You may also like this :

1) Let’s assume that you have created a module. After that, Create a block file CmsPageData.php file and paste the below code :

<?php
/**
 * Created By : Rohan Hapani
 */
namespace RH\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\SearchCriteriaBuilder     $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';
        }
    }
}

2) Now, You need to add your block into the layout XML file to access function in your phtml file. Then, you can use this function by using this below code :

<?php
    /**
     * Created By : Rohan Hapani
     */
    $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
        }
    }
?>

I hope this blog will helpful for easily understand how to get cms page collection by identifier in Magento 2. In case, I missed anything or need to add some information, always feel free to leave a comment in this blog, I’ll get back with proper solution 🙂

Tagged ,