activeDev
activeDev

Reputation: 795

Magento : add custom button to an admin form

Building a custom adminhtml module. Im using a simple form. it looks like this :

<?php

class Namespace_Modulename_Block_Adminhtml_Modulename_Edit_Tab_Form extends Mage_Adminhtml_Block_Widget_Form
{
    protected function _prepareForm()
    {
        $form = new Varien_Data_Form();
        $this->setForm($form);
        $fieldset = $form->addFieldset('modulename_form',array('legend'=>Mage::helper('modulename')->__('Module Data')));


    $fieldset->addField('test', 'text', array(
            'label'     => Mage::helper('modulename')->__('Test'),
            'name'      => 'test',
    ));

    // I want to add a custom button here. Say an action called "Add Option".
    //Clicking this action adds input boxes or dropdowns to the form that are to
    //to be included in the post when submitting the form (obviously).

I have been looking for a solution on Stack overflow and have not been able to find something that could help. I have then tried searching for something similar present in the Mage Core.

In the admin panel, if i go to [Catalog->Attributes->Manage Attributes] and click on a standard attribute like "Manufacturer" for example, on the second tab, called "Manage labels/Options" i see the following screen :

add options

There is a button and action which allows me to add options (in the form of textboxes) to the form. Identifying this as something i am trying to replicate, i went into the core to try and figure out what to do. If i open the following files (Magento Enteprise 12.0.2) :

Mage_Adminhtml_Block_Catalog_Product_Attribute_Edit_Tab_Options

I see it is empty, but extends Mage_Eav_Block_Adminhtml_Attribute_Edit_Options_Abstract

and I have gone through this file, but little makes sense to me. Am I going down the wrong way? How can I achieve something similar, a button and action which adds fields to a admin form?

thanks

Upvotes: 1

Views: 12264

Answers (2)

Alexandr  Yuditsky
Alexandr Yuditsky

Reputation: 106

  1. You can set custom template for your form, and add there any tags (like button) via html.
  2. You can add renderer for you field
$customField = $fieldset->addField('test', 'text', array(
            'label'     => Mage::helper('modulename')->__('Test'),
            'name'      => 'test',
    ));

$customField->setRenderer($this->getLayout()->createBlock('yuormodule/adminhtml_yourform_edit_renderer_button'));

in Block Class

class Yournamespace_Yourmodule_Block_Adminhtml_Yourform_Edit_Renderer_Button extends Mage_Adminhtml_Block_Abstract implements Varien_Data_Form_Element_Renderer_Interface {
  public function render(Varien_Data_Form_Element_Abstract $element) {
    //You can write html for your button here
    $html = '<button></button>';
   return $html;
  }
}

Upvotes: 7

Sam
Sam

Reputation: 21

1- First you must create a container for your Grid like this

public function __construct() {        
    parent::__construct();
    $this->setTemplate('markavip/dataflow/edit/form/mapping.phtml');
}

2- in the Same Container you will add your buttons like

public function _prepareLayout() {
    $this->setChild('add_button', $this->getLayout()->createBlock('adminhtml/widget_button')
                    ->setData(array(
                        'label' => Mage::helper('markavip_dataflow')->__('Add Option'),
                        'class' => 'add',
                        'id' => 'add_new_option_button'
    )));
    $this->setChild('delete_button', $this->getLayout()->createBlock('adminhtml/widget_button')
                    ->setData(array(
                        'label' => Mage::helper('markavip_dataflow')->__('Delete'),
                        'class' => 'delete delete-option'
    )));
    return parent::_prepareLayout();
}

public function getAddNewButtonHtml() {
    return $this->getChildHtml('add_button');
}

public function getDeleteButtonHtml() {
    return $this->getChildHtml('delete_button');
}

3- and in the PHTML you will add thes buttons like this :

 <?php echo $this->getAddNewButtonHtml() ?>

and after this you will play in JS functions for add and hide

Upvotes: 2

Related Questions