Access the custom fields in joomla 3

醉酒当歌 提交于 2020-05-27 13:09:20

问题


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?


回答1:


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

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.

Hope this helps.




回答2:


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;
        }
    }
}


来源:https://stackoverflow.com/questions/45218017/access-the-custom-fields-in-joomla-3

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!