I did this little helper for getting the custom field values:
namespace Your\Component\Name\Administrator\Helper;
\defined('_JEXEC') or die;
use Joomla\Component\Fields\Administrator\Helper\FieldsHelper;
/**
* Pro Fields Helper
*
* @since __BUMP_VERSION__
*/
class ProFieldsHelper
{
public $userKeyedCustomFields;
function __construct(string $context, $item)
{
// Get field list
$userCustomFields = FieldsHelper::getFields($context, $item, true);
$this->userKeyedCustomFields = array_column($userCustomFields, null, 'name');
}
/**
* Get the custom field value, or if empty the $default value
*/
function getFieldValue(string $fieldName, string $default=null)
{
return $this->userKeyedCustomFields[$fieldName]->value ?? $default;
}
}
So you can call it anywhere:
use Your\Component\Name\Administrator\Helper\ProFieldsHelper;
...
// Get custom field 'telephone' for a user (defaults to '000000000')
$user = Factory::getUser();
$userCustomFields = new ProFieldsHelper('com_users.user', $user);
$tel = $userCustomFields->getFieldValue('telephone', '000000000');
EDITED
Applied changes proposed by @mickmackusa in his comment
foreach()
is:$nameKeyedCustomFields = array_column($customFields, null, 'name');