简体   繁体   English

在Joomla后端中加载前端模块

[英]Load frontend module in Joomla Backend

I have developed several modules for the frontend of my Joomla2.5 site, but they could also be handy in the backend. 我已经为我的Joomla2.5网站的前端开发了几个模块,但是在后端它们也可以派上用场。 Is there any way to load front-end modules from the backend (by backend I mean the administrator interface)? 有什么办法可以从后端加载前端模块(所谓后端就是管理员界面)?

I have entered the following code into a form view. 我已将以下代码输入到表单视图中。

$module = JModuleHelper::getModule('mod_name_of_module');
$moduleHtml = JModuleHelper::renderModule($module);
echo $moduleHtml;

But it does not give anything. 但这没有任何作用。 If I use print_r($module) I get 如果我使用print_r($ module)我得到

stdClass Object ( [id] => 0 [title] => [module] => mod_name_of_module [position] => [content] => [showtitle] => 0 [control] => [params] => [user] => 0 [style] => none )

Which basically means that it doesn't find the module since the module I'm trying to load in this case has an ID of 136 and not 0. 这基本上意味着它找不到模块,因为在这种情况下我要加载的模块的ID为136而不是0。

Has anyone managed this? 有人管理过这个吗? If so: HOW??? 如果是这样:如何?

Thanks in advance and merry xmas :) 在此先感谢和圣诞快乐:)

In your *.xml config file you could just simply change: 在您的* .xml配置文件中,您只需更改即可:

<extension type="module" version="3.1.0" client="site" method="upgrade">

to this: 对此:

<extension type="module" version="3.1.0" client="administrator" position="menu" method="upgrade">

and install/discover the module as you would normally as for a frontend extension. 并像通常使用前端扩展一样安装/发现模块。 Then you can change the position etc and make needed changes to display your view as you would like them to render. 然后,您可以更改位置等,并进行所需的更改以显示您想要的视图。

The problem is that it is looking for the modules associated with the application it is in, specifically the code is looking at the admin modules folder and you want it to be looking at the site modules folder. 问题在于,它正在查找与其所在的应用程序关联的模块,特别是代码正在查找admin modules文件夹,而您希望它正在查看站点模块文件夹。 These are two stand alone applications. 这是两个独立的应用程序。

https://github.com/joomla/joomla-cms/blob/master/libraries/cms/module/helper.php#L347 https://github.com/joomla/joomla-cms/blob/master/libraries/cms/module/helper.php#L347

The easiest thing is obviously to do what the core has done and essentially provide the same modules in each of the applications. 最简单的事情显然是完成核心工作,并在每个应用程序中实质上提供相同的模块。 Modules are generally so small for the most part, that is barely going to be more code than solving this problem which requires a re-think of JModuleHelper. 通常,模块在大多数情况下是如此之小,几乎比解决这个问题需要更多的代码,而这需要重新考虑JModuleHelper。

As what Elin said, the JModuleHelper can only load the module in the backend as you invoked it in the backend. 正如Elin所说,JModuleHelper只能在后端调用模块时将其加载到后端。 Following Elin's link, you will find the source code JModuleHelper and the actual load() function that reads the module's info from the DB. 通过Elin的链接,您将找到源代码JModuleHelper和实际的load()函数,该函数从DB中读取模块的信息。 (Warning: the line might change in the future.) Here is my "hack" for my app (tested in Joomla!3.1.5): (警告:此行将来可能会更改。)这是我对应用程序的“ hack”(在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;
}

Use case: 用例:

$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);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM