User tests: Successful: Unsuccessful:
public function onAfterRoute()
{
$file = 'com_banners/admin-banner-edit.min.js';
Joomla\CMS\HTML\HTMLHelper::_('script', $file,
array('version' => 'auto', 'relative' => true)
);
return;
...and so on...
Activate the plugin. => Error.
Deactivate plugin by renaming folder plugins/system/cachexxxx/
Apply patch.
Rename back folder plugins/system/cache/ to activate it.
Reload backend ==> No error.
Error:
0 - Call to a member function addScript() on null
1 | ( ) | JROOT/libraries/src/HTML/HTMLHelper.php:736
2 | Joomla\CMS\HTML\HTMLHelper::script()
Status | New | ⇒ | Pending |
Category | ⇒ | Libraries |
Labels |
Added:
?
|
The problem is that you're using onAfterRoute
to add scripts. The document hasn't been created at this point yet. Use a later event.
The problem is that you're using onAfterRoute to add scripts. The document hasn't been created at this point yet. Use a later event.
That worked in Joomla 3 without any problems over years. If that behavior was changed in Joomla 4 then that's a harsh B\C break.
Test the same scenario in Joomla 3.
Or compare the code in Joomla 3.
https://github.com/joomla/joomla-cms/blob/staging/libraries/src/HTML/HTMLHelper.php#L651
https://github.com/joomla/joomla-cms/blob/staging/libraries/src/HTML/HTMLHelper.php#L731
It not a bug, it feature
That worked in Joomla 3 without any problems over years.
It worked, but It was incorrect even for Joomla 3, and that made many bugs because the document may be incorrectly instantiated, because route not parsed at that point of time.
Try on afterRoute at least, or afterDispatch
Route is parsed but the document hasn't been created yet. Use onAfterDispatch
or later event to manipulate the document.
Furthermore, your code doesn't even check for document type. You should always check for document type. Otherwise errors will occur on other document types.
I think this PR can be closed.
I think this PR can be closed.
Not until I have a clear answer why this grave B\C break is a "feature". I'm still convinced that it is a bug.
BTW: I know why I use it in onAfterRoute and not (again) later and next time (again) later...
@Fedik pay attention, @ReLater is already using and talking about onAfterRoute, so route is parsed
That's correct! Thus @Fedik 's comment is irrelevant.
@ReLater is right
This is a hard B\C break that could potentially affect hundreds of plugins out there.
Moreover you can still use a deprecated code in the 3.x fashion to create a document object in the onAfterRoute event:
$doc = Joomla\CMS\Factory::getDocument();
Given that this is a deeper change it should be at least well documented to update plugins accordingly.
You can argue the B/C semantics all day long, the fact of the matter is that instantiating the Document before it is intended to be can and does cause side effects if not handled properly. So it is not right, ever, for a plugin to be loading assets to the Document before the component is dispatched, and the fact it might have worked in 3.x is moreso based on flawed architecture and not being a core feature that a plugin can force Document instantiation before the request is routed and leave the application in an unexpected state (i.e. pushing assets on a JSON request into the Document before the app creates it at the intended time is going to result in the HtmlDocument being used instead of the JsonDocument, this is why extension developers have a suite of hacks to short circuit core when a non-HTML response format is intended).
Also, it is perfectly valid for the Document to not be instantiated before onAfterRoute is emitted. Plugins can still hook the system and make changes to the environment before the Document is created (using custom GET var or session var to force a format is one intricate example I can think of). So the answer is not moving Document instantiation and there’s an entire other issue bashing out why so much of the core request-response workflow is flawed and needs redesigning.
Thanks!
Added to documentation: https://docs.joomla.org/Plugin/Events/System#onAfterRoute
Status | Pending | ⇒ | Closed |
Closed_Date | 0000-00-00 00:00:00 | ⇒ | 2019-07-22 13:13:55 |
Closed_By | ⇒ | ReLater |
Identical test with stylesheet