3

I have the following plugin written:

defined( '_JEXEC' ) or die( 'Restricted access' );

jimport( 'joomla.plugin.plugin' );

class plgContentModifyArticle extends JPlugin
{
    function plgContentModifyArticle ( &$subject, $params )
    {
        parent::__construct( $subject, $params );
    }

    function onBeforeContentSave( &$article, $isNew )
    {

        echo "Can you see this?";

        global $mainframe;
        $newText = '<b>Testtext</b>';
        $user =& JFactory::getUser(); // get the user

        $article->fulltext.=$newText;
        $article->introtext.=$newText;

        return true;
    }
}

With the following xml-file:

<?xml version="1.0" encoding="utf-8"?>
<extension type="plugin" version="3.1.0" group="content">
    <name>Modifyarticle</name>
    <version>1.0.0</version>
    <description>Modify your articles</description>
    <files>
       <filename plugin="modifyarticle">modifyarticle.php</filename>
    </files>

    <install>

    </install>
</extension>

I've made all of this based on the following tutorial: https://docs.joomla.org/J3.x:Creating_a_content_plugin

Unfortunately, it doesn't add text to a new article. And I don't have any idea on how to debug this.

1 Answer 1

5

Your plugin appears to be written for a very old version of Joomla! The event in current versions is onContentBeforeSave. You are also missing $context argument. It's a good idea to check for context to make sure that you're modifying the correct content type (e.g. article).

public function onContentBeforeSave($context, $article, $isNew)
{
    if ($context !== 'com_content.article')
    {
        return true;
    }

    // Add your code here.

    return true;
}

Other pointers:

To avoid warnings in modern PHP versions rename constructor to __construct. If you only call the parent function inside, you can remove the constructor completely.

If your plugin supports only Joomla! 3.x and above, you don't need to use jimport().

Global $mainframe variable is no longer used in Joomla! You can declare $app class variable to use the application object.

You won't see echoed text here, unless you stop the script. Instead you might want to use application's enqueueMessage() method to display a message after saving completes.

Assigning JFactory::getUser() by reference generates a notice Only variables should be assigned by reference in modern PHP versions.

defined('_JEXEC') or die;

class plgContentModifyArticle extends JPlugin
{
    protected $app;

    public function onContentBeforeSave($context, $article, $isNew)
    {
        if ($context !== 'com_content.article')
        {
            return true;
        }

        $this->app->enqueueMessage('Can you see this?');

        $newText = '<b>Testtext</b>';

        // get the user
        $user = JFactory::getUser();

        // do stuff with user, otherwise above call is not needed

        $article->fulltext  .= $newText;
        $article->introtext .= $newText;

        return true;
    }
}
3
  • Thank you very much, this really helped me a lot. Especially the debugging. Where have you found all of this information? I was looking on the official Joomla-Website but I only found stuff about older versions. Commented Aug 8, 2019 at 7:27
  • 1
    The thing I use to debug code that you might find useful, and real developers might be spluttering their coffee over their keyboards reading this, is to follow your 'echo "sometext"' or 'echo print_r($variable)' with a 'die;' soon after and you will output your text or variable at that moment to a white screen and all processing stops. As for where do you find this information, in my experience it has been turning over a lot of rocks to find the odd gem of information. JSE is good, Joomla Docs could be a lot better, a couple of books and Google are where I have found enough to be dangerous.
    – Irata
    Commented Aug 9, 2019 at 1:43
  • This book from Jisse Reitsma, Programming Joomla! Plugins, is very useful and you can either buy it online or search for the .pdf that can be found.
    – Irata
    Commented Aug 9, 2019 at 7:08

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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