2013-05-03 68 views
3

我想獲取未分配給當前頁面的模塊的參數。幫助程序類JModuleHelper具有getModule方法,但在此定義:http://docs.joomla.org/JModuleHelper/getModule,模塊必須分配給當前菜單項或所有菜單項。獲取未分配給當前頁面的模塊的Joomla 2.5模塊參數

很明顯,我可以進入數據庫並自己提取參數,但我想用一個整潔的Joomla方式來完成。

我想要做的是編寫一個庫,但是庫需要一些參數化和它自己的數據庫表(這是庫不能做的兩件事),所以我試圖用一個模塊填補空白。我需要從庫代碼(可以從站點上的任何頁面調用)訪問模塊參數。

感謝所有幫助


我已經有解決的辦法是將下面的代碼添加到我的圖書館:

$db = JFactory::getDbo(); 
$query = $db->getQuery(true); 

$query->select('m.params') 
    ->from('#__modules AS m') 
    ->where('m.module=' . $db->quote('mod_my_module_name')); 
$db->setQuery($query); 
$module = $db->loadObject(); 

$module_params = new JRegistry(); 
$module_params->loadString($module->params); 

我還沒有加入這個作爲一個答案因爲我認爲應該有一個更好的解決方案,它不直接查詢核心數據庫。不過,我認爲以上是目前最好的解決方案。


更新

根據答案對的Joomla這個問題!開發人員小組我實施了一個庫/插件解決方案。插件可具有可利用代碼的任何地方被拉到參數:

$plugin = JPluginHelper::getPlugin('plugin_type', 'plugin_name'); 
$parameteres = new JRegistry($plugin->params); 
$param =$parameteres->get('parameter'); 

過度模塊插件的優點是,有給插件分配給所有項目的ID,這是他們與模塊沒有要求。

我越看越它,我越有信心,我沒有優雅的解決方案,我的原始問題,獲取模塊的參數未分配給當前項目ID。所以,如果你發現這個問題,因爲你想做類似的事情,那麼我會推薦我的解決方案或cppl的解決方案下面。兩者都可以工作,並且可能沒有太多選擇它們

+0

你可能想通過分配模塊的所有菜單項,但是把它放在不模板中存在的位置,以規避問題。然後getModule會找到它,我想。如果你有時需要顯示它,雖然這將是一個麻煩。 – 2013-05-04 21:15:22

回答

3

爲了解決這個問題,您可以獲取當前文檔,然後加載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; 
    } 
} 
+0

感謝您的嘗試,但您的代碼不起作用。另外我看不出它是如何工作的,因爲在任何時候你都沒有加載缺失的模塊,你只是加載渲染器。你是否缺少諸如$ renderer-> render($ module)的行? – Dom 2013-05-04 08:48:05

+0

我認爲我剛剛瞭解你想解決的問題,但那不是我的問題。您試圖解決在我嘗試訪問文檔時沒有模塊加載到文檔中的問題,但是我遇到的問題是我想要的模塊未分配給當前菜單項。我已經多次聲明瞭這一點JModuleHelper :: getModule無法檢索未分配給當前菜單項的模塊。 – Dom 2013-05-04 09:00:29

+0

嗯,是的,我錯過了,'JModuleHelper''受保護的'_load()'函數將只獲取**發佈**模塊分配給當前或所有菜單項。加載所有可能的模塊(所以網站有數百個,其中有一半是未放置或未發佈的)將不是非常有效 - 您可以製作自己的輔助類並覆蓋它。看到我更新的答案。 – Craig 2013-05-04 23:34:31