2

In joomla 3 you can add custom fields. They are saved in the joomla mysql database under "_fields".

I´ve created a new custom field called "region". The users can add a value in their profile settings. So my question is now, how can i call the users value of this custom field via php?

I know how to call the users profile values for example:

jimport( 'joomla.user.helper' );
$user = JFactory::getUser();
$userId = $user->id;
$userProfile = JUserHelper::getProfile( $userId );
$usercity = $userProfile->profile['city'];

So how can I call the _field values?

2
  • Have you tried a print_r($userProfile); Then you can clearly see all the fields therein.
    – Oluwaseye
    Jul 20, 2017 at 14:52
  • Yes, there are just the profile params available. The custom field values aren´t inside.
    – Robster
    Jul 20, 2017 at 14:58

2 Answers 2

2

When you add the custom field in the admin area (user fields) You can view the full user profile in the default profile page

http://localhost/joomla/index.php?option=com_users&view=profile

Here's a screenshot enter image description here

Then inspect the implementation. I came across accessing it this way.

$customFields = FieldsHelper::getFields('com_users.user', JFactory::getUser(), true);
// In my case there where only one additional field, so a took the 0-indexed value, you shall see in which index is the field you are searching for
$customFields[0]->value;

You can also try

    print_r($customFields);

Just to see whats in it.

Just so you know you can access it.

enter image description here

Hope this helps.

0
0

If you add new custom field from administration panel, number of object in $customFields array will change. And you access will poit to other field. You should access to field by it's name. You should add function for finding custom field by it's name.

$customFields = FieldsHelper::getFields('com_users.user', JFactory::getUser(), true);

public static function getValueByFieldName($fields, $field_name) {
    foreach ($fields as $field) {
        if ($field->name == $field_name){
            return $field->value;
            break;
        }
    }
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.