要解决这个问题,您可以获取当前文档,然后加载module
渲染器。例如,如果我想要我设置的最新新闻模块,标题为“体育新闻”,我可以使用:
$module = 'mod_articles_latest';
$title = 'Sport News';
$document = JFactory::getDocument();
$renderer = $document->loadRenderer('module');
$theModule = JModuleHelper::getModule($module, $title);
因此,要处理未将模块分配给当前或所有菜单项的情况,您可以创建自己的助手,该助手继承自并仅JModuleHelper
覆盖受保护的函数。_load()
class MyModuleHelper extends JModuleHelper
{
protected static function &_load()
{
static $clean;
if (isset($clean))
{
return $clean;
}
$Itemid = JRequest::getInt('Itemid');
$app = JFactory::getApplication();
$user = JFactory::getUser();
$groups = implode(',', $user->getAuthorisedViewLevels());
$lang = JFactory::getLanguage()->getTag();
$clientId = (int) $app->getClientId();
$cache = JFactory::getCache('com_modules', '');
$cacheid = md5(serialize(array($Itemid, $groups, $clientId, $lang)));
if (!($clean = $cache->get($cacheid)))
{
$db = JFactory::getDbo();
$query = $db->getQuery(true);
$query->select('m.id, m.title, m.module, m.position, m.content, m.showtitle, m.params, mm.menuid');
$query->from('#__modules AS m');
$query->join('LEFT', '#__modules_menu AS mm ON mm.moduleid = m.id');
$query->where('m.published = 1');
$query->join('LEFT', '#__extensions AS e ON e.element = m.module AND e.client_id = m.client_id');
$query->where('e.enabled = 1');
$date = JFactory::getDate();
$now = $date->toSql();
$nullDate = $db->getNullDate();
$query->where('(m.publish_up = ' . $db->Quote($nullDate) . ' OR m.publish_up <= ' . $db->Quote($now) . ')');
$query->where('(m.publish_down = ' . $db->Quote($nullDate) . ' OR m.publish_down >= ' . $db->Quote($now) . ')');
$query->where('m.access IN (' . $groups . ')');
$query->where('m.client_id = ' . $clientId);
// Disable this line in your override class's _load()...
// $query->where('(mm.menuid = ' . (int) $Itemid . ' OR mm.menuid <= 0)');
// Filter by language
if ($app->isSite() && $app->getLanguageFilter())
{
$query->where('m.language IN (' . $db->Quote($lang) . ',' . $db->Quote('*') . ')');
}
$query->order('m.position, m.ordering');
// Set the query
$db->setQuery($query);
$modules = $db->loadObjectList();
$clean = array();
if ($db->getErrorNum())
{
JError::raiseWarning(500, JText::sprintf('JLIB_APPLICATION_ERROR_MODULE_LOAD', $db->getErrorMsg()));
return $clean;
}
// Apply negative selections and eliminate duplicates
$negId = $Itemid ? -(int) $Itemid : false;
$dupes = array();
for ($i = 0, $n = count($modules); $i < $n; $i++)
{
$module = &$modules[$i];
// The module is excluded if there is an explicit prohibition
$negHit = ($negId === (int) $module->menuid);
if (isset($dupes[$module->id]))
{
// If this item has been excluded, keep the duplicate flag set,
// but remove any item from the cleaned array.
if ($negHit)
{
unset($clean[$module->id]);
}
continue;
}
$dupes[$module->id] = true;
// Only accept modules without explicit exclusions.
if (!$negHit)
{
// Determine if this is a 1.0 style custom module (no mod_ prefix)
// This should be eliminated when the class is refactored.
// $module->user is deprecated.
$file = $module->module;
$custom = substr($file, 0, 4) == 'mod_' ? 0 : 1;
$module->user = $custom;
// 1.0 style custom module name is given by the title field, otherwise strip off "mod_"
$module->name = $custom ? $module->module : substr($file, 4);
$module->style = null;
$module->position = strtolower($module- >position);
$clean[$module->id] = $module;
}
}
unset($dupes);
// Return to simple indexing that matches the query order.
$clean = array_values($clean);
$cache->store($clean, $cacheid);
}
return $clean;
}
}