phppluginselgg

Change default members tab to popular in elgg


I'm writing a plugin in elgg to change default tab of elgg members to popular instead of newest in members list. But there is a problem. As I set default tab to popular then newest tab not working anymore. here is my start.php file:

<?php
elgg_register_event_handler('init', 'system', 'plug_test_init');

function plug_test_init(){

    elgg_unregister_page_handler('members');
    elgg_register_page_handler('members', 'set_popular_as_default');

}   
function set_popular_as_default($page) {
    if (empty($page[0])) {
        $page[0] = 'popular';
    }
    if ($page[0] == 'search') {
        echo elgg_view_resource('members/search');
    } else {
        echo elgg_view_resource('members/index', [
            'page' => $page[0],
        ]);
    }
    return true;
}

Solution

  • Thanks to iionly in elgg community, in this discussion The problem solved by a using hooks of members:

    <?php
    elgg_register_event_handler('init', 'system', 'plug_test_init');
    
    function plug_test_init(){
        elgg_unregister_plugin_hook_handler('members:config', 'tabs', "members_nav_newest");
        elgg_register_plugin_hook_handler('members:config', 'tabs', "my_members_nav_newest");
        elgg_unregister_page_handler('members');
        elgg_register_page_handler('members', 'set_popular_as_default');
    
    }   
    function set_popular_as_default($page) {
        if (empty($page[0])) {
            $page[0] = 'popular';
        }
        if ($page[0] == 'search') {
            echo elgg_view_resource('members/search');
        } else {
            echo elgg_view_resource('members/index', [
                'page' => $page[0],
            ]);
        }
        return true;
    }
    function my_members_nav_newest($hook, $type, $returnvalue, $params) {
            $returnvalue['newest'] = array(
                'title' => elgg_echo('sort:newest'),
                'url' => "members/newest",
            );
         return $returnvalue;
    }