我为Joomla2.5网站的前端开发了几个模块,但它们在后端也很方便。有没有办法从后端加载前端模块(后端是指管理员界面)?
我已将以下代码输入到表单视图中。
$module = JModuleHelper::getModule('mod_name_of_module');
$moduleHtml = JModuleHelper::renderModule($module);
echo $moduleHtml;
但它没有给出任何东西。如果我使用print_r($ module),我会得到
stdClass Object ( [id] => 0 [title] => [module] => mod_name_of_module [position] => [content] => [showtitle] => 0 [control] => [params] => [user] => 0 [style] => none )
这基本上意味着它找不到模块,因为我在这种情况下尝试加载的模块的ID为136而不是0。
有人管理过吗?如果是这样:怎么样?
先谢谢你,祝圣诞快乐:)
答案 0 :(得分:1)
在* .xml配置文件中,您只需更改:
<extension type="module" version="3.1.0" client="site" method="upgrade">
到此:
<extension type="module" version="3.1.0" client="administrator" position="menu" method="upgrade">
并按正常方式安装/发现模块,如前端扩展。然后您可以更改位置等并进行必要的更改以显示您希望它们呈现的视图。
答案 1 :(得分:0)
问题是它正在寻找与其所在应用程序相关联的模块,特别是代码正在查看admin modules文件夹,并且您希望它查看站点模块文件夹。这是两个独立的应用程序。
https://github.com/joomla/joomla-cms/blob/master/libraries/cms/module/helper.php#L347
最简单的事情显然是做核心所做的事情,并在每个应用程序中提供相同的模块。模块通常在很大程度上是如此之小,几乎没有比解决这个需要重新考虑JModuleHelper的问题更多的代码。
答案 2 :(得分:0)
正如Elin所说,JModuleHelper只能在后端调用后端加载模块。按照Elin的链接,您将找到源代码JModuleHelper以及从DB读取模块信息的实际load()
函数。 (警告:这条线可能在将来发生变化。)这是我的&#34; hack&#34;对于我的应用程序(在Joomla!3.1.5中测试):
function getModule($moduleName, $instanceTitle = null){
$db = JFactory::getDbo();
$query = $db->getQuery(true)
->select('m.id, m.title, m.module, m.position, m.content, m.showtitle, m.params')
->from('#__modules AS m')
->where(Array(
'm.published = 1'
, 'm.module = ' . $db->quote($moduleName)
));
if ($instanceTitle){
$query->where('m.title = ' . $db->quote($instanceTitle));
}
$db->setQuery($query);
try
{
$modules = $db->loadObject(); // You might want to use loadObjectList() instead
}
catch (RuntimeException $e)
{
JLog::add(JText::sprintf('JLIB_APPLICATION_ERROR_MODULE_LOAD', $e->getMessage()), JLog::WARNING, 'jerror');
$clean = array();
return $clean;
}
return $modules;
}
用例:
$module = getModule('mod_your_module', 'The name of the module instance');
$params = new JRegistry;
$params->loadString($module->params);
require_once JPATH_SITE . '/modules/mod_your_module/helper.php'; // I have a helper class to format the params before render. So I reuse this helper here.
$params = ModYourModuleHelper::getParams($params);