CommercePeer Brand Logo


Programmatically: Magento 2 Product Entity

Programmatically: How to use Magento's custom widget form system for custom widget configuration

Step 1: Create a new module

Create a new module with the following structure:


	/app/code/local/[YourCompany]/[YourModule]/etc/module.xml
	

Add the following code to the module.xml file:


		
	
    
        
    
	
	

Step 2: Create the widget form

Create a new file app/code/local/[YourCompany]/[YourModule]/Block/Adminhtml/Widget/Form.php with the following code:


	class [YourCompany]_[YourModule]_Block_Adminhtml_Widget_Form extends Mage_Adminhtml_Block_Widget_Form
  {
    public function __construct()
    {
        parent::__construct();
        $this->setTemplate('widget/form.phtml');
    }
  }
	

Step 3: Create the form template

Create a new file app/code/local/[YourCompany]/[YourModule]/frontend/template/widget/form.phtml with the following code:


	/** @var $form Mage_Adminhtml_Block_Widget_Form */
	
getElements() as $element): getHtml()

Step 4: Create the widget model

Create a new file app/code/local/[YourCompany]/[YourModule]/Model/Widget.php with the following code:


	class [YourCompany]_[YourModule]_Model_Widget extends Mage_Core_Model_Abstract
  {
    public function _construct()
    {
        parent::_construct();
        $this->_init('widget/widget');
    }
  }
	

Step 5: Register the widget form

Add the following code to app/code/local/[YourCompany]/[YourModule]/etc/config.xml:


	
	
    
        <[YourCompany]_[YourModule]>
            
                
                    [YourCompany]_[YourModule]_Block_Adminhtml_Widget
                
            
            
                
                    [YourCompany]_[YourModule]_Model_Widget
                
            
        
    
	
	

Step 6: Use the widget form

In your Magento admin panel, navigate to System > Configuration > Advanced > Developer > Widget. You should see our custom widget form with a submit button. To use the widget form programmatically, you can use the following code:


		$widgetForm = Mage::getBlockSingleton('widget/form');
		$widgetForm->setWidget($widget);
		$widgetForm->render();
	

LET’S WORK TOGETHER

We love working with Small Businesses, Retailers, Manufacturers. Our team is here to Help.

→ Schedule Free Consultant Now
Copyright © 2024 CommercePeer. All Rights Reserved.