1

In my custom module, I have a field in the system configwith cmspages. I need to get the id of the selected cms page, not the identifier, so I can get the content of a cms page in a phtml file.

I have the following lines in my system.xml

<cms_page translate="label">
         <sort_order>140</sort_order>
         <label>Cookie Information Page</label>
         <comment>Use this page for learn about cookie settings.</comment>
         <frontend_type>select</frontend_type>
         <source_model>adminhtml/system_config_source_cms_page</source_model>
         <show_in_default>1</show_in_default>
         <show_in_website>1</show_in_website>
         <show_in_store>1</show_in_store>
</cms_page>

This is my Model/Adminhtml/System/Config/Source/Page.php of my module

public function toOptionArray()
{
    $pages = array();
    $cmspages = Mage::getModel('cms/page')->getCollection();
    foreach($cmspages as $page){
        $pages[] = array('label' => $page->getTitle(), 'value' => $page->getPageId());
    }

    return $pages;
}

config.xml

<config>
<modules>
    <My_Module>
        <version>1.3</version>
    </My_Module>
</modules>
<frontend>
    <translate>
        <modules>
            <My_Module>
                <files>
                    <default>My_Module.csv</default>
                </files>
            </My_Module>
        </modules>
    </translate>
    <layout>
        <updates>
            <my_module>
                <file>my/module.xml</file>
            </my_module>
        </updates>
    </layout>
    <routers>
        <my_module>
            <use>standard</use>
            <args>
                <module>My_Module</module>
                <frontName>my_module</frontName>
            </args>
        </my_module>
    </routers>
</frontend>
<admin>
    <routers>
        <adminhtml>
            <args>
                <modules>
                    <my_module_ctrl before="Mage_Adminhtml">My_Module_Adminhtml</my_module_ctrl>
                </modules>
            </args>
        </adminhtml>
    </routers>
</admin>
<adminhtml>
    <translate>
        <modules>
            <My_Module>
                <files>
                    <default>My_Module.csv</default>
                </files>
            </My_Module>
        </modules>
    </translate>
</adminhtml>
<global>
    <blocks>
        <my_module>
            <class>My_Module_Block</class>
        </my_module>
    </blocks>
    <models>
        <my_module>
            <class>My_Module_Model</class>
        </my_module>
    </models>
    <helpers>
        <my_module>
            <class>My_Module_Helper</class>
        </my_module>
    </helpers>
    <events>
        <adminhtml_widget_container_html_before>
            <observers>
                <my_module_model_observer_buttons>
                    <class>My_Module_Model_Observer</class>
                    <method>addButtons</method>
                </my_module_model_observer_buttons>
            </observers>
        </adminhtml_widget_container_html_before>
        <controller_action_layout_load_before>
            <observers>
                <my_module_model_observer_blocks>
                    <class>My_Module_Model_Observer</class>
                    <method>removeBlocks</method>
                </my_module_model_observer_blocks>
            </observers>
        </controller_action_layout_load_before>
    </events>
    <resources>
        <my_module_setup>
            <setup>
                <module>My_Module</module>
            </setup>
        </my_module_setup>
    </resources>
</global>

  • Something like <block_id> your block_id </block_id> – BartZalas Jul 5 '18 at 9:39
  • Check my answer. – Sukumar Gorai Jul 5 '18 at 10:30
  • can you add your config.xml code? – Sukumar Gorai Jul 6 '18 at 14:27
3

Below is working code, replace your system.xml code with the below one:

<cms_page translate="label">
    <sort_order>140</sort_order>
    <label>Cookie Information Page</label>
    <comment>Use this page for learn about cookie settings.</comment>
    <frontend_type>select</frontend_type>
    <source_model>[Vendor]_[Module]_Model_Adminhtml_System_Config_Source_Cms_Page</source_model>
    <show_in_default>1</show_in_default>
    <show_in_website>1</show_in_website>
    <show_in_store>1</show_in_store>
</cms_page>

Create Page.php file under the below location:

[Vendor]/[Module]/Model/Adminhtml/System/Config/Source/Cms/Page.php

and add the following code it Page.php :

<?php
class [Vendor]_[Module]_Model_Adminhtml_System_Config_Source_Cms_Page
{
   public function toOptionArray()
   {
        $pages = array();
        $cmspages = Mage::getModel('cms/page')->getCollection();
        foreach($cmspages as $page){
            $pages[] = array('label' => $page->getTitle(), 'value' => $page->getPageId());
        }

       return $pages;
   }
}

Note: Replace the [Vendor], [Module] with your namespace and module name.

| improve this answer | |
  • Fatal error: Call to a member function toOptionArray() on a non-object in /data/web/public/app/code/core/Mage/Adminhtml/Block/System/Config/Form.php on line 463 I get this error. – Lucas Jul 6 '18 at 13:13
  • Did you create any module? – Sukumar Gorai Jul 6 '18 at 13:24
  • Can you post your module files in your question? I think you have not created the model I have mentioned. – Sukumar Gorai Jul 6 '18 at 13:25
0

No need to create custom class for this, magento already has it

<source_model>adminhtml/system_config_source_cms_page</source_model>
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.