Adding Owned Groups to Ownerblock Menu

Can anyone help me fix these code correctly:

function custom_theming_owner_block_menu($hook, $type, $return, $params) {
    if (elgg_is_active_plugin('groups') && elgg_instanceof($params['entity'], 'user')) {

        $user = elgg_get_logged_in_user_entity();
        $groups = $user->getGroups(array(''), 4);

        foreach ($groups as $group) {
            $url =  $group->getURL();
            $item = new ElggMenuItem('group-$group->guid', elgg_view_icon('circle-o-notch') . $group->name, $url);
            $return[] = $item;
        }}
    return $return;
}

I tried that but only one group shows up. I need a limit of atleast four groups.

  • If you ask about groups that created by user then try this:

    $dbprefix = elgg_get_config('dbprefix');
    
    $groups  = elgg_get_entities_from_relationship(array(
        'type' => 'group',
        'owner_guid' => $user->guid,
        'limit' => 4,
        'full_view' => false,
        'joins' => array("JOIN {$dbprefix}groups_entity ge ON e.guid = ge.guid"),
        'order_by' => 'ge.name asc',
    ));
        
    if(!empty($groups)) {
        foreach ($groups as $group) {
            elgg_register_menu_item('page', array(
                'section' => 'groups',
                'name' => "group-$group->guid",
                'text' => $group->name,
                'href' => $group->getURL(),
            ));
        }
    }
  • Cool, thanks but it works only for page menu. Is it possible to do same for owner block menu instead?

  • elgg_register_plugin_hook_handler('register', 'menu:owner_block', 'custom_owner_block_menu');
    
    function custom_owner_block_menu($hook, $type, $return, $params) {
    
    $dbprefix = elgg_get_config('dbprefix');
    
    $user = elgg_get_page_owner_entity();
    
    $groups  = elgg_get_entities_from_relationship(array(
        'type' => 'group',
        'owner_guid' => $user->guid,
        'limit' => 4,
        'full_view' => false,
        'joins' => array("JOIN {$dbprefix}groups_entity ge ON e.guid = ge.guid"),
        'order_by' => 'ge.name asc',
    ));
        
    if(!empty($groups)) {
        foreach ($groups as $group) {
            $url = $group->getURL();
            $item = new ElggMenuItem("group-$group->guid", $group->name, $url);
            $return[] = $item;
        }
    }
    
    return $return;
    }

    Learn hooks

  • Awesome! working perfectly.... Thank you.