在Joomla 3.x中从组件中获取硬编码值


Get hard-coded values from component into plugin in Joomla 3.x

我有一个自定义组件,实际上有几个。每个/view/default.php

的开头和结尾都有原始和硬编码的html。

我有一个系统插件,需要得到这个html,并在某些情况下将其更改为其他东西,可以在后端管理。作为一个内容插件,这适用于所有com_content文章,但它被忽略的组件,我的理解是系统插件可以做到这一点,但我不能得到的数据进入插件,并返回它

组件文本的例子($text1, $text2被定义在文档的顶部)

  JPluginHelper::importPlugin( 'system' );
  JPluginHelper::importPlugin('plgSystemMyplugin'); 
  $dispatcher =& JDispatcher::getInstance();
  $data = array($text1, $text2);   // any number of arguments you want
  $data = $dispatcher->trigger('onBeforeRender', $data);
    <article>
<div class="spacer" style="height:25px;"></div>
<div class="page_title_text">
    <h1>title</h1>
     <?php  var_dump($data);  ?>
</div>
<section>

我的插件:

      jimport( 'joomla.plugin.plugin' );
   class plgSystemMyplugin extends JPlugin {
function onBeforeRender() {
    if (JFactory::getDocument()->getType() != 'html') {
            return;
    }
    else {
    $document=JFactory::getDocument();
    $document->addCustomTag('<!-- System Plugin has been included (for testing) -->');          
    $document=JResponse::getBody();
    $bob=JResponse::getBody();

        $db = &JFactory::getDbo();
        $db->setQuery('SELECT 1, 2 FROM #__table');
        $results = $db->loadRowList();
        $numrows=count($results);
                if($numrows >0) {
                                foreach($results as $regexes) {
                                $document  = str_replace($regexes[0],$regexes[1],$document);
                                }
                                return $document;
                }
                else  {
                    $document = 'error with plugin';
                }
    JResponse::setBody($document);
    return $document;
    }
   }
   }

目前$data返回一个数组,键为1,值(字符串)为" (blank/empty)。

而不是我所期望的数据库中的数据。

简单来说我的文件和数据库中有{sometext}它应该返回<p>my other text</p>

你能帮忙吗?

谢谢

好的。再深入研究一下,有几个问题会跳出来。最大的是,你保存getBody到一个名为$bob的变量,但然后切换到使用$document,这是上面的对象形式,而不是内容。

同样,你有一个return $document挂在代码中间,阻止你看到你要设置$document作为新的主体。可能应该更像下面这样:

    $bob=JResponse::getBody();

    $db = &JFactory::getDbo();
    $db->setQuery('SELECT 1, 2 FROM #__table');
    $results = $db->loadRowList();
    $numrows=count($results);
    if($numrows >0) {
        foreach($results as $regexes) {
            $bob  = str_replace($regexes[0],$regexes[1],$bob);
        }
    }
    else  {
        $bob = 'error with plugin';
    }
    JResponse::setBody($bob);
    return $document;
}

原始想法:

有两个想法让你开始。我不确定这是否能完全回答这个问题,但应该能让你朝着正确的方向前进。

首先,你不应该触发系统插件。他们是系统插件,所以系统会为你照顾。如果你想在你的组件中使用内容插件(你绝对可以这么做!),那么你必须像你的第一组代码一样触发它们。在这种情况下,不要为整个分派部分而烦恼。

第二,你的插件看起来已经设置好了,可以正确地从JDocument中抓取正文,所以应该可以工作。

可能的问题是整个系统插件没有被触发。确保安装了它,并且所有内容都正确命名。它必须在plugins/system/myplugin/myplugin.php基于这个名字,并确保这个xml文件也引用myplugin作为插件名。如果没有,系统将找不到类,但可能不会抛出错误。它会跳过它。这每次都给我带来麻烦。

做一些检查只是为了确保它被调用,我通常抛出一个echovar_dump靠近文件的顶部,就在函数内部。确认函数至少是首先被调用的,并且您应该知道让它工作的大部分方法。