Reputation: 13
i would like to load an article into a components templates php code within the Joomla framework.
I can load modules in php, modules in articles, components in articles and and..but i never wanted to load an article into a components php.
Does anybody know of a code snippet for that?
Appreciate any help.
Upvotes: 1
Views: 3440
Reputation: 21
With Joomla .3.8.10 I got an Fatal error: __clone method called on non-object in .../components/com_content/models/article.php on line 164
Seems that the params-property is required in model:
use Joomla\Registry\Registry; // only for new Registry below
ModelLegacy::addIncludePath(JPATH_SITE.'/components/com_content/models', 'ContentModel');
$model=JModelLegacy::getInstance('Article', 'ContentModel', array('ignore_request'=>true));
// $params=JFactory::getApplication()->getParams();
// An empty registry object is just fine:
$params=new Registry;
$model->setState('params', $params); // params (even empty) is *required* for model
$article=$model->getItem((int) articleId);
Not sure if or in what context ModelLegacy::addIncludePath...
really is required. Does anyone have insight into this?
You might want to use the Articles (note plural) model with an id-filter, as it is able to fetch tags and associations also:
use Joomla\Registry\Registry; // for new Registry
$model=JModelLegacy::getInstance('Article', 'ContentModel', array('ignore_request'=>true));
$model->setState('params', new Registry);
$model->setState('filter.article_id', (int) $articleId ); // or use array of ints for multiple articles
$model->setState('load_tags', true); // not available for Article model
$model->setState('show_associations', true);
$articles=$model->getItems();
$article=$articles[0];
Some more filters
In case you want to fetch articles by other means than id (defaults first). From components/com_content/models/articles.php
true
to include false
to exclude given id(s)Ordering and limiting
)² Valid fields: id, title, alias, checked_out, checked_out_time, catid, category_title, state, access, access_level, created, created_by, ordering, featured, language, hits, publish_up, publish_down, images, urls, filter_tag. All fields checked against whitelist.
Upvotes: 2
Reputation: 1566
I would load the article model in your view like
JModelLegacy::addIncludePath(JPATH_SITE.'/components/com_content/models', 'ContentModel');
$model = JModelLegacy::getInstance('Article', 'ContentModel', array('ignore_request' => true));
$article = $model->getItem((int) $articleId);
Now you can access all the fields which are available in the article like $item->fulltext
or $item->introtext
. Have a look at the article view to check out all the fancy stuff it does with the article before it displays it.
Upvotes: 4