1

I need to get a com_contacts item the same way Joomla does (so I can re-use a template for example). I know I can get the item by querying the database directly, but that won't prepare things the same way Joomla das in com_content.

I can get contacts by category id fine with:

require_once JPATH_SITE . '/components/com_contact/helpers/route.php';
JLoader::import('joomla.application.component.model');
JModelLegacy::addIncludePath(JPATH_SITE . '/components/com_contact/models', 'ContentModel');
$categoryModel = JModelLegacy::getInstance('Category', 'ContactModel', array('ignore_request' => true));
$categoryModel->setState('category.id', 53);
$categoryModel->setState('list.ordering', 'a.name');
$categoryModel->setState('list.direction', 'asc');
$categoryModel->setState('filter.published', 1);
$contacts = $categoryModel->getItems();
echo '<pre><tt>';print_r($contacts[0]);

How would I get a contact item by it's item id?

TIA!

  • To make it somewhat clearer: "same way Joomla does" meaning also including params, attribs, jcfields (custom fields) properties for example. – Marcel Jul 25 '18 at 15:02
1

To get single contact by ID, use Contact model:

JModelLegacy::addIncludePath(JPATH_SITE . '/components/com_contact/models', 'ContactModel');
$model = JModelLegacy::getInstance('Contact', 'ContactModel', array('ignore_request' => true));
$model->setState('contact.id', $contactId);
$model->setState('params', $params);
$contact = $model->getItem();

Where $contactId is the contact's ID and $params is the params. Params must be a Registry object. You can create a new Registry object or use app/component params, depending on your needs:

use Joomla\Registry\Registry;
$params = new Registry;

or

$params = JFactory::getApplication()->getParams();

or

use Joomla\CMS\Component\ComponentHelper;
$params = ComponentHelper::getParams('com_contact');
| improve this answer | |
  • Thank you very much for your fast answer. Still, I get an error: __clone method called on non-object in /htdocs/components/com_contact/models/contact.php on line 245 – Marcel Jul 25 '18 at 16:05
  • Ah, I see. $model->setState('params', $params); seems to be required. – Marcel Jul 25 '18 at 16:19
  • Had to add tags like this: $tags = new JHelperTags; $tags->getItemTags('com_contact.contact', $contactId); – Marcel Jul 26 '18 at 16:18

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.