2

我想删除 Magento Admin 客户编辑表单中的密码表单元素。

我正在重写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;
    }
}

非常感谢任何帮助。

编辑:令人讨厌的是,如果我更改任何表单字段值,例如更改应用于该字段的标签。例如更改密码表单的标签,实际上更改了标签。:o

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

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

    return $customer;
}
4

4 回答 4

5

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

foreach($this->getForm()->getElements() as $fieldset){
        $fieldset->removeField('id_of_desired_element');  
    }
于 2013-04-18T09:37:10.690 回答
3

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

// Remove the elementId from the form
$this->getForm()->removeField('password_fieldset');
// Remove the fieldset
$this->getForm()->getElements()->remove('password_fieldset');
于 2014-04-30T07:50:14.470 回答
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()
                ));
            }
        }
于 2012-12-21T20:01:29.943 回答
0

更新 Manne Busk 的回答:

无需遍历所有元素,只需像这样删除您想要的元素:

$this->getForm()->getElement('content_fieldset')->removeField('content_heading');
于 2013-04-30T12:11:24.627 回答