从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

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.