How can we change the admin form element position in magento? -


enter image description here

how can change position (sort order) of admin form element?

tl;dr:

you can specify caret ^ optional fourth argument when defining field:

    $fieldset->addfield('my_element','text',array(       'name'  => 'my_element',       'label' => mage::helper('customer')->__('my element')     ),'^'); 

more information

see varien_data_form_abstract::addfield:

/**  * add child element  *  * if $after parameter false - element adds end of collection  * if $after parameter null - element adds befin of collection  * if $after parameter string - element adds after of element id  *  * @param   string $elementid  * @param   string $type  * @param   array  $config  * @param   mixed  $after  * @return varien_data_form_element_abstract  */ public function addfield($elementid, $type, $config, $after=false) {     if (isset($this->_types[$type])) {         $classname = $this->_types[$type];     }     else {         $classname = 'varien_data_form_element_'.ucfirst(strtolower($type));     }     $element = new $classname($config);     $element->setid($elementid);     if ($element->getrequired()) {         $element->addclass('required-entry');     }     $this->addelement($element, $after);     return $element; } 

the comments tell how order elements. so, position field, specify fourth argument $after of somefield or whatever id of field wish place yours after.

this useful when extending customer account form within own class. here's example places new form element above first in form:

class yourcompany_adminhtml_block_customer_edit_tab_account extends mage_adminhtml_block_customer_edit_tab_account {    ...    public function initform() {     parent::initform();      $form=$this->getform();      $fieldset=$form->getelement('base_fieldset');      $fieldset->addfield('my_element','text',array(       'name'  => 'my_element',       'label' => mage::helper('customer')->__('my element')     ),'^');      return $this;   }    ...  } 

you may notice strange. fourth argument addfield call caret ('^'). places element @ top of form, appear asking in question.

but wait! why doesn't match code documentation shown above?! well, looks disconnect in consistent coding convention during development. if follow code, find addfield makes way varien_data_form_element_collection::add method. in here see 2nd argument, $after, notes in comments:

/**  * add element collection  *  * @todo straight $after  * @param varien_data_form_element_abstract $element  * @param boolean|'^'|string $after  * @return varien_data_form_element_abstract  */ public function add(varien_data_form_element_abstract $element, $after=false) 

here find false appends element fieldset, ^ character prepends element, , passing in element id place element after 1 specified.


update: how deal existing fields

what if want re-order existing field? unfortunately, ordering takes places @ time of adding field, , cannot conveniently changed after that. can offer 2 approaches job done.

method 1: re-order removing , adding again

$fieldset->removefield('core_element'); $fieldset->addfield('core_element','text',array(   'name'  => 'core_element',   'label' => mage::helper('customer')->__('core element') ),'^'); 

this simple solution, requires manually re-define field. in cases easy enough. in others, not so. scenarios, see next.

method 2: re-order cloning

$element=clone $form->getelement('core_element'); $fieldset->removefield('core_element'); $fieldset->addelement($element,'^'); 

i think best solution, because preserves element definition written. important in case of attributes fields generated in part frontend_input_renderer classes. learn more here.


Comments

Popular posts from this blog

c# - Operator '==' incompatible with operand types 'Guid' and 'Guid' using DynamicExpression.ParseLambda<T, bool> -