Categories: How ToMagento 2

How to Get CMS Page List in System Configuration in Magento 2

While developing extensions in Magento 2 or to custom develop some requirements, you may sometimes need to get the list of all CMS pages in admin configuration of which one can select and use. Default Magento doesn’t provide such facility and thus I have come with the tutorial to help you out with this.

Let’s get started with the code to get CMS page list in System –> Configuration:

First of all, go to app -> code -> vendor -> module -> etc -> adminhtml -> system.xml and put below code in the file.


{{VendorName}}\{{module name}}\Model\Config\Source\Cms

Now put the below code in file app -> code -> vendor -> module -> model -> config -> source -> cms.php

namespace {{vemdor}}\{{module}}\Model\Config\Source;

class Cms implements \Magento\Framework\Option\ArrayInterface
{
    public function toOptionArray()
    {
         /**
     * @return array
     */  $res = array();
   $objectManager = \Magento\Framework\App\ObjectManager::getInstance();
    $collection = $objectManager->get('\Magento\Cms\Model\ResourceModel\Page\CollectionFactory')->create();
     $collection->addFieldToFilter('is_active' , \Magento\Cms\Model\Page::STATUS_ENABLED);  
  foreach($collection as $page){
     $data['value'] = $page->getData('identifier');
            $data['label'] = $page->getData('title');
     $res[] = $data;
  } 
  return $res;
    }
}

Once you put both the codes as mentioned above, you can see the list of all CMS pages in System -> Configuration of Magento 2.

Hope this tutorial has helped you implementing in many extensions or to fulfill your custom requirement of CMS page selection. Let me know if you have any doubt or issues regarding, I’ll be happy to assist you any time.

Click to rate this post!
[Total: 5 Average: 4.2]
Dhiren Vasoya

Dhiren Vasoya is a Director and Co-founder at MageComp, Passionate 🎖️ Certified Magento Developer👨‍💻. He has more than 9 years of experience in Magento Development and completed 850+ projects to solve the most important E-commerce challenges. He is fond❤️ of coding and if he is not busy developing then you can find him at the cricket ground, hitting boundaries.🏏

Recent Posts

6 Innovative Tools Revolutionizing E-Commerce Operations

E-commerce has transformed the way consumers shop for products and services and interact with businesses.…

2 days ago

How Upcoming Cookie Changes Will Affect Your E-commerce Website?

The e-commerce world is constantly in flux. New tech and strategies emerge daily to help…

2 days ago

Magento 2: How to Add Header and Footer in Checkout

Hello Magento Friends, In today’s blog, we will discuss adding a header and footer to…

2 days ago

Understanding Flexbox Layout in React Native

Hello React Native Friends, Building a visually appealing and responsive mobile app is crucial in…

4 days ago

HYVÄ Themes Releases: 1.3.6 & 1.3.7 – What’s New

We're thrilled to announce the release of Hyvä Themes 1.3.6 and 1.3.7! These latest updates…

5 days ago

How Modern E-Commerce Platforms Leverage Docker & Kubernetes for Scalability

Your e-commerce platform is surging - orders are rolling in, traffic spikes are becoming the…

5 days ago