How to perform something after user validates registration by email?

Hello.

I am trying to write a plugin (Elgg 2.2.1), that, after a new user validates registration by email (and only after the user is validated), creates a new group based on some of the data submitted by registration form and joins the user to this new group. Also, the new group should not be created and owned by newly registered user, but by special "administrator" user, which is intended owner of all the groups created in such manner.

I tried doing it like this:

in start.php:

elgg_register_plugin_hook_handler( 'action', 'uservalidationbyemail/validate', 'validate_action_hook' );

function validate_action_hook($hook, $type, $value, $params) {
    $user = elgg_get_logged_in_user_entity();
    if( elgg_get_user_validation_status($user->guid) == true ) {
        $userdata1 = $user->data1;
        $userdata2 = $user->data2;
        $groupname = $address.' '.$city;
        $group = elgg_get_entities_from_metadata( array( 'type' => 'group', 'metadata_name_value_pairs' => array( array( 'name' => 'name', 'value' => $groupname ) ) ) );
        if( !elgg_instanceof($group, 'group') ) {
            $gdesc = $groupname;
            $group_owner_guid = elgg_get_entities_from_metadata( array( 'type' => 'user', 'metadata_name_value_pairs' => array( array( 'name' => 'username', 'value' => 'administrator' ) ) ) )->guid;
            $group = new ElggGroup();
            $group->subtype = 'subtype1';
            $group->name = $groupname;
            $group->description = $gdesc;
            $group->membership = ACCESS_PRIVATE;
            $group->owner_guid = $group_owner_guid;
            $group->save();
        }
        $group->join( $user );
        return true;
    }
    return false;
}

Unfortunately, this doesn't work, and the Elgg documentation wasn't very helpful with providing me the right answers.

  • Group names are stored in the groups_entity table and are not metadata. They are attributes. Use elgg_get_entities_from_attributes()

  • Fixed it:

    $groupList = elgg_get_entities_from_metadata( array( 'types' => 'group', 'subtypes' => $my_subtype, 'name' => $groupname ) );

    Now I just need to figure out why users can't see the group they belong to and other group members in front-end. Here is how I create the group:

                $group = new ElggGroup();
                $group->subtype = $my_subtype;
                $group->name = $groupname;
                $group->description = $gdesc;
                $group->membership = ACCESS_PRIVATE;
                $group->owner_guid = $group_owner_guid;
                $group->setContentAccessMode( 'members_only' );
                $group->save();

    BR,
    ZP

    EDIT: I will use the function you suggested. Thanks a lot, again!

  • ...

                $group->save();
                $group->access_id = $group->group_acl;
                $group->save();

     

    Did the trick. Now everything works as intended. Thank you all for help, consider this solved.

    BR,
    ZP

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