web-dev-qa-db-ja.com

同じコンテンツを表示する2つの異なるモジュール

Joomla 3.3.3のmod_article_categoryモジュールを複製して、IDでフィルターされた1つの記事のみを表示しました(ドロップダウンで選択できます)。

私のメインコード:

defined('_JEXEC') or die;
require_once __DIR__ . '/helper.php';
$article = ModArticleHelper::getArticle($params)[0];
require JModuleHelper::getLayoutPath('mod_article', $params->get('layout', 'default'));

そして私のテンプレートコード:

<?php defined('_JEXEC') or die;
$show_title = $params->get('show_title',true);
$h = $params->get('h','h3');
$class = htmlspecialchars($params->get('moduleclass_sfx'));
?>
<article class="<?= $class ?>">
    <?php if($show_title){echo "<$h>$article->title</$h>";} ?>
    <?= $article->introtext.$article->fulltext ?>   
</article>

このモジュールを使用して記事をモジュールの位置に表示すると、機能します。このモジュールを複数回使用すると問題が発生します。表示される記事は常に最後に追加されたモジュールの記事であり、すべてのモジュール固有のパラメーター(記事のタイトルの表示など)も最後に追加されたモジュールのみをリッスンします。サイト全体のモジュール設定(モジュールタイトルの表示など)は変更します。

複数の位置にあるときにモジュールが機能しないのはなぜですか?

完全なコード(helper.php)

defined('_JEXEC') or die;
$com_path = JPATH_SITE . '/components/com_content/';
require_once $com_path . 'helpers/route.php';
JModelLegacy::addIncludePath($com_path . '/models', 'ContentModel');

abstract class ModArticleHelper{
    public static function getArticle(&$params){
        // Get an instance of the generic articles model
        $articles = JModelLegacy::getInstance('Articles', 'ContentModel', array('ignore_request' => true));

        // Set application parameters in model
        $app = JFactory::getApplication();
        $appParams = $app->getParams();
        $articles->setState('params', $appParams);
        $articles->setState('filter.published', 1);

        // Access filter
        $access = !JComponentHelper::getParams('com_content')->get('show_noauth');
        $authorised = JAccess::getAuthorisedViewLevels(JFactory::getUser()->get('id'));
        $articles->setState('filter.access', $access);

        // Item ID filter
        $articles->setState('filter.id', $params->get('article_id', array()));

        // Filter by language
        $articles->setState('filter.language', $app->getLanguageFilter());

        $items = $articles->getItems();

        foreach ($items as &$item){
            $item->slug = $item->id . ':' . $item->alias;
            $item->catslug = $item->catid . ':' . $item->category_alias;

            if ($access || in_array($item->access, $authorised)){
                // We know that user has the privilege to view the article
                $item->link = JRoute::_(ContentHelperRoute::getArticleRoute($item->slug, $item->catslug));
            } else {
                $item->link = JRoute::_('index.php?option=com_users&view=login');
            }
        }
        return $items;
    }
}
2
xaddict

コンポーネントヘルパーの代わりにネイティブのjoomlaデータベースコードを使用するために、ヘルパーコード全体(2分以内)をやり直しました。完全なhelper.phpコードは次のとおりです。

defined('_JEXEC') or die;

$com_path = JPATH_SITE . '/components/com_content/';
//require_once $com_path . 'router.php';
require_once $com_path . 'helpers/route.php';

abstract class ModArticleHelper{

    public static function getArticle(&$params){
        $app = JFactory::getApplication();
        $appParams = $app->getParams();

        $db = JFactory::getDbo();
        $query = $db->getQuery(true);
        $query->select('content.*,c.id as catid,c.alias as category_alias');
        $query->from('#__content as content, #__categories as c');
        $query->where('content.id = '.$params->get('article_id'));
        $query->where('content.state = 1');
        $query->where('content.catid = c.id');
        $db->setQuery($query);
        $items = $db->loadObjectList();

        foreach ($items as &$item){
            $item->slug = $item->id . ':' . $item->alias;
            $item->catslug = $item->catid . ':' . $item->category_alias;
            $item->link = JRoute::_(ContentHelperRoute::getArticleRoute($item->slug, $item->catslug));
        }
        return $items;
    }
}

そしてそれはうまくいきます!

2
xaddict

ContentModelArticlesには内部キャッシュがあります。

ContentModelArticles-> getItems()=> JModelList-> getItems()、155行目

    public function getItems()
    {
    // Get a storage key.
    $store = $this->getStoreId();

    // Try to load the data from internal storage.
    if (isset($this->cache[$store]))
    {
        return $this->cache[$store];
    }

キャッシュアクセスはStoreIdに基づいているため、モジュールごとに異なるコンテキストを定義する必要があります。例えば。 $ articles-> set( 'context'、 'mod_mymodule'。$ id);

1
Anibal