【问题标题】:What's the source_model for a cms block chooser in magento?magento 中 cms 块选择器的 source_model 是什么?
【发布时间】:2015-08-31 22:20:01
【问题描述】:

我想在我的 magento 实例中添加一个 config 字段。您应该可以在其中存储一个 cms 块。

<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>

但我只找到了 cms pages (adminhtml/system_config_source_cms_page) 的模型。

cms blocks对应的source_model是什么?

【问题讨论】:

标签: magento model magento-1.12


【解决方案1】:

我认为 Mage_Cms_Model_Resource_Block_Collection 类对此非常有用:

                    <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>   

【讨论】:

  • 这是一个很好的解决方案,因为它利用了超级方法 Varien_Data_Collection::toOptionArray 的核心功能,并说明了所有 Magento 集合在管理表单上下文中的灵活性。
【解决方案2】:

没有,但你可以自己做:

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;
    }
}

【讨论】:

    【解决方案3】:

    创建您自己的源模型-

    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;
        }
    }
    

    将其包含在您的系统 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>
    

    【讨论】:

      【解决方案4】:

      您可以使用与类别静态块字段相同的模型: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>
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2016-12-05
        • 2022-07-21
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2010-11-25
        • 1970-01-01
        相关资源
        最近更新 更多