1

I've made a custom attribute module using this magento stackexchange Link to article.

etc/frontend/events.xml

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Event/etc/events.xsd">
    <event name="core_layout_render_element">
        <observer name="core_layout_render_container_attribute" instance="Stackoverflow\ContainerAttribute\Observer\LayoutSchemaAttributesObserver" />
    </event>
</config>

Observer/LayoutSchemaAttributesObserver.php

namespace Stackoverflow\ContainerAttribute\Observer;

use Magento\Framework\Event\ObserverInterface;
use Magento\Framework\View\Layout\Element;

class LayoutSchemaAttributesObserver implements ObserverInterface
{

public function execute(\Magento\Framework\Event\Observer $observer)
{
    /** @var \Magento\Framework\View\Layout $layout */
    $layout = $observer->getEvent()->getLayout();
    $elementName = $observer->getEvent()->getElementName();

    if($layout->isContainer($elementName) && ($containerTag = $layout->getElementProperty($elementName, Element::CONTAINER_OPT_HTML_TAG))) {
        $nodes = $layout->getXpath(sprintf('//*[@name="%s"]/attribute[@name]', $elementName));
        if ($nodes) {
            /** @var \SimpleXMLElement $_node */
            foreach ($nodes as $_node) {
                $name = $_node->attributes()->name;
                $value = $_node->attributes()->value;
                $output = $observer->getEvent()->getTransport()->getOutput();
                $output = preg_replace(
                    "/^(<$containerTag.*?)(>)/",
                    sprintf("$1 %s$2", ($name && $value) ? sprintf("%s=\"%s\"", $name, $value) : $name),
                    $output
                );
                $observer->getEvent()->getTransport()->setOutput($output);
            }
        }
    }
}
}

This then allowed me to use:

<attribute name="customAttribute" />

It worked great in 2.1 did exactly what i needed. However after upgrading to 2.2.0 I'm now getting the following error:

Element 'attribute': This element is not expected. Expected is one of ( block, container, referenceBlock, referenceContainer, uiComponent ).

Obviously attribute isn't a valid xml tag for M2.2 but, why can't I now use this? What has changed in M2.2 that makes this no longer work?

As always, appreciate any help.

andy jones
  • 1,383
  • 2
  • 21
  • 50

0 Answers0