Allow to use $before parameter in addCss/addJs from XML #4151
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Description (*)
In 57563fc you added the ability to use
addCss
,addJs
andaddItem
to add a new script or stylesheet before or after another specific item.This works fine if you call it from PHP:
But usually you define your JS and CSS in XML. The
<before>
parameter doesn't work there.You'll find the reason for this in Mage_Page_Block_Html_Head::_sortItems(). This method sorts the newly added JS/CSS to the correct position. The method states in its PHPDoc comment that the $before parameter can be string or bool.
But then $before is compared using strict comparison.
So, setting
$before
to a string value would never work. The<before>true</before>
that you set in the XML earlier comes in as string"true"
, not as booleantrue
, and so would not have worked either.To fix this, my PR adds a call to
filter_var(...)
.filter_var(...)
converts any boolean-like values ("true", "false", "1", "0", "on", "off", ...) into actual boolean values.Manual testing scenarios (*)
Magento's rwd theme by default has these stylesheets in the <head> section:
Imagine that on the home page, you want to add a new stylesheet my.css. For some dependency reasons you want to have it BEFORE the madisonisland.css. So, you add this to the Layout Update XML of the CMS page "home".
It doesn't matter if the my.css actually exists.
Without my PR, the my.css will simply be added to the end, even if you set
<before>true</before>
:With my PR, it is correctly inserted before the madisonisland.css:
Questions or comments
You might argue that we could just make the comparison non-strict. This would work with "true", "1", "0" and similar, but not with the string
"false"
, which would still be interpreted as booleantrue
. That's why I usedfilter_var()
.Contribution checklist (*)