Search code examples
phpmysqljoomla

Access the custom fields in joomla 3


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?


Solution

  • 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.