1

I try to add a new container to my layout. The container needs the attribute data-block='minicart'

<container name="minicart-wrapper" htmlTag="div" htmlClass="minicart-wrapper" data-block='minicart'/>

If I try this I get

Element 'container', attribute 'data-block': The attribute 'data-block' is not allowed.

More Code:

<referenceContainer name="header-wrapper">
    <container name="header-row" label="Header Bootstrap" as="header-row" htmlTag="div" htmlClass="row">

        <container name="header-row-col-a" htmlTag="div" htmlClass="col-2"/>
        <container name="header-row-col-b" htmlTag="div" htmlClass="col-7"/>
        <container name="header-row-col-c" htmlTag="div" htmlClass="col-3">

            <container name="minicart-row" label="Wrapper" htmlTag="div" htmlClass="row no-gutters minicart-wrapper">  <--- Need data-block="minicart" here

                <container name="mini-wk" label="Top" htmlTag="div" htmlClass="col-9 offset-3 mini-wk">

                    <container name="mini-wk-top" label="Top" htmlTag="div" htmlClass="mini-wk-top">
                        <container name="mini-wk-top-row" label="Top Row" htmlTag="div" htmlClass="row no-gutters">

                            <container name="mini-wk-top-row-col-a" label="Benutzerkonto" htmlTag="div" htmlClass="col-6"/>
                            <container name="mini-wk-top-row-col-b" label="Anmelden/Abmelden" htmlTag="div" htmlClass="col-6"/>
                        </container>
                    </container>

                    <container name="mini-wk-content" label="Minicart-Main" htmlTag="div" htmlClass="mini-wk-content row no-gutters"/>
                </container>
            </container>
        </container>
    </container>
</referenceContainer>

3 Answers 3

1

This is not possible in core Magento 2, the container node only supports these attributes:

  • name
  • label
  • before
  • after
  • as
  • output
  • htmlTag
  • htmlId
  • htmlClass

You would need to load a template inside that container and add your attribute there.

0

You should Try

<?xml version="1.0" ?>
<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" layout="2columns-left" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
<head>
    <css src="Vendor_ModuleName::css/style.css"/>
</head>
   <body>
        <attribute name="class" value="new-class-name"/>
        <attribute name="data-block" value="minicart"/>
    </body>
</page>

[Update answer for add attribute ]

use jQuery to add attibute to your container

please use container class name to add atrribute

$('.minicart-wrapper').attr("data-block", "minicart");

I hope it helps you

12
  • @BenCrook is right here is alternative way to do Commented Jan 17, 2020 at 11:56
  • Why update handle customer_account ? Commented Jan 17, 2020 at 12:25
  • @Black this is used in Customer Dashboard. Here it's not Require. I update my Answer. Commented Jan 17, 2020 at 13:58
  • I don't understand. How is this code supposed to add the attribute data-block to my container with name minicart-wrapper ? Commented Jan 17, 2020 at 14:04
  • @Black BenCrook already said It's not possible in Magento , Here I am show add attribute in Main Body Tag of This Layout only , and other is You would need to load a template inside that container and add your attribute there. Commented Jan 17, 2020 at 14:45
0

So there's not really a good way to solve this but it is duable.

If you must add a new attribute to a container or block you will need to modify the file:vendor/magento/framework/View/Layout/etc/elements.xsd

Here we will want to look for the type of tag you are wanting to modify.

ex. if i wanted to change a reference container i'd look for containerReferenceType under this section you'll find an <xs:attribute in this area is where you want to define your attribute.

I recommend doing this via patch as this makes it easier to maintain the modification through entire project without the need of changing the xsi:noNamespaceSchemaLocation to point to the path of YOUR .xsd file. As this might get tricky later on down the road if you find yourself needing this attribute in other parts of your project.

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.