I've replicated the mod_article_category module from Joomla 3.3.3 to only show one article, filtered by id (which you can choose in a dropdown).
My main code:
defined('_JEXEC') or die;
require_once __DIR__ . '/helper.php';
$article = ModArticleHelper::getArticle($params)[0];
require JModuleHelper::getLayoutPath('mod_article', $params->get('layout', 'default'));
And my template code:
<?php defined('_JEXEC') or die;
$show_title = $params->get('show_title',true);
$h = $params->get('h','h3');
$class = htmlspecialchars($params->get('moduleclass_sfx'));
?>
<article class="<?= $class ?>">
<?php if($show_title){echo "<$h>$article->title</$h>";} ?>
<?= $article->introtext.$article->fulltext ?>
</article>
If I use this module to show an article in a module position it works. The problem arises when I have this module multiple times. The shown article is always that of the last added module and all the module-specific params (like showing the title of the article) also only listen to the last added module. The sitewide module settings (like showing the module title) DO change.
Why doesn't the module work when in multiple positions?
FULL CODE (helper.php)
defined('_JEXEC') or die;
$com_path = JPATH_SITE . '/components/com_content/';
require_once $com_path . 'helpers/route.php';
JModelLegacy::addIncludePath($com_path . '/models', 'ContentModel');
abstract class ModArticleHelper{
public static function getArticle(&$params){
// Get an instance of the generic articles model
$articles = JModelLegacy::getInstance('Articles', 'ContentModel', array('ignore_request' => true));
// Set application parameters in model
$app = JFactory::getApplication();
$appParams = $app->getParams();
$articles->setState('params', $appParams);
$articles->setState('filter.published', 1);
// Access filter
$access = !JComponentHelper::getParams('com_content')->get('show_noauth');
$authorised = JAccess::getAuthorisedViewLevels(JFactory::getUser()->get('id'));
$articles->setState('filter.access', $access);
// Item ID filter
$articles->setState('filter.id', $params->get('article_id', array()));
// Filter by language
$articles->setState('filter.language', $app->getLanguageFilter());
$items = $articles->getItems();
foreach ($items as &$item){
$item->slug = $item->id . ':' . $item->alias;
$item->catslug = $item->catid . ':' . $item->category_alias;
if ($access || in_array($item->access, $authorised)){
// We know that user has the privilege to view the article
$item->link = JRoute::_(ContentHelperRoute::getArticleRoute($item->slug, $item->catslug));
} else {
$item->link = JRoute::_('index.php?option=com_users&view=login');
}
}
return $items;
}
}