Add user to an invisible group programmatically

Hi. everyone;

I am building a simple plugin to add my user automatically in invisible group.

I am using this fonction provided by elgg.

function groups_join_group($group, $user) {

    // access ignore so user can be added to access collection of invisible group
    $ia = elgg_set_ignore_access(TRUE);
    $result = $group->join($user);
    elgg_set_ignore_access($ia);

    if ($result) {
        // flush user's access info so the collection is added
        get_access_list($user->guid, 0, true);

        // Remove any invite or join request flags
        remove_entity_relationship($group->guid, 'invited', $user->guid);
        remove_entity_relationship($user->guid, 'membership_request', $group->guid);

        elgg_create_river_item(array(
            'view' => 'river/relationship/member/create',
            'action_type' => 'join',
            'subject_guid' => $user->guid,
            'object_guid' => $group->guid,
        ));

        return true;
    }

    return false;
}

In my settings only admin user can create invisible group.

- When non admin user is connected the above code works perfectly for public group. (non admin user is attributed to a public group)

- But the above function is not working for invisible group. When non admin user is connected the following error code is generated and the user is not attributed to invisible group.

PHP Fatal error:  Call to a member function join() on boolean in /home/pessek/Documents/WEB/elgg/mod/pessek_linkedin/views/default/resources/home.php

What am I suppose to do for automatically allocated non admin user to invisible group programmatically ??

Best regard.

Beginning Developers

Beginning Developers

This space is for newcomers, who wish to build a new plugin or to customize an existing one to their liking