簡體   English   中英

在Joomla后端中加載前端模塊

[英]Load frontend module in Joomla Backend

我已經為我的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。

有人管理過這個嗎? 如果是這樣:如何?

在此先感謝和聖誕快樂:)

在您的* .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">

並像通常使用前端擴展一樣安裝/發現模塊。 然后,您可以更改位置等,並進行所需的更改以顯示您想要的視圖。

問題在於,它正在查找與其所在的應用程序關聯的模塊,特別是代碼正在查找admin modules文件夾,而您希望它正在查看站點模塊文件夾。 這是兩個獨立的應用程序。

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

最簡單的事情顯然是完成核心工作,並在每個應用程序中實質上提供相同的模塊。 通常,模塊在大多數情況下是如此之小,幾乎比解決這個問題需要更多的代碼,而這需要重新考慮JModuleHelper。

正如Elin所說,JModuleHelper只能在后端調用模塊時將其加載到后端。 通過Elin的鏈接,您將找到源代碼JModuleHelper和實際的load()函數,該函數從DB中讀取模塊的信息。 (警告:此行將來可能會更改。)這是我對應用程序的“ 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);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM