
In this blog, we will understand about how to get CMS page list in System Configurations in Magento 2. So, first we will understand about CMS page. Magento 2 CMS pages are type of static pages that allows adding text, video, photos, links etc. Magento 2 provides the facility to create rich content and the ease of managing multiple contents from backend.
By Default, Magento 2 provides the below CMS pages :
- 404 Not Found
- Home Page
- Product Page
- Category Page
- Enable Cookies
- Privacy Policy
- About us
- Customer Service
If you want to create new CMS page then, you have to go to the Content > Pages from here, you can add new CMS page by clicking Add New Page.
Now, to add CMS page list in System Configurations you have to follow below steps :
Step 1: Create a system.xml file at app/code/Mageants/Blog/etc/adminhtml and add the below code in that file.
Note : Here, Mageants is our Vendor name and Blog is Module name you have to add your vendor namer and module name.
<?xml version="1.0" ?> <config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Config:etc/system_file.xsd"> <system> <tab id="mageants" translate="label"> <label>Mageants</label> </tab> <section id="blog" translate="label" type="text" sortOrder="320" showInDefault="1" showInWebsite="1" showInStore="1"> <label>Blog</label> <tab>mageants</tab> <resource>Mageants_Blog::config</resource> <group id="general" translate="label" type="text" sortOrder="10" showInDefault="1" showInWebsite="1" showInStore="1"> <label>CMS Pages</label> <field id="cms_pages" translate="label" type="multiselect" sortOrder="50" showInDefault="1" showInWebsite="1" showInStore="1"> <label>CMS Pages</label> <source_model>Mageants\Blog\Model\Config\Source\GetCmsPages</source_model> </field> </group> </section> </system> </config> |
Step 2 : Create GetCmsPage.php file at app/code/Mageants/Blog/Model/Config/Source and add below code in that file.
<?php namespace Mageants\Blog\Model\Config\Source; use Magento\Cms\Api\Data\PageInterface; use Magento\Cms\Api\PageRepositoryInterface; use Magento\Framework\Api\SearchCriteriaBuilder; use Magento\Framework\App\ObjectManager; use Magento\Framework\Exception\LocalizedException; use Magento\Framework\Option\ArrayInterface; use Psr\Log\LoggerInterface; /** * Class CmsPages * @package Vendor\Module\Model\Config\Source */ class GetCmsPages implements ArrayInterface { /** * @var PageRepositoryInterface */ private $pageRepositoryInterface; /** * @var SearchCriteriaBuilder */ private $searchCriteriaBuilder; /** * CmsPages constructor. * @param PageRepositoryInterface $pageRepositoryInterface * @param SearchCriteriaBuilder $searchCriteriaBuilder */ public function __construct( PageRepositoryInterface $pageRepositoryInterface, SearchCriteriaBuilder $searchCriteriaBuilder ) { $this->pageRepositoryInterface = $pageRepositoryInterface; $this->searchCriteriaBuilder = $searchCriteriaBuilder; } /** * @return array */ public function toOptionArray() { $optionArray = []; try { $pages = $this->getCmsPageCollection(); if ($pages instanceof LocalizedException) { throw $pages; } $cnt = 0; foreach ($pages as $page) { $optionArray[$cnt]['value'] = $page->getIdentifier(); $optionArray[$cnt]['label'] = $page->getTitle(); $cnt++; } } catch (LocalizedException $e) { ObjectManager::getInstance()->get(LoggerInterface::class)->info($e->getMessage()); } catch (\Exception $e) { ObjectManager::getInstance()->get(LoggerInterface::class)->info($e->getMessage()); } return $optionArray; } /** * @return \Exception|PageInterface[]|LocalizedException */ public function getCmsPageCollection() { $searchCriteria = $searchCriteria = $this->searchCriteriaBuilder->create(); try { $collection = $this->pageRepositoryInterface->getList($searchCriteria)->getItems(); } catch (LocalizedException $e) { return $e; } return $collection; } } |
After creating this files you have to run all the Magento commands, after that you can find your CMS page configurations at Store > Configurations > Mageants > Blog as below Screenshot :

Conclusion :
We hope above blog helps you to clearly understand How to Get CMS Page List in System Configuration in Magento 2. In case of any kind of problem with the above code implementation, you can contact us or let us know in comment section.