我正在创建一个模块,允许用户在创建角色权限期间选择网站(系统 - >权限 - >角色 - >添加新角色 - >角色资源)。我正在使用观察者来实现这一目标,但是我无法获得表单对象。
Observer.php
class Mymodule_Mycompany_Model_Observer
{
public function appendCustomRow(Varien_Event_Observer $observer)
{
$block = $observer->getEvent()->getBlock();
if (!isset($block)) {
return $this;
}
if ($block->getType() == 'adminhtml/permissions_editroles') {
//get form instance
$form = $observer->getEvent()->getForm();
//create new custom fieldset 'website'
$fieldset = $form->addFieldset('website', array(
'legend' => 'Website Extras',
'class' => 'fieldset-wide'
)
);
//add new field
$fieldset->addField('website', 'text', array(
'name' => 'website',
'label' => Mage::helper('adminhtml')->__('Website'),
'title' => Mage::helper('adminhtml')->__('Website'),
'disabled' => false,
));
}
}
}
Mymodule中/ MyCompany的在/ etc / config.xml中
<adminhtml>
<events>
<core_block_abstract_prepare_layout_before>
<observers>
<Event_column_append>
<type>model</type>
<class>Mymodule_Mycompany_Model_Observer</class>
<method>appendCustomColumn</method>
</Event_column_append>
</observers>
</core_block_abstract_prepare_layout_before>
</events>
</adminhtml>
答案 0 :(得分:7)
我解决了这个问题。这是修改后的代码片段
public function appendCustomColumn(Varien_Event_Observer $observer)
{
$block = $observer->getEvent()->getBlock();
if (!isset($block)) {
return $this;
}
if ($block->getType() == 'adminhtml/permissions_tab_roleinfo') {
//get form instance
$form = $block->getForm();
//create new custom fieldset 'website'
$fieldset = $form->addFieldset(
'website_field',
array(
'legend' => 'Website Extras',
'class' => 'fieldset-wide'
)
);
//add new field
$fieldset->addField('website', 'text', array(
'name' => 'website',
'label' => Mage::helper('adminhtml')->__('Website'),
'title' => Mage::helper('adminhtml')->__('Website'),
'disabled' => false,
));
}
}
XML配置:
<events>
<adminhtml_block_html_before>
<observers>
<Event_column_append>
<type>model</type>
<class>Mymodule_Mycompany_Model_Observer</class>
<method>appendCustomColumn</method>
</Event_column_append>
</observers>
</adminhtml_block_html_before>
</events>
希望有帮助:)...
干杯, Swapna