2

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

4

1 回答 1

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> 

然后我添加了包含此代码的文件选择器.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)
        );
    }
}
于 2016-04-11T13:54:58.933 回答