Customize logout

I'm using Elgg 2.3.14 and looking to override/extend the logout action to include some custom code.  My plugin /var/www/html/mod/my-plugin/actions/logout.php has been created which has my custom code... however none of my code is being executed.  Do I need to register my custom action in the start.php and include an "actions" property in the elgg-plugin.php under my plugin's root directory?  If so, I couldn't find what the actual structure for including actions in elgg-plugin.php was.

  • In first, elgg-plugin.php is not an Elgg 2 file but Elgg 3.

    For custom Elgg 2 action you need to register it in your start.php.

  • Thanks,, that worked.. another question regarding my custom logout.  I'd like to update the account menu to point to that new logout action.  currently I have this in my start.php:

     

    $item = elgg_get_menu_item('topbar', 'logout');

    if ($item) {

    system_message("got item");

    $item->setParentName('account');

    $item->setText(elgg_echo('logout'));

    $item->setPriority(104);

    $item->setHref('action/tsnlogout');

    $item->setName(elgg_echo('tsnlogout'));

    } else {

    system_message("didn't get item");

    }

    but the menu isn't updating with the new info and my system message "didn't get item" prints out when the page loads.

  • found it... nevermind :-)  added 'parent_name' property when registering the menu item.

  • Use menu hook.

    Another way is:

    elgg_register_action('logout', __DIR__ . '/actions/tsnlogout.php');

    So, you don't need to add the menu's hook.

  • In this case you should use a hook.

    My suggestion is a new Class:

    start.php

    elgg_register_plugin_hook_handler('register', 'menu:topbar', [\MyPlugin\Menus::class, 'setupTopbarMenu']);

    \mod\your-cool-plugin\classes\MyPlugin\Menus.php

    <?php
    namespace MyPlugin;
    
    use ElggMenuItem;
    
    class Menus {
    
    public static function setupTopbarMenu($hook, $type, $return, $params) {
      $return[] = \ElggMenuItem::factory([
        'name' => 'tsnlogout',
        'href' => 'action/tsnlogout',
        'is_action' => true,
        'text' => elgg_view('output/img', [
                   'src' => 'link-to-image',
                 ]),
        'parent' => elgg_echo('account'),
        'priority' => 104,
      ]);
    
      return $return;
     }
    
    }
    
    
  • ok.. so now, I'm looking for an easy way to remove the original logout menu.  I see elgg_unregister_menu_item and tried:

    elgg_unregister_menu_item('topbar', 'account/logout');

    but that didn't work.. Guessing I need to somehow get the parent item and then somehow call out the child 'logout' item in order to remove it?

  • Also, I tried to add an image to my topbar menu that will link to another page.  I have factChain-Pen-badg3.png located in my /var/www/html/mod/fc-plugin/graphic/factChain-Pen-badge3.png and have the following code in start.php:

    $text = elgg_view('output/img', array(

    'src' => elgg_get_site_url(). 'mod/fc_plugin/graphic/factChain-Pen-badge3.png',

    'alt' => '', 'class' => '',

    'title' => '',

    'width' => '',

    'height' => '',

    ));

    elgg_register_menu_item('topbar', array(

    'name' => 'factChainPen',

    'href' => 'https://mytesturlFC.com',

    'text' => $text,

    'priority' => 1000,

    'section' => 'alt',

    ));

    doesn't appear at all :-(

  • Thanks for the help, yes learn,try,search is the best approach, some of these items have required a quick turnaround so sometimes the research time makes it difficult to hit some deadlines.. It doesn't feel good asking for solutions without being thorough first so I know where you're coming from.  So thanks for all the help you've provided thus far. This is my first project with elgg and php so it has definitely been a learning experience.