0

I want to create a simple configuration. But I can't get the data attributes (countryCode and class) from it. Can you help me please ?

assignment_adapter.xml :

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:MyVendor_CommercialAssign:etc/assignment_adapter.xsd">
    <map countryCode="Default" class="Default"/>
    <map countryCode="FR" class="FR"/>
</config>

assignment_adapter.xsd :

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
    <xs:element name="config">
        <xs:complexType>
            <xs:sequence>
                <xs:element name="map" type="mapType" minOccurs="1" maxOccurs="unbounded"/>
            </xs:sequence>
        </xs:complexType>
    </xs:element>

    <xs:complexType name="mapType">
        <xs:attribute name="countryCode" type="xs:string" use="required"/>
        <xs:attribute name="class" type="xs:string" use="required"/>
    </xs:complexType>
</xs:schema>

di.xml :

<?xml version="1.0"?>

<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
    <virtualType name="AssignmentAdapterReader" type="Magento\Framework\Config\Reader\Filesystem">
        <arguments>
            <argument name="converter" xsi:type="object">MyVendor\CommercialAssign\Model\Config\Assignment\Converter</argument>
            <argument name="schemaLocator" xsi:type="object">MyVendor\CommercialAssign\Model\Config\Assignment\SchemaLocator</argument>
            <argument name="fileName" xsi:type="string">assignment_adapter.xml</argument>
        </arguments>
    </virtualType>
    <type name="MyVendor\CommercialAssign\Model\Config\Assignment\Data">
        <arguments>
            <argument name="reader" xsi:type="object">AssignmentAdapterReader</argument>
            <argument name="cacheId" xsi:type="string">myvendor_commercial_assignement_map_cache</argument>
        </arguments>
    </type>
</config>

Here is my converter convert function :

public function convert($source)
    {
        $maps = $source->getElementsByTagName('map');
        $mapInfo = [];

        $i = 0;
        foreach ($maps as $map) {
            $mapInfo[$i]['complete'] = $map;
            $mapInfo[$i]['attributes'] = $map->attributes;
            $mapInfo[$i]['child'] = $map->childNodes;
            $i++;
        }

        return ['map' => $mapInfo];
    }

My Data class content :

use \Magento\Framework\Config\Data as DataConfig;

class Data extends DataConfig
{
}

So when I did a get('map') from my data reader I get this (there is no countryCode or class from the xml why ?) :

array(2) {
  [0] => array(3) {
    ["complete"] => object(DOMElement)#148 (18) {
      ["tagName"] => string(3) "map"
      ["schemaTypeInfo"] => NULL
      ["nodeName"] => string(3) "map"
      ["nodeValue"] => string(0) ""
      ["nodeType"] => int(1)
      ["parentNode"] => string(22) "(object value omitted)"
      ["childNodes"] => string(22) "(object value omitted)"
      ["firstChild"] => NULL
      ["lastChild"] => NULL
      ["previousSibling"] => string(22) "(object value omitted)"
      ["nextSibling"] => string(22) "(object value omitted)"
      ["attributes"] => string(22) "(object value omitted)"
      ["ownerDocument"] => string(22) "(object value omitted)"
      ["namespaceURI"] => NULL
      ["prefix"] => string(0) ""
      ["localName"] => string(3) "map"
      ["baseURI"] => string(36) "/Applications/MAMP/htdocs/project/"
      ["textContent"] => string(0) ""
    }
    ["attributes"] => object(DOMNamedNodeMap)#140 (1) {
      ["length"] => int(2)
    }
    ["child"] => object(DOMNodeList)#134 (1) {
      ["length"] => int(0)
    }
  }
  [1] => array(3) {
    ["complete"] => object(DOMElement)#123 (18) {
      ["tagName"] => string(3) "map"
      ["schemaTypeInfo"] => NULL
      ["nodeName"] => string(3) "map"
      ["nodeValue"] => string(0) ""
      ["nodeType"] => int(1)
      ["parentNode"] => string(22) "(object value omitted)"
      ["childNodes"] => string(22) "(object value omitted)"
      ["firstChild"] => NULL
      ["lastChild"] => NULL
      ["previousSibling"] => string(22) "(object value omitted)"
      ["nextSibling"] => string(22) "(object value omitted)"
      ["attributes"] => string(22) "(object value omitted)"
      ["ownerDocument"] => string(22) "(object value omitted)"
      ["namespaceURI"] => NULL
      ["prefix"] => string(0) ""
      ["localName"] => string(3) "map"
      ["baseURI"] => string(36) "/Applications/MAMP/htdocs/project/"
      ["textContent"] => string(0) ""
    }
    ["attributes"] => object(DOMNamedNodeMap)#135 (1) {
      ["length"] => int(2)
    }
    ["child"] => object(DOMNodeList)#146 (1) {
      ["length"] => int(0)
    }
  }
}
1

As you can see in the outputed section each element has two attributes but you just need to read them.

["attributes"] => object(DOMNamedNodeMap)#140 (1) {
  ["length"] => int(2)
}

Try using the getAttribute method to do this.

public function convert($source)
{
    $maps = $source->getElementsByTagName('map');
    $mapInfo = [];

    $i = 0;
    foreach ($maps as $map) {
        $mapInfo[$i]['complete'] = $map;
        $mapInfo[$i]['countryCode'] = $map->getAttribute('countryCode');
        $mapInfo[$i]['class'] = $map->getAttribute('class');
        $mapInfo[$i]['child'] = $map->childNodes;
        $i++;
    }

    return ['map' => $mapInfo];
}

Or you could loop through the attributes to get the information.

foreach ($map->attributes as $attrName => $attrNode) {
  • Thanks, It works well. I searched for the class DOMNamedNodeMap but I don't know why my IDE won't find it :/ – Kpone May 24 '17 at 8:15

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.