2

I have the following custom xml inside of my Magento_theme > layout > default.xml

<referenceBlock name="top.links">

            <block class="Magento\Framework\View\Element\Html\Link" name="contactus.link"  after="my-account-link">
                <arguments>
                    <argument name="label" xsi:type="string" translate="false">Contact Us</argument>
                    <argument name="path" xsi:type="string" translate="false">contact-us</argument>
                </arguments>
            </block>

            <block class="Magento\Framework\View\Element\Html\Link" name="aboutus.link" after="contactus.link">
                <arguments>
                    <argument name="label" xsi:type="string" translate="false">About Us</argument>
                    <argument name="path" xsi:type="string" translate="false">about-us</argument>
                </arguments>
            </block>

            <referenceBlock name="register-link" remove="true" />
            <referenceBlock name="authorization-link" remove="true" />
            <referenceBlock name="wish-list-link" remove="true" />
            <reeferenceBlock name="my-account-link" remove="false" />

        </referenceBlock>

Im removing blocks and adding custom ones in.

What I'd like to to is add a class to the two custom blocks - aboutus.link and contactus.link - how do i go about doing this? Or can it not be done with xml.

2

You can add css as described below by the element

name="class"

    <block class="Magento\Framework\View\Element\Html\Links" name="header.links">
        <arguments>
            <argument name="label" xsi:type="string" translate="false">Text</argument>
           <argument name="path" xsi:type="string" translate="false">Text</argument>
            <argument name="class" xsi:type="string">header links</argument>
        </arguments>
    </block>
  • adding class creates an exception in 2.1.2 => section source is not supported, adding css_class doesn't create on but then the class is never added – OZZIE Mar 7 '17 at 12:27
2

There are 2 ways to do that:-

1- Add wrapping container to your block, like :-

<container name="div.header.links" htmlTag="div" htmlClass="header_links" after="-">
        <block class="Magento\Framework\View\Element\Html\Links" name="header.links" >

     </block>
</container>

2:- Add tag to referanceContainer tag, like :-

<referenceContainer name="header.links" htmlClass="css_class" htmlTag="div">
<block class="Magento\Cms\Block\Block" name="header.links">
   <arguments>

   </arguments>
</block>
0

Looking at the Magento\Framework\View\Element\Html\Link\Current code for version 2.2.6, there is no class argument supported (as documented in the previously accepted answer). However class is an accepted attribute. It can be used to set the css_class as follows:

<block class="Magento\Framework\View\Element\Html\Link\Current">
  <arguments>
    <argument name="label" xsi:type="string" translate="true">My Link</argument>
    <argument name="path" xsi:type="string">my_page</argument>
    <argument name="attributes" xsi:type="array">
       <item name="class" xsi:type="string">the-class</item>
    </argument>
   </arguments>
</block>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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