7

I have a template, where would like to print the content of an article, what is working quite nice so far, using this code:

JModelLegacy::addIncludePath(JPATH_SITE . '/components/com_content/models');
$model = JModelLegacy::getInstance('Article', 'ContentModel');
$article = $model->getItem($aid);
$terms = empty($article->fulltext) ? $article->introtext : $article->fulltext;

So basically, I have the article's id and load it, BUT that throws an error if the current language differs from the language of the article. Unfortunately, I cannot »translate« the article ID, so I need to find a way, to get the right article based on the current language.

Fortunately, there are the associations I can use for that Job (image below).

associations view

So now my Question:

How can I load the associated Article?

n.b.: I already had a look at the database, and I found the associations table, but I just hope that there is already some code I could use!

Thanks in Ahead!

2 Answers 2

7

You can use JLanguageAssociations::getAssociations. This function will return an array containing associated items. You can also check if if the language filter Items Associations parameter is enabled using JLanguageAssociations::isEnabled();.

Here's an example from ROOT/administrator/components/com_content/models/article.php:

// Load associated content items
$app = JFactory::getApplication();
$assoc = JLanguageAssociations::isEnabled();

if ($assoc)
{
    $item->associations = array();

    if ($item->id != null)
    {
        $associations = JLanguageAssociations::getAssociations('com_content', '#__content', 'com_content.item', $item->id);

        foreach ($associations as $tag => $association)
        {
            $item->associations[$tag] = $association->id;
        }
    }
}

return $item;

Resources:

1
  • 1
    Great, I'm glad it worked out :)
    – johanpw
    Jan 16, 2015 at 14:37
1

components/com_content/helpers/association.php

has a class ContentHelperAssociation and function getAssociations($id = 0, $view = null) you could probably use.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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