Magento Certified Developer GuideCategory: 12. WidgetWhat classes are typically involved in Magento widget architecture
Manish Prakash Staff asked 2 years ago

In a typical widget structure the files which you need are (besides a standard magento module structure)

1. widget.xml
2. widget block file
3. source model for widget parameter options

widget.xml

<?xml version="1.0"?>
<widgets>
  <widget_unique_name type="unique/widget" translate="name description" module="excellence">
    <name>Sample Widget</name>
    <description>Sample Widget For Showing how widget works</description>
    <is_email_compatible>1</is_email_compatible>
    <parameters>
      <param1>
        <label>Link Options</label>
        <visible>1</visible>
        <required>1</required>
        <type>multiselect</type>
        <source_model>unique/options</source_model>
      </param1>
    </parameters>
  </widget_unique_name>
</widgets>

Frontend Block File For Display

//the below class also extend "Mage_Core_Block_Template" and then have the standard template file system
//instead of return html like below
class Excellence_Unique_Block_Widget extends Mage_Core_Block_Abstract implements Mage_Widget_Block_Interface {
/**
  * Produce links list rendered as html
  *
  * @return string
  */
  protected function _toHtml() {
    $html = '';
    $param = $thisĀ­>getData('param1');
      
    return $param;
  }
}

Options file

<?php
// app/code/local/Envato/WidgetLinks/Model/Options.php
class Excellence_Unique_Model_Options {
/**
  * Provide available options as a value/label array
  *
  * @return array
  */
  public function toOptionArray() {
    return array(
      array('value' => 'print', 'label' => 'Hi'),
      array('value' => 'email', 'label' => 'Hello'),
    );
  }
}

We can also define a template file our widget. In our widget.xml file we need to write

<parameters>
<template>
<required>1</required>
<visible>1</visible>
<label>Template</label>
<type>select</type>
<value>unique/widgets.phtml</value>
</template>
</parameters>

and would widget block class should extend “Mage_Core_Block_Template”
this way we can directly use a template file