We just need extends 'Mage_Adminhtml_Block_Catalog_Category_Tab_Attributes'
and Rewrite function _prepareForm();
add These code into function :
if ($this->getAddHiddenFields()) {
$fieldset->addField('position', 'text', array(
'label'=>'Position',
'name' => 'position',
'value' => $this->getCategory()->getPosition(),
));
}
and do not forget change last code to :
return Mage_Adminhtml_Block_Catalog_Form::_prepareForm();
not return Parent::_prepareForm();
Now I will told you how to rewrite this block .
1. Define the block in our config xml file
<?xml version="1.0"?>
<config>
<modules>
<Bysoft_Myadminhtml>
<version>0.1.0</version>
</Bysoft_Myadminhtml>
</modules>
<global>
<helpers>
<myadminhtml>
<class>Bysoft_Myadminhtml_Helper</class>
</myadminhtml>
</helpers>
<blocks>
<adminhtml>
<rewrite>
<catalog_category_tab_attributes>Bysoft_Myadminhtml_Block_Catalog_Category_Tab_Attributes</catalog_category_tab_attributes>
</rewrite>
</adminhtml>
</blocks>
</global>
</config>
2. create local block file . extends core block file:
<?php
class Bysoft_Myadminhtml_Block_Catalog_Category_Tab_Attributes extends Mage_Adminhtml_Block_Catalog_Category_Tab_Attributes
{
/**
* Prepare form before rendering HTML
*
* @return Mage_Adminhtml_Block_Catalog_Category_Tab_Attributes
*/
protected function _prepareForm() {
$group = $this->getGroup();
$attributes = $this->getAttributes();
$form = new Varien_Data_Form();
$form->setHtmlIdPrefix('group_' . $group->getId());
$form->setDataObject($this->getCategory());
$fieldset = $form->addFieldset('fieldset_group_' . $group->getId(), array(
'legend' => Mage::helper('catalog')->__($group->getAttributeGroupName()),
'class' => 'fieldset-wide',
));
if ($this->getAddHiddenFields()) {
if (!$this->getCategory()->getId()) {
// path
if ($this->getRequest()->getParam('parent')) {
$fieldset->addField('path', 'hidden', array(
'name' => 'path',
'value' => $this->getRequest()->getParam('parent')
));
}
else {
$fieldset->addField('path', 'hidden', array(
'name' => 'path',
'value' => 1
));
}
}
else {
$fieldset->addField('id', 'hidden', array(
'name' => 'id',
'value' => $this->getCategory()->getId()
));
$fieldset->addField('path', 'hidden', array(
'name' => 'path',
'value' => $this->getCategory()->getPath()
));
}
}
$this->_setFieldset($attributes, $fieldset);
foreach ($attributes as $attribute) {
$rootId = Mage_Catalog_Model_Category::TREE_ROOT_ID;
/* @var $attribute Mage_Eav_Model_Entity_Attribute */
if ($attribute->getAttributeCode() == 'url_key') {
if (
(!$this->getCategory()->getId() && $this->getRequest()->getParam('parent', $rootId) == $rootId)
|| ($this->getCategory()->getParentId() == $rootId)
) {
$fieldset->removeField('url_key');
} else {
$form->getElement('url_key')->setRenderer(
$this->getLayout()->createBlock('adminhtml/catalog_form_renderer_attribute_urlkey')
);
}
}
}
if ($this->getAddHiddenFields()) {
$fieldset->addField('position', 'text', array(
'label'=>'Position',
'name' => 'position',
'value' => $this->getCategory()->getPosition(),
));
}
if ($this->getCategory()->getLevel() == 1) {
$fieldset->removeField('custom_use_parent_settings');
} else {
if ($this->getCategory()->getCustomUseParentSettings()) {
foreach ($this->getCategory()->getDesignAttributes() as $attribute) {
if ($element = $form->getElement($attribute->getAttributeCode())) {
$element->setDisabled(true);
}
}
}
if ($element = $form->getElement('custom_use_parent_settings')) {
$element->setData('onchange', 'onCustomUseParentChanged(this)');
}
}
if ($this->getCategory()->hasLockedAttributes()) {
foreach ($this->getCategory()->getLockedAttributes() as $attribute) {
if ($element = $form->getElement($attribute)) {
$element->setReadonly(true, true);
}
}
}
if (!$this->getCategory()->getId()){
$this->getCategory()->setIncludeInMenu(1);
}
$form->addValues($this->getCategory()->getData());
Mage::dispatchEvent('adminhtml_catalog_category_edit_prepare_form', array('form'=>$form));
$form->setFieldNameSuffix('general');
$this->setForm($form);
return Mage_Adminhtml_Block_Catalog_Form::_prepareForm();
}
}

本文介绍如何在Magento中扩展并重写'Mage_Adminhtml_Block_Catalog_Category_Tab_Attributes'块,以添加额外的字段如位置,并确保正确显示。通过配置XML文件定义模块和块,以及修改核心块文件实现。

被折叠的 条评论
为什么被折叠?



