Log in

How to alter a rendered view

Overriding views is a little like forking: you don't get upstream core/plugin changes. In Elgg 1.8+ you can alter views--to do more surgical alterations--after they've been rendered. You do this using the "view" plugin hook.

Example: remove breadcrumbs that have no links

// arguments are 'view', the view name, and your handler callback
elgg_register_plugin_hook_handler('view', 'navigation/breadcrumbs', 'myPlugin_alter_breadcrumbs');

function myPlugin_alter_breadcrumbs($hook, $type, $returnValue, $params) {
    // $hook is 'view', $type is the view name, $returnValue is the string
    // output of the view, and $params['viewtype'] has the view type.

    if ($params['viewtype'] === 'default') {
        if (false === strpos($returnValue, '<a ')) {
            $returnValue = ''; // if no link, remove completely
        }
    }
    return $returnValue;
}

 

Comments

  • Steve Clay 812 days ago

    Another difference to consider: even if another plugin overrides the view, your handler function will still be called. This can be good or bad depending on how that plugin changes the view and how your handler operates.

  • Matt Beckett 812 days ago

    Thanks for reminding me about this steve, I really under-utilize this plugin hook as it's quite easy to extend or overwrite a view, but this does definitely have some advantages in certain situations.

  • Ismayil Kharedinov 812 days ago

    +1

    Haven't used that hook since 1.7