获取未分配给当前页面的模块的Joomla 2.5模块参数

获取未分配给当前页面的模块的Joomla 2.5模块参数,joomla,parameters,module,joomla2.5,joomla-extensions,Joomla,Parameters,Module,Joomla2.5,Joomla Extensions,我想获取未分配给当前页面的模块的参数。助手类JModuleHelper有一个getModule方法,但如此处所定义的:,必须将该模块分配给当前菜单项或所有菜单项 显然,我可以直接进入数据库,自己提取参数,但我想用一种更整洁的Joomla方式来做 我尝试编写一个库,但是库需要一些参数化和它自己的数据库表(这两件事库不能做),所以我尝试使用一个模块来填补空白。我需要从库代码中访问模块参数(可以从站点上的任何页面调用) 谢谢你的帮助 我采用的解决方案是将以下代码添加到我的库中: $db = JFac

我想获取未分配给当前页面的模块的参数。助手类JModuleHelper有一个getModule方法,但如此处所定义的:,必须将该模块分配给当前菜单项或所有菜单项

显然,我可以直接进入数据库,自己提取参数,但我想用一种更整洁的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解决方案。这两种方法都可以使用,而且可能没有太多的选择余地,您可以获取当前文档,然后加载
模块
渲染器。e、 g.如果我想要我设置的标题为“体育新闻”的最新新闻模块,我可以使用:

$module='mod_articles_latest';
$title=‘体育新闻’;
$document=JFactory::getDocument();
$renderer=$document->loadRenderer('module');
$theModule=JModuleHelper::getModule($module,$title);
因此,要处理模块未分配给当前或所有菜单项的情况,您可以创建自己的助手,该助手继承自
JModuleHelper
,并仅覆盖受保护的函数
\u load()

类MyModuleHelper扩展了JModuleHelper
{
受保护的静态功能和加载()
{
静态$clean;
如果(isset($clean))
{
退回$clean;
}
$Itemid=JRequest::getInt('Itemid');
$app=JFactory::getApplication();
$user=JFactory::getUser();
$groups=内爆(“,”,$user->getAuthorizedViewLevel());
$lang=JFactory::getLanguage()->getTag();
$clientId=(int)$app->getClientId();
$cache=JFactory::getCache('com_模块','');
$cacheid=md5(序列化(数组($Itemid,$groups,$clientId,$lang));
如果(!($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(“#u模块为m”);
$query->join('LEFT','#u模块u菜单作为mm ON mm.moduleid=m.id');
$query->where('m.published=1');
$query->join('LEFT','##作为e-ON-e.element=m.module和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)。’或m.publish_-up=‘.$db->Quote($now)。’);
$query->where('m.access IN('.$groups.'));
$query->where('m.client_id='。$clientId);
//在重写类的_load()中禁用此行。。。

//$query->where('(mm.menuid='(int)$Itemid.'或mm.menuid感谢您的尝试,但您的代码对我不起作用。我也看不出它是如何工作的,因为您在任何时候都没有加载缺少的模块,您只是在加载渲染器。您是否缺少一行,例如$renderer->render($module)?我想我刚刚理解了您试图解决的问题,但这不是我遇到的问题。您试图解决的问题是,在我试图访问文档时,没有模块加载到文档中,但我遇到的问题是,我想要获取的模块没有分配到当前菜单项。我已声明这是一个c现在有几次JModuleHelper::getModule无法检索未分配给当前菜单项的模块。是的,我确实错过了,
JModuleHelper
受保护的
\u load()
功能只会将已发布的模块分配给当前或所有菜单项。加载所有可能的模块不是很有效(因此站点有数百个,其中一半是未放置或未发布的)-您可能可以创建自己的helper类并超越它。请参阅我的更新答案。创建自己的子类似乎是解决问题的一个非常繁重的方法。我认为更好的解决方案是直接从数据库中获取模块数据。我知道这并不优雅,但我认为这是可用解决方案中最好的一个。我不确定我是否理解站在“重”的地方关于它,您所要做的就是将代码复制到一个帮助文件中,然后加载它。无论如何,至少SQL是为您准备的。您可能希望通过将模块分配给所有菜单项,但将其放置在模板中不存在的位置来规避此问题。我认为getModule会找到它。如果您需要显示它,请艾姆斯认为这会很麻烦。
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;
    }
}