【问题标题】:Access the custom fields in joomla 3访问 joomla 3 中的自定义字段
【发布时间】:2017-07-20 14:44:53
【问题描述】:

在 joomla 3 中,您可以添加自定义字段。它们保存在“_fields”下的 joomla mysql 数据库中。

我创建了一个名为“区域”的新自定义字段。用户可以在他们的配置文件设置中添加一个值。所以我现在的问题是,如何通过 php 调用这个自定义字段的用户值?

我知道如何调用用户的 profile 值,例如:

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

那么如何调用 _field 值?

【问题讨论】:

  • 你试过 print_r($userProfile);然后就可以清楚的看到里面的所有字段了。
  • 是的,只有配置文件参数可用。自定义字段值不在里面。

标签: php mysql joomla


【解决方案1】:

当您在管理区域(用户字段)中添加自定义字段时 您可以在默认个人资料页面中查看完整的用户个人资料

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

这是截图

然后检查实现。我是通过这种方式访问​​它的。

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

你也可以试试

    print_r($customFields);

只是看看里面有什么。

让您知道您可以访问它。

希望这会有所帮助。

【讨论】:

    【解决方案2】:

    如果您从管理面板添加新的自定义字段,$customFields 数组中的对象数量将会改变。并且您访问将指向其他领域。您应该通过其名称访问字段。您应该添加通过名称查找自定义字段的功能。

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-11-03
      • 1970-01-01
      • 2014-10-09
      • 1970-01-01
      • 1970-01-01
      • 2014-05-25
      • 1970-01-01
      相关资源
      最近更新 更多