dongluojiao6322 2013-06-20 09:33
浏览 31

从Joomla 3.x中获取组件中的硬编码值到插件中

I have a custom component, in fact several. Each will have raw and hard-coded html at the beginning and end of their /view/default.php

I have a system plugin that needs to get this html and in some cases change it to something else, that can be managed in the back end. As a content plugin this works fine on all com_content articles, but it is ignored by components, my understanding is system plugins can do this but i can't get the data into the plugin and return it

example of component text ($text1, $text2 are defined at the top of the document)

  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>

my plugin:

      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;
    }
   }
   }

at the moment $data returns an array with a key 1 and value (string) of "" (blank/empty).

but not the data from the database I am expecting.

in simple terms I have {sometext} in my file and my database and it should return <p>my other text</p>

can you help?

thanks

  • 写回答

1条回答 默认 最新

  • dongpiao9078 2013-06-20 17:20
    关注

    Ok. Well looking at this deeper there is a couple of issues that jump out. The biggest being that you save getBody into a variable named $bob but then switch everywhere to using $document which is the object form above, not the content.

    Also, you had a return $document hanging out in the middle of the code that prevented you from seeing that you were going to set $document as the new body. Probably should be more like below:

        $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;
    }
    

    Original Thoughts:

    Two thoughts to get you started. I'm not sure that this will actually fully answer the question, but should get you moving in the right direction.

    First, you should not have to trigger the system plugin. They are system plugins, so the system will take care of that for you. If you wanted to use content plugins in your component (which you can definitely do!) then you would have to trigger them like your first set of code. In this case, don't bother with the entire dispatch section.

    Second, your plugin looks set up to grab the body from the JDocument correctly, so that should work.

    The likely issue is that the entire system plugin is just not being triggered. Make sure that it is installed and everything is named correctly. It has to be at plugins/system/myplugin/myplugin.php based on this name and make sure that the xml file with this also references myplugin as the plugin name. If not, the system won't find the class but likely won't throw an error. It will just skip it. This gives me trouble every time.

    To do some checking just to make sure it gets called, I usually throw an echo or var_dump near the top of the file and just inside the function. Confirm that the function is at least getting called first and you should be most of the way to getting this to work.

    评论

报告相同问题?

悬赏问题

  • ¥15 ogg dd trandata 报错
  • ¥15 高缺失率数据如何选择填充方式
  • ¥50 potsgresql15备份问题
  • ¥15 Mac系统vs code使用phpstudy如何配置debug来调试php
  • ¥15 目前主流的音乐软件,像网易云音乐,QQ音乐他们的前端和后台部分是用的什么技术实现的?求解!
  • ¥60 pb数据库修改与连接
  • ¥15 spss统计中二分类变量和有序变量的相关性分析可以用kendall相关分析吗?
  • ¥15 拟通过pc下指令到安卓系统,如果追求响应速度,尽可能无延迟,是不是用安卓模拟器会优于实体的安卓手机?如果是,可以快多少毫秒?
  • ¥20 神经网络Sequential name=sequential, built=False
  • ¥16 Qphython 用xlrd读取excel报错