Christian
Christian

Reputation: 28144

Joomla JError::raiseWarning() shows twice

I'm writing a Joomla plugin that looks more or less like:

<?php

    // no direct access
    defined( '_JEXEC' ) or die( 'Restricted access' );

    class plgSystemTest extends JPlugin {
        function onAfterInitialise(){
            static $showWarnings = true;

            if($somecondition && $showWarnings)
                JError::raiseWarning(100,'Some warning that shows up twice.');

            $showWarnings = false;
        }
    }

?>

I'm testing this plugin on Joomla 1.5, 1.6 and 1.7. While the warning does show up in each of them, it shows up twice the second time the page loads.

By the way, it's also worth noting that the $showWarnings flag doesn't help at all.

enter image description here

Notes

Upvotes: 0

Views: 1064

Answers (2)

Christian
Christian

Reputation: 28144

As @alghimo hinted, warnings are somehow stored in a session.

Unfortunately, I don't have time to waste investigating such strange issues in Joomla, so the technical details are still hazy.

The way I got it to work as I want is to not to show the message when $_GET['layout'] is edit:

if($_REQUEST['layout'] != 'edit'){
    JError::raiseWarning(100, 'Bla bla bla.');
}

That said, I don't feel this is the right answer to the issue. If anyone comes up with a better idea, I'll be more than happy to switch answers accordingly.

Upvotes: 0

alghimo
alghimo

Reputation: 2899

Have you checked that no other piece of code is trigerring JError::raiseWarning or the "onAfterInitialise" event?. That static var should work unless you have two different instances of the plugin.

You could try this:

class plgSystemTest extends JPlugin {
        function onAfterInitialise(){
            static $showWarnings;
            if ( !isset( $showWarnings ) ) {
                JError::raiseWarning(100,'Some warning that shows up twice.');
                $showWarnings = false;
            }
        }
    }
}

if it still doesn't work, try this:

class plgSystemTest extends JPlugin {
        protected static $showWarnings;

        function onAfterInitialise(){
            if ( !isset( self::$showWarnings ) ) {
                JError::raiseWarning(100,'Some warning that shows up twice.');
                $showWarnings = false;
            }
        }
    }
}

I hope it helped!

I'm editing the answer to answer your comment

First of all: sorry, I made a mistake on my code. In the second piece of code, you should change this:

$showWarnings = false

For this:

self::$showWarnings = false;

If it still isn't working, try this:

class plgSystemTest extends JPlugin {
    protected static $showWarnings = true;

    function onAfterInitialise(){
        static $call_count = null;
        if ( self::$showWarnings === true ) {
            if( !isset( $call_count ) ) {
                $call_count = 1;
            } else {
                $call_count++;
            }
            JError::raiseWarning(100,'Some warning that shows up twice. Total: ' . $call_count);
            self::$showWarnings = false;
        }
    }
}

Regards

Upvotes: 1

Related Questions