Skip to content

Instantly share code, notes, and snippets.

@akizor
Last active August 29, 2015 14:06
Show Gist options
  • Save akizor/fa22c0e9ff1a2cb192e7 to your computer and use it in GitHub Desktop.
Save akizor/fa22c0e9ff1a2cb192e7 to your computer and use it in GitHub Desktop.
Magento - New admin custom form entity to display custom content

Installation

  • Create file 'Content.php' under app/code/local/Varien/Data/Form/Element/

Usage

  • In Yourform.php block add another form field using $fieldset->addField("name-of-field","content", array());

How to use

This form element accepts several parameters:

  • label - this will define the field name
  • block - you can display grids/containers/tabs/anything you want
  • template - it even supports templates in case you want to overwrite the block's toHtml output
  • params - in case you want to parse parameters from your form to your block/template files. Use $this->getParams() to get'em
  • after_element_html - this will output html content after your block was rendered

Keep in mind

  • Carefull what you're displaying because form inputs will be submitted on save. Make sure your inputs don't have the same name as the current form inputs.
  • When using grids, make sure they are ajax ready. Use renderers to display form inputs. If you are using filters, make sure filter input names are not the same with the rest of the form.
  • When displaying custom content that requires some JS/CSS to run (eg. Dialogs/Editors) include them in your form xml.
<?php
class Varien_Data_Form_Element_Content extends Varien_Data_Form_Element_Abstract {
private $_defaults = array(
'block' => 'core/template',
'params' => '',
'template' => '',
);
public function __construct($attributes=array())
{
parent::__construct($attributes);
$this->setType('content');
$this->setExtType('content');
if($block_name = $this->getBlock()){
$this->_defaults['block'] = $block_name;
}
if($params = $this->getParams()){
$this->_defaults['params'] = $params;
}
}
public function getElementHtml()
{
$html = '<div class="content-header" style="margin:15px 0 0 0; padding:0;"><h3>' . $this->getLabelHtml() . '</h3></div><br />';
$data = $this->getParams();
if($this->hasTemplate()){
$html .= Mage::app()->getLayout()->createBlock($this->_defaults['block'])->setParams($this->_defaults['params'])->setTemplate($this->getTemplate())->toHtml();
} else {
$html .= Mage::app()->getLayout()->createBlock($this->_defaults['block'])->setParams($this->_defaults['params'])->toHtml();
}
$html .= $this->getAfterElementHtml();
return $html;
}
public function toHtml(){
return '<tr><td colspan="2">' . $this->getElementHtml() . '</td></tr>';
}
protected function hasTemplate(){
if($this->getTemplate()){
return true;
}
return false;
}
}
<?php
//use this line to add a new form field
$fieldset->addField('my_custom_field', 'content', array(
'label' => Mage::helper('adminhtml')->__('Cool Label'),
'block' => 'mymodule/adminhtml_mycustom_grid_with_ajax',
//'params' => '', //use arrays or objects for params
//'after_element_html' => 'after_element_html',
//'template' => 'myfolder/myfile.phtml'
));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment