1

I'm pretty new to Joomla, so sorry if this sounds silly.

I am using Extra Field "Date of Birth" from UserProfile in this code:

jimport( 'joomla.user.helper' );  
$user = JFactory::getUser();  
$userId = $user->id;  
$userProfile = JUserHelper::getProfile( $userId );  
$dob = $userProfile->profile['dob'];  
echo "dob = " . $dob ;  
echo "authorise =" . $user->authorise('core.admin', 'com_component');

Although $dob displays fine when logged in as a Super User, it won't show when logged in as a registered user.

So I'd like to display this field at a "basic non-administrator" level if it's possible.

Is there a way to do it? Or shall I temporarily change the User type to be able to access this field (I would obviously prefer not to...)?


Following the responses from mickmackusa and Sharky : Thank you so much for your answers. I'm pretty sure the registered user has a date set up, since it shows when turned into "Super User".

To help see better what's happening, I've changes the code a little :

jimport( 'joomla.user.helper' ); 
$user = JFactory::getUser();

echo "<br>--debug--";
echo "<br> id = " . $user->id ;
echo "<br> name = " . $user->name ;
echo "<br> username = " . $user->username ;

$isAdmin = $user->get('isRoot');
if ($isAdmin) {
echo '<br>You are an Administrator';
}
else  {
echo '<br>You are not Administrator';
}


$userId = $user->id; 
$userProfile = JUserHelper::getProfile( $userId );

$dob = $userProfile->profile['dob'];
echo "<br> dob = " . $dob ;
echo "<br> authorise =" . $user->authorise('core.admin', 'com_component');

echo "<br>var_dump = " . $userProfile; //var_dump(JUserHelper::getProfile( $userId ));

echo "<br>--debug--<br>";

And here's what I get:

  1. When logged as Super User:

As admin 2

This code displays:

As admin 1

  1. When logged as Adherent:

enter image description here

The same code displays:

As adherents


When logged in as a SuperUser, var_dump(JUserHelper::getProfile( $userId )); says:

 object(Joomla\CMS\Object\CMSObject)#656 (3) {
     ["_errors":protected]=> array(0) { }
     ["id"]=> string(4) "2399"
     ["profile"]=> array(1) {
         ["dob"]=> string(19) "2020-07-28 00:00:00"
     }
 }

When logged as Adherent, the same code says:

object(Joomla\CMS\Object\CMSObject)#1412 (2) {
    ["_errors":protected]=> array(0) { }
    ["id"]=> string(4) "2399"
}
  • On the surface, what you have posted seems right at first glance: How to display user profile information? , Joomla 3 get profile , Joomla: difference between JFactory::getUser and JUserHelper::getProfile , Access the custom fields in joomla 3 Are you able to provide further diagnostics? Are there any generated errors? What does var_dump(JUserHelper::getProfile( $userId )); say? – mickmackusa Aug 6 at 6:09
  • 1
    I can't reproduce the issue. Maybe the registered user doesn't have the date set up? – Sharky Aug 6 at 6:13
  • 1
    Go to Extensions / Plugins and find the User - Profile plugin and click on it to edit it. What is shown as Access? If it's set to Super Users for example, then you won't be able to get at it from other user groups. (As you probably know, this is the plugin which sets up your profile data for you). Try setting it to Public and see if that fixes things. – Robbie Jackson Aug 6 at 22:25
  • @Robbie Jackson Oh that did the trick indeed! I had not setup this extension myself, thus I didn't think of looking at that Access parameter for this. Thank you so much for your help, I'm very grateful. And thanks to all of the contributors to this post. – Lazare Aug 7 at 2:34
  • Good upvoteworthy question @Laz. Thank you for doing the diagnostics steps requested and progressing the status of your question. – mickmackusa Aug 7 at 6:34
1

Whenever you call JUserHelper::getProfile() the code in that function triggers a Joomla event 'onContentPrepareData' to which the Joomla user plugin User - Profile subscribes. This plugin adds in the profile data which has been stored for this user and returns it as an array.

However, plugins have an Access field which controls which User Groups have viewing access to that plugin. So if for example the Access is set to Super Users, then only users whose User Group(s) belongs to the Super Users Viewing Access Level will be able to include that plugin. For other users running the same code, the plugin will not be included, and the profile data won't be made available.

To fix the issue go to Extensions / Plugins and find the User - Profile plugin. Click on it to edit it and set the Access field to Public (or whatever is appropriate for your case).

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.