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:


	<?xml version="1.0"?>
	<module name="[YourCompany]_[YourModule]" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../../../app/etc/module.xsd">
		<dependencies>
			<module name="Mage_Admin" />
		</dependencies>
	</module>
	

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:


	<?xml version="1.0"?>
	<config>
		<modules>
			<[YourCompany]_[YourModule]>
				<blocks>
					<widget>
						<class>[YourCompany]_[YourModule]_Block_Adminhtml_Widget</class>
					</widget>
				</blocks>
				<models>
					<widget>
						<class>[YourCompany]_[YourModule]_Model_Widget</class>
					</widget>
				</models>
			</[YourCompany]_[YourModule]>
		</modules>
	</config>
	

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.