Calling JFactory::getDocument in a system plugin's onAfterInitialise method will break the document format

1,151 views
Skip to first unread message

Andrew Eddie

unread,
Apr 17, 2011, 11:29:53 PM4/17/11
to joomla-de...@googlegroups.com
While diagnosing a client's web site, I ran across this problem that
all Joomla developers need to be aware of.

The condition was that the RSS feeds were broken. Depending on the
browser, you either got an error or you were returned to the original
page. SEF and suffixes were turned on. Joomla had been upgraded to
the latest version.

Debugging involved tracing when the "format" variable was set so that
Joomla knew that the page was a feed. I found the best place to
intercept this was by putting:

echo debug_print_backtrace();

at the start of the JFactory::getDocument() method in
/libraries/joomla/factory.php

This revealed that a few, what I'll call "content" related, plugins
were firing when the "onAfterInitialise" event was triggered by
Joomla's event handling system. So I examined these and found the
root cause was where a plugin called JFactory::getDocument BEFORE the
request URL had been routed because it was triggered by the
"onAfterInitialise" event (routing is done only after this event is
fired). Only during the routing process does the ".feed" in a SEF URL
get transformed into "format=feed". Calling JFactory::getDocument
before this happens mean that JDocument will assume the default case -
that the request is for a regular HTML page. Since the factory method
returns a singleton, all subsequent calls to JFactory::getDocument
return the already initialised (to HTML) document.

The "hot fix" in my case was to simply rename the plugin function for
"onAfterInitialise" to "onAfterRoute". All feeds worked normally
after that.

SUMMARY:

When you are writing system plugins, never make a call to
JFactory::getDocument in any method that triggers before
"onAfterRoute", such as "onAfterInitialise". Doing so will cripple
any document format pages other than HTML (such as feeds, PDF's, et
al) when both SEF and Add Suffix are turned on in Global
Configuration.

Regards,
Andrew Eddie
http://learn.theartofjoomla.com - training videos for Joomla 1.6 developers

shumisha

unread,
Apr 18, 2011, 2:14:36 PM4/18/11
to Joomla! General Development
Hi Andrew,

Indeed, we have bumped into this problem a few times. It is documented
here also : http://www.en.joomgallery.net/faq/general-faq/problem-with-certain-system-plugins.html.
Pretty much since 2008, sh404SEF has had a setting to overcome this
issue that users can activate if they use one of those plugins. The
solution is a bit different, it consists in simply leaving the format
variable out of the SEF url.
ie instead of /mypage.pdf, we use /mypage?format=pdf (this issue
appeared most often with pdf format for us)

Cheers
> Andrew Eddiehttp://learn.theartofjoomla.com- training videos for Joomla 1.6 developers

Dan YJ

unread,
Feb 6, 2014, 8:36:57 AM2/6/14
to joomla-de...@googlegroups.com
Thank you Andrew! 
Past 24h I am braking my head with this. Just finished system plugin for our template framework and 
found out that if this cwgears plugin loads before mine , our overrides brakes completely and I found 
$doc = JFactory::getDocument();  loading in onAfterInitialise inside their plugin


I almost ported entire plugin (8 months of work ) to library. 
But my question, if you have time to answer is , is there any way I can stop this particular plugin to load , or anything I could do 
without moving plugin order in Plugin manager?  I hate to do set ordering 1 on install and it can be changed any time. 

So is there anything else we can do to be sure that someone else mistake does not affect our plugins?
Reply all
Reply to author
Forward
0 new messages