How to Get All CMS Pages In System Configuration in Magento 2
CMS pages are static pages that allow adding text, video, photos, etc and provide the facility to create rich content and the ease of managing multiple contents.
The default Magento 2 CMS pages include:
- Home Page
- Category Page
- Product Page
- About Us Page
- Customer service Page
- Privacy policy
- 404 not found
- Enable cookies
- 503 service unavailable
Moreover, the store owner can also create a custom CMS page as per requirements.
One often has to work with CMS pages while working with Magento 2 extension development or customization.
For instance, the admin needs to choose and exclude CMS page from a list of all CMS pages in system configuration as mentioned in the below image:
In that scenario, you need to get all CMS pages in system configuration in Magento 2.
Use the below code the right way and you are done!
Steps to Get All CMS Pages In System Configuration in Magento 2
- Create registration.php file at app/code/Vendor/Module.
123456789<?phpuse Magento\Framework\Component\ComponentRegistrar;ComponentRegistrar::register(ComponentRegistrar::MODULE,'Vendor_Module',__DIR__); - Create module.xml at app/code/Vendor/Module/etc and use the below code.
123456<?xml version="1.0"?><config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd"><module name="Vendor_Module" setup_version="1.0.0"></module></config> - Create system.xml at app/code/Vendor/Module/etc/adminhtml.
1234567891011121314151617181920212223<?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="vendor" translate="label" sortOrder="100"><label>vendor</label></tab><section id="section_id" translate="label" type="text" sortOrder="320" showInDefault="1" showInWebsite="1"showInStore="1"><label>Module Name</label><tab>vendor</tab><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>\Vendor\Module\Model\Config\Source\CmsPages</source_model></field></group></section></system></config> - Create CmsPages.php file at app/code/Vendor/Module/Model/Config/Source.
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980<?phpnamespace Vendor\Module\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 CmsPages 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();}} 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;}}
That’s it. Make your extensions work expectedly by certain configurations that are made to have particular values for that you need to set Magento 2 default value system configuration.
Any doubts on this topic can be discussed in the Comments section below. I’d be happy to solve them.
Feel free to share the above solution with fellow developers via social media.
Thanks.
Sanjay Jethva
Sanjay is the co-founder and CTO of Meetanshi with hands-on expertise with Magento since 2011. He specializes in complex development, integrations, extensions, and customizations. Sanjay is one the top 50 contributor to the Magento community and is recognized by Adobe.
His passion for Magento 2 and Shopify solutions has made him a trusted source for businesses seeking to optimize their online stores. He loves sharing technical solutions related to Magento 2 & Shopify.
Prev
How to Show Breadcrumbs on Product Page in Magento 2
How to Add Custom Content in Order Summary on Checkout Page in Magento 2
Next