Widgets

Seotoaster’s plugins can bring their own widgets into the system. It makes shortcodes look nicer and allows to move a part of routine stuff from the main plugin’s class to more suitable location. Let’s try to create a widget for our todo plugin. 

In the plugin we have 2 option makers for the form and the list options. Let’s do more elegant way and create a widget called Todo (yes, the same name with the plugin) and teach it to handle our options.
All plugin’s widgets should stay in the plugin_directory/system/app/Widgets folder. Each widget should have its own directory. So, we will create a directory called Todo and create a class called Todo.php in it. Check it out

All widgets should extend Widgets_Abstract class and implements protected _load method. Initially your widget class should look similar to the example

class Widgets_Todo_Todo extends Widgets_Abstract {
    protected function _load() {
        
    }
}

_load method should return content that will be place instead of the widget shortcode. Widgets are able to work with views. Let’s add a directory called views in our widget directory and override protected _init() method of the widget to initialize the view property

class Widgets_Todo_Todo extends Widgets_Abstract {

    protected function _init() {
        $this->_view = new Zend_View(array('scriptPath' => __DIR__ . '/views'));
    }

The next step is to change the short codes that you put in the content to the new ones. Go and change {%%plugin:todo:form} to {%%todo:form} and {%%plugin:todo:list} to {%%todo:list} Those new shortcodes will tell the Seotoaster to work with your plugin’s widget. Lets move our form.todo.phtml and list.todo.phtml to the widget’s view directory. Similar to the plugin, everything that goes after the : in the widget colled “options”. All options, passed to the widget, could be found in the $this->_options array. Also a lot of usefull data could be found in the $_toasterOptions array. (see the Widgets_Abstract class for more information such as automated widget caching, etc...) Okay, we are almost there, lets modify our widget’s code to handle the options. Checkout an example below

protected function _load() {
        if(empty($this->_options)) {
            throw new Exceptions_SeotoasterWidgetException('Not enough parameters');
        }
        $rendererName = '_render' . ucfirst(array_shift($this->_options));
        if(method_exists($this, $rendererName)) {
            return $this->$rendererName();
        }
        return '';
    }

    protected function _renderForm() {

        //few assignments to make our plugin more user friendly and show some feedback
        $this->_view->error              = $this->_sessionHelper->error;
        $this->_view->messages           = $this->_flashMessenger->getMessages();
        $this->_sessionHelper->pluginUrl = $this->_toasterOptions['url'];

        $this->_view->todoForm = new Todo_Forms_Todo();
        return $this->_view->render('form.todo.phtml');
    }

    protected function _renderList() {
        $this->_view->todos = $this->_mapper->fetchAll();
        return $this->_view->render('list.todo.phtml');
    }

This is the one way to organize options handling. At first we generate a “renderer” name - name of the protected method of our widget that will be responsible for the option, and, if this method exists we try to execute it. That easy! Finally go to the Todo plugin’s main class and remove _makeOptionForm and _makeOptionList methods. We do not need them anymore. That’s it! We moved neccessary code to the widget! Let’s move on and see what else can we do

DevNews