Magento – Conditionally add CSS class to body tag

configurationlayoutmagento2

In Magento 1 it was possible to conditionally add class to body tag through layout. It could be done with addBodyClass action with an additional condition in the ifconfig. For example:

<reference name="root">
    <action method="addBodyClass" ifconfig="yoursection/yourgroup/yourfield_value"><className>big-font</className></action>
</reference>

Without any additional conditions, in Magento 2 a class can be added like this:

<body>
    <attribute name="class" value="big-font" />
</body>

Is there any way to add similar condition in Magento 2?

This of course does not work, ifconfig is ignored:

<body>
    <attribute name="class" value="big-font" ifconfig="yoursection/yourgroup/yourfield_value" />
</body>

Best Answer

Unfortunately, as of Magento 2.1, the <attribute> tag does not allow the ifconfig attribute. It only supports two attributes : name and value you can find out from lib/internal/Magento/Framework/View/Layout/etc/body.xsd :

<xs:complexType name="bodyAttributeType">
    <xs:simpleContent>
        <xs:extension base="xs:string">
            <xs:attribute type="xs:string" name="name" use="optional"/>
            <xs:attribute type="xs:string" name="value" use="optional"/>
        </xs:extension>
    </xs:simpleContent>
</xs:complexType>