Magento Widget.xml,禁用或隐藏参数

时间:2013-03-05 12:36:24

标签: php magento widget

是否可以隐藏/禁用Widget中的参数? 在CMS页面上放置窗口小部件时,您会看到窗口小部件参数,您可以选择各种选项。 在我的一个小部件中,如果特定模块不活动,我想隐藏一个参数。 (我知道如何检查模块是否处于活动状态) 我还想从模板下拉列表中删除选项值。删除使用缺少的模块的模板。 有Magento方式吗?使用Magento Enterprise 1.12.0.2

1 个答案:

答案 0 :(得分:0)

您可以通过位于模块的etc文件夹中的模块widget.xml来执行此操作。例如在我的情况下,我想显示选择器  选择/关联事件(我的自定义模块中的实体)与小部件,所以我这样做了如下

在我的小部件块下(在widget.xml中)我添加了如下所示的参数:

 <eventsmanager_event_view type="tpl_eventsmanager/event_widget_view" translate="name description" module="tpl_eventsmanager">
        <name>Event Calendar</name>
        <description>Event Calendar widget</description>
        <is_email_compatible>0</is_email_compatible>
      <parameters>
            <event_id type="complex" translate="label">
                <visible>1</visible>
                <required>1</required>
                <label>Event</label>
                <type>label</type>
                <helper_block>
                    <type>tpl_eventsmanager/adminhtml_event_widget_chooser</type>
                    <data>
                        <button translate="open">
                            <open>Select Event</open>
                        </button>
                    </data>
                </helper_block>
                <sort_order>120</sort_order>
            </event_id>
        </parameters>
    </eventsmanager_event_view> 

然后我添加了包含此代码的文件chooser.php:

<?php
class Tpl_EventsManager_Block_Adminhtml_Event_Widget_Chooser extends Mage_Adminhtml_Block_Widget_Grid
{
    /**
     * Block construction, prepare grid params
     *
     * @access public
     * @param array $arguments Object data
     * @return void
     * @author TPL
     */
    public function __construct($arguments=array())
    {
        parent::__construct($arguments);
        $this->setDefaultSort('entity_id');
        $this->setDefaultDir('ASC');
        $this->setUseAjax(true);
        $this->setDefaultFilter(array('chooser_status' => '1'));
    }

    /**
     * Prepare chooser element HTML
     *
     * @access public
     * @param Varien_Data_Form_Element_Abstract $element Form Element
     * @return Varien_Data_Form_Element_Abstract
     * @author TPL
     */
    public function prepareElementHtml(Varien_Data_Form_Element_Abstract $element)
    {
        $uniqId = Mage::helper('core')->uniqHash($element->getId());
        $sourceUrl = $this->getUrl(
            '*/eventsmanager_event_widget/chooser',
            array('uniq_id' => $uniqId)
        );
        $chooser = $this->getLayout()->createBlock('widget/adminhtml_widget_chooser')
            ->setElement($element)
            ->setTranslationHelper($this->getTranslationHelper())
            ->setConfig($this->getConfig())
            ->setFieldsetId($this->getFieldsetId())
            ->setSourceUrl($sourceUrl)
            ->setUniqId($uniqId);
        if ($element->getValue()) {
            $event = Mage::getModel('tpl_eventsmanager/event')->load($element->getValue());
            if ($event->getId()) {
                $chooser->setLabel($event->getEventName());
            }
        }
        $element->setData('after_element_html', $chooser->toHtml());
        return $element;
    }

    /**
     * Grid Row JS Callback
     *
     * @access public
     * @return string
     * @author TPL
     */
    public function getRowClickCallback()
    {
        $chooserJsObject = $this->getId();
        $js = '
            function (grid, event) {
                var trElement = Event.findElement(event, "tr");
                var eventId = trElement.down("td").innerHTML.replace(/^\s+|\s+$/g,"");
                var eventTitle = trElement.down("td").next().innerHTML;
                '.$chooserJsObject.'.setElementValue(eventId);
                '.$chooserJsObject.'.setElementLabel(eventTitle);
                '.$chooserJsObject.'.close();
            }
        ';
        return $js;
    }

    /**
     * Prepare a static blocks collection
     *
     * @access protected
     * @return Tpl_EventsManager_Block_Adminhtml_Event_Widget_Chooser
     * @author TPL
     */
    protected function _prepareCollection()
    {
        $collection = Mage::getModel('tpl_eventsmanager/event')->getCollection();
        $collection->addAttributeToSelect('event_name');
        $collection->addAttributeToSelect('status');
        $this->setCollection($collection);
        return parent::_prepareCollection();
    }

    /**
     * Prepare columns for the a grid
     *
     * @access protected
     * @return Tpl_EventsManager_Block_Adminhtml_Event_Widget_Chooser
     * @author TPL
     */
    protected function _prepareColumns()
    {
        $this->addColumn(
            'chooser_id',
            array(
                'header' => Mage::helper('tpl_eventsmanager')->__('Id'),
                'align'  => 'right',
                'index'  => 'entity_id',
                'type'   => 'number',
                'width'  => 50
            )
        );

        $this->addColumn(
            'chooser_event_name',
            array(
                'header' => Mage::helper('tpl_eventsmanager')->__('Event Name'),
                'align'  => 'left',
                'index'  => 'event_name',
            )
        );
        $this->addColumn(
            'chooser_status',
            array(
                'header'  => Mage::helper('tpl_eventsmanager')->__('Status'),
                'index'   => 'status',
                'type'    => 'options',
                'options' => array(
                    0 => Mage::helper('tpl_eventsmanager')->__('Disabled'),
                    1 => Mage::helper('tpl_eventsmanager')->__('Enabled')
                ),
            )
        );
        return parent::_prepareColumns();
    }

    /**
     * get url for grid
     *
     * @access public
     * @return string
     * @author TPL
     */
    public function getGridUrl()
    {
        return $this->getUrl(
            'adminhtml/eventsmanager_event_widget/chooser',
            array('_current' => true)
        );
    }
}