Hi,
really (!) love the Customs Fields in Joomla 3.7.x
I am currently looking at how to use them from my own component.
I run into something odd (at least for me) and just wanted to double check with the community :)
I want to add the com_user.user fields to my user object:
$user = JFactory::getUser($userId);
// Check for class FieldsHelper (only available in Joomla version 3.7+
if (class_exists('FieldsHelper'))
{
$jcFields = FieldsHelper::getFields('com_users.user', $user, false);
// Add Custom Fields to user object
foreach ($jcFields as $jcField)
{
$user->{$jcField->name} = $jcField->value;
}
}
This works perfect :) I have e.g. a custom field 'nickname' and for user 123 I set the value to 'sjaak'
$user->nickname = 'sjaak'
so far so good.
Now I want to do the same for articles, but when using the code above with the article object and com_content.article $jcFields holds the custom fields for articles, but not the value?
I can solve that by getting the values via the model -> getFieldValue
$article = JTable::getInstance("content");
$article->load($articleId);
// Check for class FieldsHelper (only available in Joomla version 3.7+
if (class_exists('FieldsHelper'))
{
// com_content.article fields are only available after trigger onContentPrepare > need to get value via FieldsModel
$fieldModel = JModelLegacy::getInstance('Field', 'FieldsModel', array('ignore_request' => true));
$jcFields = FieldsHelper::getFields('com_content.article', $articleId, false);
// Add Custom Fields to article object
foreach ($jcFields as $jcField)
{
$fieldValue = $fieldModel->getFieldValue($jcField->id, $articleId);
$article->{$jcField->name} = $fieldValue;
}
}
Is this expected behavior: the difference between custom fields for users and for articles? or am I approaching this the wrong way?