How to customize register form

Hi everyone,

I wanna create a customized register form for my elgg based website by creating a new plugin since profile manager isn't compatible with elgg 1.9.3. I've read previous discussions about this issue. So far I've created manifest.xml and copied register.php in actions/register.php to mod/mycutomplugin/actions and views/default/forms/register.php to mod/cutomregister/views/default/forms and changed the last one (view). The problem I have in writing start.php file. The following is my code for start.php:

elgg_register_event_handler('init', 'system', 'cutomregister_init');
function cutomregister_init() {
   $action_path = elgg_get_plugins_path() . "cutomregister/actions/register.php";
   elgg_register_action("register", $action_path, 'public');           
   elgg_register_page_handler('register', 'cutomregister_page_handler');

}

function cutomregister_page_handler() {
    $content = elgg_view_form('register');
    $params = array(
         'title' => '',
         'filter' => '',
        'content' => $content
    );

    $body = elgg_view_layout('one_column', $params);

    echo elgg_view_page('', $body);
    return TRUE;

}

The problem is that elgg_view_form('register') displays the default elgg registration form not my customized form (mod/cutomregister/views/default/forms/register.php).

I'd really appreciate that you help me with writing the proper code for this matter (and not just explaining by words), considering that I'm new to elgg.

Thanks in advance

 

  • Sounds like you might have system cache enabled. Disable it or flush the caches after adding/removing views.

  • I disabled system cache and it worked . Thanks to Ismayil Kharedinov.

  • I've faced another problem. After adding two new fields (named province and city) to the form and their corresponding columns (named province and city) to elgg_users_entity table in database, I modified register action (customregister/actions/register.php) by adding following codes in 'get variables' section:
    $email = get_input('email');

    $province = get_input('province');

     and changed the following code :

    $guid = register_user($username, $password, $name, $email, $city, $province);

    but after submitting the form, it says there was an internal error in registration.

    What did I miss? What else do I have to modify in order to store user data in the database?

  • After adding [...] columns (named province and city) to elgg_users_entity table in database

    No, do not modify the database!

    You don't have to manually add new columns. You can add new properties for an object as simply as this:

    // ElggUser object
    $user->city = $city;

    See: http://learn.elgg.org/en/1.9/design/database.html#metadata

    I modified register action (customregister/actions/register.php) 

    This would be best done by registering a plugin handler for the 'register', 'user' hook. See: http://learn.elgg.org/en/1.9/design/events.html#plugin-hooks

    You can call $province = get_input('province'); inside the hook handler and save the custom data to the ElggUser object that gets passed into the handler in the fourth parameter. The parameter is an array and the ElggUser can be found with the key 'user'.

  • Simply to save that extra fields, you don't need to override the registration action. You can use

    elgg_register_event_handler("create", "user", "my_registration_event");    
    function my_registration_event($event, $object_type, $object){
        if (($object instanceof ElggUser) && ($event == 'create') && ($object_type == 'user')) {
            $city  = get_input('city ');
            if ($city){
                create_metadata($object->guid, 'city', $city , 'text', 0, ACCESS_PUBLIC);
            }    
            return true;
        }    
    }    

    to get the data back, you can use

    echo $user->city;

  • @Juho Jaakkola  and @Team Webgalli : Thank you guys. I followed the Team Webgalli's way and registration was successful. Now I want to have those fields in user profile. But when I add $user->city to for instance details.php  in profile plugin, nothing is shown. I checked database and the additional fields (city and province) were not stored in elgg_metdata, elgg_metastrings and ellg_user_entity tables. @Team Webgalli : Would you tell me in which table they're stored ? and How can I retrieve them in user profile?

  • There is a small typo in that code. It should be

    $city  = get_input('city');

  • I made a mistake in creating metadata. It turns out  that each field must be added by create_metadata function separately :

        create_metadata($object->guid, 'city', $city, 'text', 0, ACCESS_PUBLIC);
        create_metadata($object->guid, 'province', $province, 'text', 0, ACCESS_PUBLIC);

    The values are stored in elgg_metastrings table and now I can echo them in user profile. Thank you all.