• Home
  • Consultancy
  • Contact
  • Extending actions in Elgg

    May 8th, 2009 by Marcus Povey

    Those eagle-eyed developers who have been tracking the Elgg core SVN may have noticed that I have recently committed a bunch of captcha related changes, including a simple captcha module.

    I just thought I’d write a quick post about it as this module makes use of a bit of Elgg functionality which has been around for a while, but that I know a number of plugin developers have missed.

    Namely, the ability to extend actions.

    When the Elgg framework calls an action the Action handler triggers a plugin hook called “action” before executing the action itself. This hook looks like this:

    $event_result = true;
    $event_result = trigger_plugin_hook('action', $action, null, $event_result);

    Where $action is the action being called. If the hook returns false then the main action will not be executed.

    The captcha module uses this to intercept the register and user/requestnewpassword actions and redirect them to a function which checks the captcha code. This check returns true if valid or false if not (which prevents the associated action from executing).

    This is done as follows:

    register_plugin_hook("action", "register", "captcha_verify_action_hook");
    register_plugin_hook("action", "user/requestnewpassword", "captcha_verify_action_hook");

    .
    .
    .

    function captcha_verify_action_hook($hook, $entity_type, $returnvalue, $params)
    {
    $token = get_input('captcha_token');
    $input = get_input('captcha_input');

    if (($token) && (captcha_verify_captcha($input, $token)))
    return true;

    register_error(elgg_echo('captcha:captchafail'));

    return false;
    }

    As you can see, this lets a plugin extend an existing action without the need to replace the action itself. In the case of the captcha plugin it allows the plugin to provide captcha support in a very loosely coupled way.

    Happy coding!

    Share and Enjoy: These icons link to social bookmarking sites where readers can share and discover new web pages.
    • Digg
    • del.icio.us
    • StumbleUpon
    • Reddit
    • Facebook
    • TwitThis
    • LinkedIn
    • NewsVine
    • Slashdot
    • Technorati
    • Google Bookmarks
    • email

    Related posts (automatically generated)

    3 Responses to “Extending actions in Elgg”

    1. Pedro Prez says:

      Very interesting Marcus!

    2. [...] the Captcha module extends a number of actions to require a correctly validated Captcha code. This list itself is the product of a plugin hook [...]

    3. sani hyne says:

      good explanation. i need to know one thing. what if we would like to redirect on our own selected page after generating the error if captcha not matched.?
      i use this captcha on above mentioned site. it works fine but error showing on dashboard except same page.

    Leave a Reply

    All content is © Copyright Marcus Povey 2008-2010 and released under a Creative Commons licence unless otherwise stated.

    Creative Commons License