1

We have linked the author to com_contacts and now I want to show an author image which will be a custom field in com_contacts.

I was able to show it as a com_user custom field using the following code:

$fields = FieldsHelper::getFields('com_users.user', JFactory::getUser($author), true);

foreach($fields as $field) {
    $fields[$field->name] = $field;
}

$profilePicture = $fields['profilepicture50x50']->value;
echo $profilePicture;

But this does not help me if I want to use com_contacts. I tried changing it to

$fields = FieldsHelper::getFields('com_contacts.contact', JFactory::getUser($author), true);

But when I look in the API I do not see an API helper in that way. I can get the author ID from the current page.

By using $author = $displayData['item']->created_by; or by using $displayData['contact_link'] to find the entire com_contact URL

Can anyone direct me to the correct API page or help me to understand how I could call such a field?

2

Your approach will still work, but you just need to pass a contact object as the second parameter to your call

$fields = FieldsHelper::getFields(...);

Unfortunately afaik there is no API around contact the way there is around user, so it's a bit more complex to get it.

I got it by doing a database query (as in Selecting data using JDatabase) to find the #__contact_details table id (which I set to $contactId) where created_by = user_id in the contact_details table. Then I used JTable to get the contact object:

JTable::addIncludePath(JPATH_ADMINISTRATOR . '/components/com_contact/tables');
$contact = JTable::getInstance('Contact', 'ContactTable', array());
if ($contact->load($contactId))
{
    JLoader::register('FieldsHelper', JPATH_ADMINISTRATOR . '/components/com_fields/helpers/fields.php');
    $fields = FieldsHelper::getFields('com_contact.contact', $contact, true);
}

Then your code works as before.

I guess you have specific reasons for using Contact rather than User, but you obviously have to be careful as you can assign the same User to 2 or more Contact records.

(Once you get the contact id you can get the contact object using the contact model instead, as described in Get com_contacts item by id?.)

| 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.