J4! | Accessing VALUE of User Custom Field?

53 views
Skip to first unread message

Bruce Decker

unread,
Feb 28, 2024, 8:12:56 PMFeb 28
to Joomla! General Development
In J3!, I was able to do this to get the labels and values from User custom fields:
[code]

JLoader::register('FieldsHelper', JPATH_ADMINISTRATOR . '/components/com_fields/helpers/fields.php');

if ($cspUserId == ''){

      ///deprecated|J3!|$cspUserId = JFactory::getUser();

                  $cspUserId = Joomla\CMS\Factory::getApplication()->getIdentity()->id; //J4!

   }

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

foreach ($customFields as $customField) {

   $cspReturn[$customField->title] = $customField->value;   

}

[/code]

However, now when I view the array of objects from FieldsHelper::getFields, the property 'value' is not present.  I need to read the values from users custom fields in my component.  Are they accessible with another class or method?  If so, can someone point me the way?   I'd prefer to use the Joomla API, but if needed, I could read directly from the database.

Thanks!

Bruce Decker

unread,
Feb 29, 2024, 9:25:49 PMFeb 29
to Joomla! General Development
I figure it out.
use Joomla\CMS\Factory; //!J4
$cspUserId = Joomla\CMS\Factory::getApplication()->getIdentity()->id;
$user = \Joomla\CMS\Factory::getApplication()->getSession()->get('user');
$customFields = FieldsHelper::getFields('com_users.user', $user, 0); echo '$cspUserId=' . $cspUserId . '<br>';
foreach ($customFields as $field) {
    echo '$field=' . $field->name . '<br>';
    echo '$value=' . $field->value . '<br>';
    echo '<br>--<br>';
 }

Toivo Talikka

unread,
Mar 1, 2024, 6:28:42 AMMar 1
to Joomla! General Development
There is another way that does not use the foreach loop:

use Joomla\CMS\Factory;
use Joomla\Component\Fields\Administrator\Helper\FieldsHelper;
$customFields = FieldsHelper::getFields('com_users.user', Factory::getUser(), true);
$values = array_column($customFields, 'value', 'name');
// echo 'values = ' . print_r($values, true);
echo 'fielda = ' . $values['fielda'] . ' fieldb = ' . $values['fieldb'] . ' fieldc = ' . $values['fieldc'];

Reply all
Reply to author
Forward
0 new messages