Adding and Saving as metadata a new registration field's information

Hi all, I new to elgg and trying to modify registration form.

Sorry, for this old question but all the answers that i have seen, no where is a clear discussion using suggestive code of adding the data from new field as metadata through extension of register action.

So, I started off by extending the register/extend view :

---------------------------------------------------------------------------


<?php
$options = array(
'male'=>'Male',
'female'=>'Female',
);
echo elgg_view('input/dropdown', array(
'name' => 'gender',
'options_values' => $options,
'required'=>true,
));
?>

---------------------------------------------------------------------------

and then, I tried to extend the action using elgg_register_plugin_hook_handler

-------------------------------------------------------------------------------------------------------------------

elgg_register_plugin_hook_handler("action", "register", "add_new_field_gender");

function add_new_field_gender($hook, $entity_type, $returnvalue, $params) {

$input = get_input('gender');

// Problem here : how do i add this data to user object that is going to be formed after registration as metadata

if (someuserobject->save()) {
...
return true;
}
return false;
}
---------------------------------------------------------------------------

Now, how do I add this information that i get from gender field as metadata to that specific user object. Since, i suppose that register_user() function is going to be executed after this which by the way uses no object to whom we could add this data as metadata, how do we go about adding the info?

  • Generally would just recommend using "profile manager" for this.

    If you don't want to use their plugin you can still look at the code to see how they do it.

  • Thanks Evan for a quick reply,
    I have seen and used the Profile Manager plugin and I dont want to use it since I would like to have more control than what the plugin provides.

    I just want to know if I am on right track and i guess it shouldnt be tough to store it as metadata.

    Since, i am quite new to elgg and dont have much experience, I couldnt figure out exactly how the plugin manages to do it.
    Although I am trying to understand its working, in the meantime, can you help me doing it my way, i think its possible, but just dont know where to store.

  • ok, so, as far as i have understood, profile manager plugin basically overides the original actions/register action by a actions/useradd.php file. It stores the custom field inputs in some variables and after registering a user with register_user() function, it gets its guid and adds custom field data stored as metadata to it, simple enough.

    But the problem is that, if I try to extend the action, by registering a plugin hook handler function, it will attempt to perfom the function before the action and thereby making it impossible to store before the user has been registered, that seems to make sense now.

    So, a possible work around i guess would be to override the register action and do the same thing but i will not be able to use profile manager plugin with it, ofcourse. Cool i have some thing now. But it would be way better if i could just extend the action, and add the field data to it. If anyone sees this thread, please share your ideas and suggestions...

  • don't worry about the action, you can use the 'create', 'user' event to add your metadata

    When a user is created, check for your custom inputs, save the metadata, and let the event continue

  • @Matt :

    Is this what you're saying that i register an event handler for create user :

    elgg_register_event_handler('init','system','new_user_signup_init');

    function new_user_signup_init() {

      elgg_register_event_handler('create', 'user', 'new_user_signup_add_metadata');

    }

    and then, use the event handler function to add metadata to user object :

    function new_user_signup_add_metadata($object,$user) {

    if (($object) && ($object instanceof ElggUser)){

        $gender = get_input('gender');

        $user->gender = $gender;

        return true;

    }

    return false;

    }

    But, can i fetch custom inputs like this in this event handler? or is it something else that you were trying to say, with checking custom inputs?

     

  • That's about right. You probably also want to vet the input to make sure it is a value you expect.

  • Yep, though the arguments for your function are wrong

    Instead of: 

    new_user_signup_add_metadata($object,$user)

    It should be

    new_user_signup_add_metadata($event, $type, $user)

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