【问题标题】:Load frontend module in Joomla Backend在 Joomla 后端加载前端模块
【发布时间】:2014-01-06 15:12:01
【问题描述】:

我已经为我的 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。

有人处理过这个吗?如果是这样:如何???

提前致谢,祝你圣诞快乐 :)

【问题讨论】:

    标签: php joomla joomla2.5


    【解决方案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">
    

    并像通常的前端扩展一样安装/发现模块。然后您可以更改位置等并进行必要的更改以显示您希望它们呈现的视图。

    【讨论】:

      【解决方案2】:

      问题是它正在寻找与它所在的应用程序关联的模块,特别是代码正在查看管理模块文件夹,而您希望它正在查看站点模块文件夹。这是两个独立的应用程序。

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

      显然,最简单的事情就是做核心所做的事情,并在每个应用程序中提供相同的模块。大多数模块通常都很小,几乎没有比解决这个需要重新考虑 JModuleHelper 的问题更多的代码。

      【讨论】:

        【解决方案3】:

        正如 Elin 所说,JModuleHelper 只能在您在后端调用它时将模块加载到后端。在 Elin 的链接之后,您将找到源代码 JModuleHelper 和从数据库读取模块信息的实际 load() 函数。 (警告:这条线将来可能会改变。)这是我的应用程序的“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;
        }
        

        用例:

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

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2014-11-21
          • 2015-07-21
          • 2014-07-06
          • 1970-01-01
          • 2013-12-24
          • 1970-01-01
          • 2018-06-27
          • 1970-01-01
          相关资源
          最近更新 更多