5

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?

Paul Roub
  • 36,322
  • 27
  • 84
  • 93
Simon
  • 594
  • 2
  • 6
  • 25

4 Answers4

8

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>   
Alex Shchur
  • 741
  • 4
  • 13
  • 1
    This is a great solution because it taps into the core functionality of the super method `Varien_Data_Collection::toOptionArray` and illustrates the flexibility of all Magento collections in the context of an admin form. – Rick Buczynski Mar 16 '15 at 01:28
4

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;
    }
}
blmage
  • 4,214
  • 1
  • 23
  • 25
2

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>
Afroz Alam
  • 874
  • 6
  • 19
2

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>
Marius
  • 15,148
  • 9
  • 56
  • 76