2

I need to display the contact information of a contact on an article override (addresse, phone number...).
I can get the user with : $user = JFactory::getUser(); but how can I get a user contact details?

I tried using this :

JTable::addIncludePath(JPATH_ADMINISTRATOR . '/components/com_contact/tables');
$contact = JTable::getInstance('contact_details');    

echo '<pre>'; print_r($contact); echo '</pre>';

But it doesn't output anything.

3

You will firstly need to retrieve the user ID like so:

$id = JFactory::getUser()->id;

Then get the array of profile data, like so:

$profile = JUserHelper::getProfile($id)->profile;

You can now access any of the data:

echo $profile['address1'];
echo $profile['address2'];
echo $profile['city'];
echo $profile['region'];
echo $profile['country'];
echo $profile['postal_code'];
echo $profile['phone'];
echo $profile['website'];
echo $profile['favoritebook'];
echo $profile['aboutme'];
echo $profile['dob'];

You need to make sure you're logged in, else you won't be able to get the information:

Here's the full code with a sanity check:

$user = JFactory::getUser();

if (!$user->guest)
{
    $profile = JUserHelper::getProfile($user->id)->profile;

    echo $profile['address1'];
    echo $profile['address2'];
    echo $profile['city'];
    echo $profile['region'];
    echo $profile['country'];
    echo $profile['postal_code'];
    echo $profile['phone'];
    echo $profile['website'];
    echo $profile['favoritebook'];
    echo $profile['aboutme'];
    echo $profile['dob'];
}
  • Thanks for the quick reply, I have tested this but it outputs Undefined property: Joomla\CMS\Object\CMSObject::$profile. I removed ->profile and the outputed array is : Joomla\CMS\Object\CMSObject Object ( [_errors:protected] => Array ( ) [id] => 142 ). What am I doing wrong? – web-tiki Apr 27 '18 at 12:30
  • @web-tiki - You'll get this error if you're not logged in. I've updated the answer with a sanity check. – Lodder Apr 27 '18 at 12:43
  • Thx for the info. I didn't have the profile plugin enabled as I wanted to use the com_contact fields instead of the user profile. For this particular project, I can use the profile plugin thanks to your code but is there a way to get the contact information from the com_contact in an article override? – web-tiki Apr 27 '18 at 12:50
  • On a side note, I know before hand the id of the user I need to show as it the same for everyone. An example of what I am trying to do is "display the address of a site owner in the footer under the title of an article" – web-tiki Apr 27 '18 at 12:53
1
$user = JFactory::getUser();
$db = JFactory::getDbo();
$query = $db->getQuery(true);
$query->select('*')->from('#__contact_details')->where('user_id = '.(int)$user->id);
$db->setQuery($query);
$contacts = $db->loadObjectList(); 
/* one user can be associated with multiple contacts */
if($contacts) {
    /* do stuff with the contact records */
}
  • Good edit mickmackusa ! – Michael Nov 8 '18 at 20:56

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.