Question

Can I create a widget instance and assign it to a specific page to appear through install script? I'd like to have some functionality similar to sample contents, but with widgets. To be more specific, my question is aimed at creating instances of widgets and placing them on certain pages programmatically through install script.

Was it helpful?

Solution

Its not pretty, but of course you can create a widget instance programmatically, for example in a setup script:

$widgetParameters = array(
    'param1' => 'This is some value from the widget form,
    'param2' => 'Some/other/value',
    'param2_2' => 'Rly?',
    'template' => 'this/is/the/template.phtml'
);

$instance = Mage::getModel('widget/widget_instance')->setData(array(
    'type' => 'your_module/your_widget',
    'package_theme' => 'default/theme', // has to match the concrete theme containing the template
    'title' => 'This is the Widget title',
    'store_ids' => '0', // or comma separated list of ids
    'widget_parameters' => serialize($widgetParameters),
    'page_groups' => array(array(
        'page_group' => 'all_pages',
        'all_pages' => array(
            'page_id' => null,
            'group' => 'all_pages',
            'layout_handle' => 'default',
            'block' => 'left',
            'for' => 'all',
            'template' => $widgetParameters['template],
        )
    ))
))->save();

This example sets the widget instance to display on every page in the left column.
If you want to specify different pages or target blocks, you need to update the values accordingly. I suggest looking what is set in Mage_Widget_Adminhtml_Widget_InstanceController::saveAction() and adjust the code above accordingly.

OTHER TIPS

Licensed under: CC-BY-SA with attribution
Not affiliated with magento.stackexchange
scroll top