jalbasri
jalbasri

Reputation: 338

onContentBeforeSave and onContentAfterSave not triggering

I know there are many questions here with the same problem but I have been through the solutions for all the ones I have found and have not been able to fix my problem.

Both the onContentBeforeSave and onContentAfterSave events do not trigger when saving articles from the frontend and the backend. The onContentBeforeDisplay event in the same plugin works fine.

here is my code, do you have any ideas? Thank you.

    <?php
// no direct access
defined( '_JEXEC' ) or die( 'Restricted access' );
jimport('joomla.plugin.plugin');

class plgContentBillrepeat extends JPlugin {
        /**
         * Load the language file on instantiation. Note this is only available in    Joomla 3.1 and higher.
     * If you want to support 3.0 series you must override the constructor
     *
     * @var    boolean
     * @since  3.1
     */
    protected $autoloadLanguage = true;

    /**
     * Plugin method with the same name as the event will be called automatically.
     */

    function onContentBeforeSave($context, &$article, $isNew) {
            JError::raiseNotice( 100, 'onContentBeforeSave plugin fired!' );
            return true;
    }

    function onContentAfterSave($context, &$article, $isNew) {
            JError::raiseNotice( 100, 'onContentAfterSave plugin fired!' );
    }

    function onContentBeforeDisplay($context, &$article, &$params, $limit=0) {
            JError::raiseNotice( 100, 'onContentBeforeDisplay plugin fired!' );
            return "";
    }
}
?>

Upvotes: 2

Views: 1496

Answers (2)

Wizard
Wizard

Reputation: 3151

According to https://docs.joomla.org/Potential_backward_compatibility_issues_in_Joomla_3.0_and_Joomla_Platform_12.1#Plugin_Events

Plugin Events

onContentBeforeSave event now receives $article by value not by reference. Sample definition: public function onContentBeforeSave($context, $article, $isNew).

onContentAfterSave event now receives $article by value not by reference. Sample definition: public function onContentAfterSave($context, $article, $isNew).

Upvotes: 1

jalbasri
jalbasri

Reputation: 338

Solved the problem by passing the article by value and not by reference.

This seems to work:

    function onContentBeforeSave($context, $article, $isNew) {
            JError::raiseNotice( 100, 'onContentBeforeSave plugin fired!' );
            return true;
    }

    function onContentAfterSave($context, $article, $isNew) {
            JError::raiseNotice( 100, 'onContentAfterSave plugin fired!' );
            return true;
    }

Although I'm not too sure why because all the documentation clearly indicate article should be passed by reference and even questions I've seen here have indicated that passing by value was causing the event not too be triggered.

Upvotes: 3

Related Questions