3

現在のページに割り当てられていないモジュールのパラメータを取得したい。ヘルパー クラス JModuleHelper には getModule メソッドがありますが、http://docs.joomla.org/JModuleHelper/getModule で定義されているように、モジュールを現在のメニュー項目またはすべてのメニュー項目に割り当てる必要があります。

もちろん、データベースにアクセスしてパラメーターを自分で引き出すこともできますが、もっときちんとした Joomla の方法でそれをやりたかったのです。

私がしようとしているのはライブラリを作成することですが、ライブラリにはいくつかのパラメータ化と独自のデータベース テーブル (ライブラリではできない 2 つのこと) が必要なので、モジュールを使用してギャップを埋めようとしています。ライブラリ コード (サイトの任意のページから呼び出すことができます) からモジュール パラメーターにアクセスする必要があります。

助けてくれてありがとう


私が行った解決策は、次のコードをライブラリに追加することです。

$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 のソリューションをお勧めします。どちらも機能し、おそらくどちらかを選択することはあまりありません

4

1 に答える 1

3

これを回避するには、現在のドキュメントを取得してmoduleレンダラーをロードします。たとえば、「Sport News」というタイトルでセットアップした最新のニュース モジュールが必要な場合は、次のように使用できます。

$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;
    }
}
于 2013-05-04T02:15:24.027 に答える