{ Hi! I'm Mike }
I'm a core developer with The Horde Project and a founding parter of Horde LLC - the company behind the world's most flexible groupware platform. This is my personal blog full of random thoughts about development and life in general.
May 13, 2011

Building a custom Horde_Block

Anyone who has used Horde at all should know what a Horde_Block is. These are the individual bits of content that are displayed on the "portal" page in Horde. Things like the Mail Summary, Calendar Summary etc...

If you have a custom application, or even just need some standalone content presented as a Horde_Block, it's fairly easy to put one together. For this example, let's assume that you have some custom content you want to display as a block, but that it's not tied to any Horde application (similar to the WeatherDotCom block). First order of business is to create a new php file for your block. The easiest way to do that is to copy the Example.php file from skeleton/lib/Block and edit it appropriately. The content of that file when you are done should be similar to:

<?php
/**
 * @package Horde
 */
class Horde_Block_Foo extends Horde_Core_Block
{
    /**
     */
    public function __construct($app, $params = array())
    {
        parent::__construct($app, $params);

        $this->_name = _("Foo Summary");
    }

    /**
     */
    protected function _params()
    {
        return array(
            'color' => array(
                'type' => 'text',
                'name' => _("Color"),
                'default' => '#ff0000'
            )
        );
    }

    /**
     */
    protected function _title()
    {
        return _("Some special Foo content");
    }

    /**
     */
    protected function _content()
    {
        $html  = '<table width="100" height="100" bgcolor="%s">';
        $html .= '<tr><td>&nbsp;</td></tr>';
        $html .= '</table>';

        return sprintf($html, $this->_params['color']);
    }

}

Note the name of the Class is Horde_Block_Foo this file should be saved as horde/lib/Block/Foo.php. If the block were to be called "Bar" instead, the class name would be Horde_Block_Bar and would have been saved as horde/lib/Block/Bar.php - you get the idea.

The main method you are interested in is the _content() method. This is where the block content is generated. The HTML for the block should be built as a string and returned from this method. If you want to be able to configure anything about the block, you should add to the _params() method. In this example, a text value named "color", with a default value or "#ff0000' is available. As shown in the example, to obtain the value of a setting, you use $this->_params['setting_name'].  There are other types of values available as well. For example, if you wanted to provide a select list of choices instead you could do something like:

 array(
           'units' => array(
                'type' => 'enum',
                'name' => _("Units"),
                'default' => 'standard',
                'values' => array(
                    'standard' => _("Standard"),
                    'metric' => _("Metric")
                )
            ),
)

 

This provides a select list named "Units" and allows either "Standard" or "Metric" as choices.

After adding this file and adding the content you want it to show, it will be available as a block to add to your users' portals the next time they login.