Custom Profile fields - best use

Hi,

Does somebody know what is the best way to add a custom field to a user profile and then get the value in a custom view ?

I have added a custom field via the administration page (http://localhost/elgg/admin/appearance/profile_fields) but i don't know how I can retrieve the value of this field. (There is no specific id set in the administration page for each new fields ?)

I retrieve the current user with: $user = elgg_get_logged_in_user_entity();

 

Thanks for you help

  • If you do not want to use profile manager, try and look at this plugin in the link after this sentence and see how easy it is to add any profile field to profile fields.

    https://elgg.org/plugins/2665852

    Look at the start.php file for a number of fields and then look at the action file how the fields are processed. Then look at a file called /profile/basic and see how the form is made for the profile fields

    Hope this helps.

    Posted via cell phone.

  • I also don't think that the admin interface to add custom profile fields is of much use (if you want to do more than just display these fields and their values on profile pages). It seems the custom fields are addressed by id internally (which is of course not very intuitive and might cause trouble if you ever change the order of the fields).

    Don't know how the plugin does it @Tom has linked. But there's the 'profile:fields', 'profile' plugin hook you can use. Just register a function to add a profile field / fields in the init funtion of your plugin:

    elgg_register_plugin_hook_handler('profile:fields', 'profile', 'my_custom_profile_fields_handler');

    And then add the my_custom_profile_fields_handler() function also to start.php of your plugin:

    function my_custom_profile_fields_handler($hook, $type, $return_value, $params) {
    
        // Types of fields: 'text', 'longtext', 'tags', 'url', 'email', 'location', 'date'
        // Add to language file(s) strings for the field label(s) like 'profile:my_first_custom_profile_field' => "Label for my first custom field",
    
        $return_value['my_first_custom_profile_field'] = 'text';
        $return_value['my_second_custom_profile_field'] = 'longtext';
    
        return $return_value;
    }

    And then you should be able to get the values saved in a field for a user (e.g. the logged in user) with

    $user = elgg_get_logged_in_user_entity();
    $value = $user->my_first_custom_profile_field;
  • Thanks, that works perfectly !

    do you know if  there is a specific hook to unregister default fields ?

  • Should work with the same plugin hook:

    function my_custom_profile_fields_handler($hook, $type, $return_value, $params) {
    
        // Following array contains the profile fields that should NOT(!!) be used
        // As example all default fields listed here - remove the WANTED fields from the array
        $profile_defaults_to_remove = array (
            'description' => 'longtext',
            'briefdescription' => 'text',
            'location' => 'location',
            'interests' => 'tags',
            'skills' => 'tags',
            'contactemail' => 'email',
            'phone' => 'text',
            'mobile' => 'text',
            'website' => 'url',
            'twitter' => 'text',
        );
    
        // Array of profile fields to be used
        $my_profile_fields = array();
        // First remove the unwanted fields
        foreach($return_value as $name => $type) {
            if (!array_key_exists($name, $profile_defaults_to_remove)) {
                $my_profile_fields[$name] = $type;
            }
        }
    
        // Now add custom fields
        // Types of fields: 'text', 'longtext', 'tags', 'url', 'email', 'location', 'date'
        // Add to language file(s) strings for the field label(s) like 'profile:my_first_custom_profile_field' => "Label for my first custom field",
    
        $my_profile_fields['my_first_custom_profile_field'] = 'text';
        $my_profile_fields['my_second_custom_profile_field'] = 'longtext';
    
        return $my_profile_fields;
    }
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