Magento – Display reviews in custom widget

widget

I'm creating a widget to display ratings. (Already created -> activated -> added in a page)

Calling this widget by {{widget type="widgetreviewtab/list"}} on a cms page named sample

Now when access http://site.com/custom.html it's empty.

Below is my WidgetReviewTab/Block/List.php where i'm trying to get rating lists.

class Review_WidgetReviewTab_Block_List extends Mage_Core_Block_Abstract implements Mage_Widget_Block_Interface
{
    protected function _toHtml()
    {        
        $html = $this->getChildHtml('review_list');
        return $html;
    }

}

If i use $html = 'custom text'; i'm getting result as custom text so widget's function is fine.

Is it proper to call $this->getChildHtml('review_list'); in widget.

I think i'm missing something ? Did any one have any idea to make it work ?

Best Answer

Widgets are independent entities apart from the whole layout XML system. So calling the $this->getChildHtml() method, which depends on layout XML, will not work in this case.

When you use $this->getChildHtml('review_list') that actually means that somewhere in the layout XML a 'review_list' block is defined as a child of the current block. That is not the case here, so lets forget the getChildHtml for now.

When you create a widget in the content through a widget directive, thats the {{widget..}} thingy you already talked about in your question, it is being parsed by a template filter.

This means that all logic for building up the content should be present in the Review_WidgetReviewTab_Block_List. A proper widget also needs a widget.xml in your module. In this xml file you can configure default settings but also define for instance which template should be used to render your widget. I attached a example at the bottom of my answer.

Displaying ratings of any kind means you probably need to load a helper or model via your widget block to get the needed data from the database.

A good tutorial to start building widgets: https://www.magentocommerce.com/knowledge-base/entry/tutorial-creating-a-magento-widget-part-1

Widget example:

<?xml version="1.0"?>
<widgets>
   <example type="review_widgetreviewtab/list">
       <name>Widget name</name>
       <description>Widget descriptoin</description>
       <parameters>
           <some_setting>
            <label>Setting name</label>
            <required>1</required>
            <visible>1</visible>
            <value>1</value>
            <type>text</type>
        </some_setting>
        <template>
            <required>1</required>
            <type>select</type>
            <value>widgets/my_widget_template.phtml</value>
        </template>
    </parameters>
</example>

Related Topic