如何删除Magento Admin表单元素?

时间:2012-12-21 18:50:44

标签: magento

我想在Magento管理客户编辑表单中删除密码表单元素。

我正在重写Mage_Adminhtml_Block_Customer_Edit_Tab_Account如下。但即使从表单中删除元素,它仍然在客户信息选项卡中显示密码字段。

<?php

require 'Mage/Adminhtml/Block/Customer/Edit/Tab/Account.php';

class Mycompany_Mymodule_Block_Adminhtml_Customer_Edit_Tab_Account
    extends Mage_Adminhtml_Block_Customer_Edit_Tab_Account
{

    public function initForm()
    {
        $customer = parent::initForm();

        $customer->getForm()->removeField('password_fieldset');
        $customer->getForm()->removeField('new_password');

        return $customer;
    }
}

非常感谢任何帮助。

编辑:令人烦恼的是,如果我更改任何表单字段值,例如更改应用于字段的标签。 例如更改密码表单的标签,实际更改标签。 :○

public function initForm()
{
    $customer = parent::initForm();

    $customer->getForm()->getElement('new_password')->setLabel('Test Label');

    return $customer;
}

4 个答案:

答案 0 :(得分:5)

您需要对父元素执行removeField(),如下所示:

foreach($this->getForm()->getElements() as $fieldset){
        $fieldset->removeField('id_of_desired_element');  
    }

答案 1 :(得分:2)

这就是我最终删除magento中的字段集的方式:

// Remove the elementId from the form
$this->getForm()->removeField('password_fieldset');
// Remove the fieldset
$this->getForm()->getElements()->remove('password_fieldset');

答案 2 :(得分:0)

Mage_Adminhtml_Block_Customer_Edit_Tab_Account

中删除以下代码

initForm()方法下

if ($customer->getId()) {
            if (!$customer->isReadonly()) {
                // Add password management fieldset
                $newFieldset = $form->addFieldset(
                    'password_fieldset',
                    array('legend' => Mage::helper('customer')->__('Password Management'))
                );
                // New customer password
                $field = $newFieldset->addField('new_password', 'text',
                    array(
                        'label' => Mage::helper('customer')->__('New Password'),
                        'name'  => 'new_password',
                        'class' => 'validate-new-password'
                    )
                );
                $field->setRenderer($this->getLayout()->createBlock('adminhtml/customer_edit_renderer_newpass'));

                // Prepare customer confirmation control (only for existing customers)
                $confirmationKey = $customer->getConfirmation();
                if ($confirmationKey || $customer->isConfirmationRequired()) {
                    $confirmationAttribute = $customer->getAttribute('confirmation');
                    if (!$confirmationKey) {
                        $confirmationKey = $customer->getRandomConfirmationKey();
                    }
                    $element = $fieldset->addField('confirmation', 'select', array(
                        'name'  => 'confirmation',
                        'label' => Mage::helper('customer')->__($confirmationAttribute->getFrontendLabel()),
                    ))->setEntityAttribute($confirmationAttribute)
                        ->setValues(array('' => 'Confirmed', $confirmationKey => 'Not confirmed'));

                    // Prepare send welcome email checkbox if customer is not confirmed
                    // no need to add it, if website ID is empty
                    if ($customer->getConfirmation() && $customer->getWebsiteId()) {
                        $fieldset->addField('sendemail', 'checkbox', array(
                            'name'  => 'sendemail',
                            'label' => Mage::helper('customer')->__('Send Welcome Email after Confirmation')
                        ));
                        $customer->setData('sendemail', '1');
                    }
                }
            }
        } else {
            $newFieldset = $form->addFieldset(
                'password_fieldset',
                array('legend'=>Mage::helper('customer')->__('Password Management'))
            );
            $field = $newFieldset->addField('password', 'text',
                array(
                    'label' => Mage::helper('customer')->__('Password'),
                    'class' => 'input-text required-entry validate-password',
                    'name'  => 'password',
                    'required' => true
                )
            );
            $field->setRenderer($this->getLayout()->createBlock('adminhtml/customer_edit_renderer_newpass'));

            // Prepare send welcome email checkbox
            $fieldset->addField('sendemail', 'checkbox', array(
                'label' => Mage::helper('customer')->__('Send Welcome Email'),
                'name'  => 'sendemail',
                'id'    => 'sendemail',
            ));
            $customer->setData('sendemail', '1');
            if (!Mage::app()->isSingleStoreMode()) {
                $fieldset->addField('sendemail_store_id', 'select', array(
                    'label' => $this->helper('customer')->__('Send From'),
                    'name' => 'sendemail_store_id',
                    'values' => Mage::getSingleton('adminhtml/system_store')->getStoreValuesForForm()
                ));
            }
        }

答案 3 :(得分:0)

更新Manne Busk的回答:

不要循环遍历所有元素,只需删除所需的元素即可:

$this->getForm()->getElement('content_fieldset')->removeField('content_heading');