10

Is there any way to use ifconfig in Magento 2 to conditionally add CSS assets, similar to Magento 1? Here's an example from Magento 1:

<action ifconfig="config_path/group/field" method="addItem">
    <type>skin_css</type><name>css/styles.css</name>
</action>

I tried something like this in Magento 2 but it doesn't work:

<head>
    <css src="css/styles.css"
        ifconfig="config_path/group/field" />
</head>

It just displays the ifconfig attribute on frontend:

<link rel="stylesheet" type="text/css" 
    ifconfig="config_path/group/field" 
    href="http://example.com/pub/static/frontend/Magento/luma/en_US/css/styles.css" />

Is there any equivalent in Magento 2?

3 Answers 3

3

It seems like it currently is not possible looking at the head.xsd definition in the develop branch:

https://github.com/magento/magento2/blob/develop/lib/internal/Magento/Framework/View/Layout/etc/head.xsd#L9

you could do something like the Require Js Module: https://github.com/magento/magento2/blob/develop/app/code/Magento/RequireJs/Block/Html/Head/Config.php

Here, the require config and mixins are inserted directly after the require.js file.

You could add your css files like this with a condition.

I also found an Issue here with a similar problem, but it did not get an answer: https://github.com/magento/magento2/issues/2004

1

You can add a class to body with the config, and the CSS style only works with that class.

<reference name="root">
    <action method="addBodyClass" ifconfig="section/group/isactive_full_homepage">
        <classname>home-full-page</classname>
    </action>
</reference>
1
  • Couldn't get this to work?
    – 00-BBB
    Apr 3, 2019 at 15:46
0

Have a look in this other thread:

https://stackoverflow.com/questions/32582704/magento-2-get-scopeconfig-values

He applied in block node instead of action node, maybe it can solve.

P.S: Make sure to empty the cache and recompile.

6
  • for the blockType it is actually defined in the xsd: github.com/magento/magento2/blob/develop/lib/internal/Magento/…. @maginfortis did you try to add the ifconfig to the css node? If it throws a validation error you could try to add the ifconfig to the head.xsd mentionend in my answer just to validate if they mabe forgot to add this to the xsd, or if there is actually not this parameter implemented ;) Feb 16, 2016 at 17:12
  • Yep, you are right, I was just checking it in the CSS. Maybe one option could be using an "around" method wrapper by defining a plugin. Feb 16, 2016 at 17:16
  • 1
    this could work on this method: github.com/magento/magento2/blob/develop/lib/internal/Magento/…. But you should always be extremely careful not executing the proceed (even if it seems legit in this case) because all other plugins listening on this method and executed after this plugin would also not be executed Feb 16, 2016 at 17:19
  • Another option could be filtering it out in the html output just using an after plugin, what do you think? Feb 16, 2016 at 17:24
  • this all (also my answer) does not feel really "right" doesn't it ;) i wonder if they thought about another way doing this or just left the ifconfig out by mistake Feb 16, 2016 at 17:26

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.