问题
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.
回答1:
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.
回答2:
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?
Update
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
- filter.article_id: single int or array of ints
- filter.article_id.include: (bool) true|false
true
to includefalse
to exclude given id(s) - -> dito for category, author, author_alias
- filter.subcategories: (bool) false|true
- filter.max_category_levels: (int) 1
- filter.date_filtering: (string) off|range|relative
- filter.date_field: (string) created db table field²
- filter.start_date_range for filter.date_filtering==range
- filter.end_date_range for filter.date_filtering==range
- filter.relative_date (int) 0 number of days in the past from today, for filter.date_filtering==relative
- filter.tag (int) 0 tag id
- load_tags: (bool) true|false
- show_associations: (bool) false|true
Ordering and limiting
- list.ordering: (string) ordering db table field²
- list.direction: (string) ASC|DESC
- list.limit: (int)
- list.start: (int) 0 start at zero
)² 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.
来源:https://stackoverflow.com/questions/19765160/loading-an-article-into-a-components-template-in-joomla