Trac_Logo_512x512There are a number of ways that an Elgg plugin developer can manipulate views via the powerful Elgg views system.

Most Elgg programmers are by now familiar with extending or replacing existing views, or providing new views for new objects etc.

But what if you just wanted to make a simple tweak to an existing view – for example to replace all instances of rude words in a feed article, or to turn passive links into active ones?

Well, fortunately Elgg provides a post processing hook for views which can do just that.

After every view has been generated, the framework will trigger a plugin hook called “‘display’, ‘view'”. This hook is passed a parameter ‘view’ which contains the name of the view being processed (eg. object/blog).

The contents of the view are passed in the $returnvalue variable which you can perform any processing on before returning it from the hook.

I have just uploaded a Trac tags plugin to the Elgg community site which provides a good example of this.

The Trac tags plugin is a tiny plugin which uses the views post processing hook to turn Trac links (e.g. #xxxxx for tickets and [xxxxx] for changesets) into active links into your repository, and here’s how – first we register the hook:

function tractags_init()
{
....
// Register our post processing hook
register_plugin_hook('display', 'view', 'tractags_rewrite');


// define views we want to rewrite codes on (means we don't have to process *everything*)
$CONFIG->tractags_views = array(
'object/thewire',
'object/blog'
);

....
}

Then in our handler looks something like this:

function tractags_rewrite($hook, $entity_type, $returnvalue, $params)
{
global $CONFIG;

$view = $params['view'];

if (($view) && (in_array($view, $CONFIG->tractags_views)))
{
// Search and replace ticket numbers
$returnvalue = preg_replace_callback('/(#)([0-9]+)/i',
create_function(
'$matches',
'
global $CONFIG;

return "<a href=\"{$CONFIG->trac_baseurl}ticket/{$matches[2]}\">{$matches[0]}</a>";
'
), $returnvalue);

// Search and replace changesets
$returnvalue = preg_replace_callback('/(\[)([0-9]+)(\])/i',
create_function(
'$matches',
'
global $CONFIG;

return "<a href=\"{$CONFIG->trac_baseurl}changeset/{$matches[2]}\">{$matches[0]}</a>";
'
), $returnvalue);

return $returnvalue;
}
}

I’m sure you will be able to come up with some much more interesting uses!

Image from the Trac project.

5 thoughts on “Post processing Elgg views – Trac tags example

  1. Hi Marcus,

    There is a little problem when you are trying of overwrite a view with this method. The parameteres passed to the original view are not passed to the delegated ones.

    This is because the current code on elgglib is:
    + $content = trigger_plugin_hook(‘display’,’view’,array(‘view’ => $view_orig),$content);

    and not

    $content = trigger_plugin_hook(‘display’,’view’,array(‘view’ => $view,’vars’=>$vars),$content);

    Cheers,

  2. Just FYI to any readers, as of 1.8 you should now use the hook [view, the/view/name] rather than the deprecated [display, view] hook. This makes it possible to register for exactly the views you want to process.

  3. Microformats are an easy way to semantically mark up a web page so that it can be easily parsed and understood by a computer.
    This allows you to write code to easily do many funky things, not least of which extract author information from a page with minimal effort. This is how Idno’s distributed friending functionality works, btw.
    Idno/Known supports MF2 natively, but support was sadly lacking in Elgg, so I wrote a quick plugin to add it.
    Overview
    The plugin, once installed and activated, adds some hidden markup to certain core views and does some basic post processing to add MF2 markup.
    It is designed to be as light weight as possible, while still providing useful functionality.
    As I mentioned above, and in the readme, this was primarily written to scratch an itch – namely, to provide the base functionality for IdnoElgg distributed friending functionality. Cross login functionality will come, hopefully, when I have time (although if you’re in a hurry you could always provide an incentive!).
    » Visit the project on Github…

    Share this:EmailLinkedInTwitterGoogleFacebookReddit

Leave a Reply