Fetching a users selected custom field value in Joomla 4

For Joomla! 4.x Coding related discussions, you could also use: http://groups.google.com/group/joomla-dev-general

Moderator: ooffick

Forum rules
Post Reply
JooN00b
Joomla! Apprentice
Joomla! Apprentice
Posts: 16
Joined: Wed Dec 07, 2022 6:40 pm

Fetching a users selected custom field value in Joomla 4

Post by JooN00b » Wed Dec 07, 2022 8:30 pm

In Joomla 3 i used an external profile extension and could fetch the value of a custom user field with

Code: Select all

$user = JFactory::getUser();
$valueXYZ = JUserHelper::getProfile($user->id)->field_xyz;
I have now created the corresponding custom user fields in the native Joomla 4 component com_fields and the context com_users.user. I can't seem to find a simple solution lika above so instead i currently have solved it as follows:

Code: Select all

$user = \Joomla\CMS\Factory::getApplication()->getSession()->get('user');
use \Joomla\Component\Fields\Administrator\Helper\FieldsHelper;
if ($user->id != NULL) {
  foreach (FieldsHelper::getFields('com_users.user', $user, 0) as $field) {
            if ($field->name == "field_xyz")
            {
	           $valueXYZ = $field->value;
            }
            
        }
}
It seems way to complicated and to go through 40 field with foreach every time seems inefficient. I do not want to hard code the ID of the specific field in each module/component. By doing so it would be sufficient to use the code

Code: Select all

$user = \Joomla\CMS\Factory::getApplication()->getSession()->get('user');
JModelLegacy::addIncludePath(JPATH_ADMINISTRATOR . '/components/com_fields/models', 'FieldsModel');
$fieldModel = JModelLegacy::getInstance('Field', 'FieldsModel', array('ignore_request' => true));
$value = $fieldModel->getFieldValue(7, $user->id);
What simple solution am I missing?

User avatar
toivo
Joomla! Master
Joomla! Master
Posts: 15443
Joined: Thu Feb 15, 2007 5:48 am
Location: Sydney, Australia

Re: Fetching a users selected custom field value in Joomla 4

Post by toivo » Wed Dec 07, 2022 10:37 pm

Welcome to the Joomla forum!

You are right about using the names of custom fields, rather than field ids. Using field names makes the PHP code self documenting. Here is an example with switch and case statements:

Code: Select all

use Joomla\CMS\Factory;
use Joomla\Component\Fields\Administrator\Helper\FieldsHelper;
$customFields 	= FieldsHelper::getFields('com_users.user', Factory::getUser(), true); 
$fieldA		= '';
$fieldB		= '';
$fieldC		= '';
foreach ($customFields as $field)
{
	switch ($field->name)
	{
		 case 'fielda':
			$fieldA	= $field->value;
		 case 'fieldb':
			$fieldB	= $field->value;
		 case 'fieldc':
			$fieldC	= $field->value;			
		default:
	}
}
Toivo Talikka, Global Moderator

JooN00b
Joomla! Apprentice
Joomla! Apprentice
Posts: 16
Joined: Wed Dec 07, 2022 6:40 pm

Re: Fetching a users selected custom field value in Joomla 4

Post by JooN00b » Thu Dec 08, 2022 9:29 am

Thanks for your response and the welcome @Toivo.

Both your solution and mine uses a foreach-loop that I would prefer to avoid.

Each time I use the suggested solution I actually only need one or two values of the 40 custom fields so iterating through all 40 just to for example fetch a single value feels quite ineffective.

Is there any other function that directly fetches the value of a single custom user field that I have missed?

User avatar
toivo
Joomla! Master
Joomla! Master
Posts: 15443
Joined: Thu Feb 15, 2007 5:48 am
Location: Sydney, Australia

Re: Fetching a users selected custom field value in Joomla 4

Post by toivo » Fri Dec 30, 2022 6:00 am

Just found a shorter way using the PHP function array_column(). This example has user custom fields fielda, fieldb and fieldc.

Code: Select all

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 '<div><p>values = ' . print_r($values, true) . '</p></div>';
echo '<p>fielda = ' . $values['fielda']  . '</p>';
echo '<p>fieldb = ' . $values['fieldb']  . '</p>';
echo '<p>fieldc = ' . $values['fieldc']  . '</p>';
It would be interesting to compare the elapsed time of this function and the foreach loop.
Toivo Talikka, Global Moderator


Post Reply

Return to “Joomla! 4.x Coding”