5

I have some code on PHP something like:

$oldstring = ...;
...
$pattern = ...;
$newstring = preg_replace_callback($pattern, ... {...}, $oldstring);

echo $newstring;

I need to apply this code (maybe as class or somehow) to content of component and each existing module on the page, something like:

$doc = JFactory::getDocument();

$oldcomponent = $doc->getBuffer('component', '', array('class'=> ''));
// the same code for applying
echo $newcomponent;

$oldmodule1 = $doc->getBuffer('modules', 'position-1', array('style'=> ''));
// the same code for applying
echo $newmodule1;

$oldmodule2 = $doc->getBuffer('modules', 'position-2', array('style'=> ''));
// the same code for applying
echo $newmodule2;

...

$oldmoduleN = $doc->getBuffer('modules', 'position-N', array('style'=> ''));
// the same code for applying
echo $newmoduleN;

The second code is only something like what I mean. I.e. as old strings should be component and each module and they should be echoed as new strings on its positions on the page.

Can someone write example how to create simple Joomla plugin for this purpose. Thanks

3

There are a variety of ways on how to modify Joomla content with a plugin:

  • A component may call the plugin mechanism, for example com_content uses the "onContentPrepare" event. You can use this to modify com_content content. The plugins for this are in plugins/content, but that is just common behaviour, the plugin would work in any folder.

  • A system plugin can take the output of any component and modify it, for example with regular expressions.

  • A system plugin can also modify the whole HTML

  • A module calls the "onRenderModule" event, so you can use this to modify the module output.

So you need to create a plugin for components and one for modules and you can share your code, for example by installing a library.

A more detailed example:

So putting it all together:

public function onRenderModule($module, $attribs)
{
    $modulePosition = $attribs['name']
    $moduleStyle = $attribs['style']

    $module->content = str_replace('foo','bar',$module->content;

}

This would replace all occurences of foo with bar in any module.

  • I find out all this stuff by using a debugger. – jdog Sep 17 '15 at 19:11
1

Ok, I've found out how to modify component and any module with a system plugin:

class PlgSystemSomename extends JPlugin
{
    public function onAfterDispatch()
    {
        $doc = JFactory::getDocument();
        $oldComponent = $doc->getBuffer('component');
        $db = JFactory::getDBO();
        $query = $db->getQuery(true);
        $query->select($db->quoteName('position'))
            ->from($db->quoteName('#__modules'))
            ->where($db->quoteName('published')." = 1 AND ".$db->quoteName('client_id')." = 0");
        $db->setQuery($query);
        $list = $db->loadObjectList();
        ...
        $pattern = ...;
        $newComponent = preg_replace_callback($pattern, ... {...}, $oldComponent);
        if(count($list) > 0) {
            foreach($list as $module) {
                $newModule = preg_replace_callback($pattern, ... {...}, $doc->getBuffer('modules', $module->position, array('style'=> 'xhtml')));
                $doc->setBuffer($newModule, 'modules', $module->position, array('style'=> 'xhtml'));
            }
        }

        $doc->setBuffer($newComponent, 'component');
        return true;
    }
}
  • That only works if you know the chrome of the module you want to replace! – jdog Sep 20 '15 at 23:05
  • Now you are deleting whatever chrome has been setup in the template – jdog Sep 22 '15 at 19:10
  • I suppose you could load all templates from the database, make a list of functions, load all modules.php, compare the list of functions, inspect modules.php for include() and load that as well.....but why would you now use my method? – jdog Sep 22 '15 at 20:29
  • If you mean onRenderModule, it doesn't work, as I was mentioning in the past. Yeah, maybe I use it incorrectly but you still have not explained how to use it – stckvrw Sep 22 '15 at 20:51
  • Have you installed a debugger since? – jdog Sep 22 '15 at 21:46

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.