What's the source_model for a cms block chooser in magento?

4.4k Views Asked by At

I want to add a config field to my magento instance. You should be able to store a cms block in it.

<block translate="label">
    <label>Cms block</label>
    <frontend_type>select</frontend_type>
    <source_model>???</source_model>
    <sort_order>30</sort_order>
    <show_in_default>1</show_in_default>
    <show_in_website>1</show_in_website>
    <show_in_store>1</show_in_store>
</block>

But I only found the model for cms pages (adminhtml/system_config_source_cms_page).

What is the corresponding source_model for cms blocks?

4

There are 4 best solutions below

1
On BEST ANSWER

I think class Mage_Cms_Model_Resource_Block_Collection works great for this:

                    <cms_block translate="label">
                      <label>Left template CMS block</label>
                      <frontend_type>select</frontend_type>
                      <source_model>cms/resource_block_collection</source_model>
                      <sort_order>0</sort_order>     
                      <show_in_default>1</show_in_default>
                      <show_in_website>0</show_in_website>
                      <show_in_store>0</show_in_store>
                    </cms_block>   
0
On

There aren't any, but you can make yours :

class Your_Module_Model_System_Config_Source_Cms_Block
{
    protected $_options;

    public function toOptionArray()
    {
        if (!$this->_options) {
            $this->_options = Mage::getResourceModel('cms/block_collection')
                ->load()
                ->toOptionArray();
        }
        return $this->_options;
    }
}
0
On

Create your own source model-

class Namespace_Modulename_Model_System_Config_Source_Cmsblock
{
    protected $_options;

    public function toOptionArray()
    {
        if (!$this->_options) {
            $this->_options = Mage::getResourceModel('cms/block_collection')
                ->load()
                ->toOptionArray();
        }
        return $this->_options;
    }
}

Include it in your system xml:

<block translate="label">
  <label>Cms block</label>
  <frontend_type>select</frontend_type>
  <source_model>modulename/system_config_source_cmsblock</source_model>
  <sort_order>30</sort_order>
  <show_in_default>1</show_in_default>
  <show_in_website>1</show_in_website>
  <show_in_store>1</show_in_store>
</block>
0
On

You can use the same model used for the category static block field: Catalog_Model_Category_Attribute_Source_Page aka catalog/category_attribute_source_page

<block translate="label">
  <label>Cms block</label>
  <frontend_type>select</frontend_type>
  <source_model>catalog/category_attribute_source_page</source_model>
  <sort_order>30</sort_order>
  <show_in_default>1</show_in_default>
  <show_in_website>1</show_in_website>
  <show_in_store>1</show_in_store>
</block>