23

In Magento 2 (almost) all arguments listed in xml files have an attribute xsi:type that determine how the value of the argument is iterpreted.
For example, in di.xml file of the backend module there is this:

<argument name="scopeType" xsi:type="const">Magento\Framework\App\Config\ScopeConfigInterface::SCOPE_TYPE_DEFAULT</argument>

this means that the value of the argument scopeType is the value of the constant Magento\Framework\App\Config\ScopeConfigInterface::SCOPE_TYPE_DEFAULT

or this one

<argument name="template" xsi:type="string">Magento_Theme::root.phtml</argument>

this means that the value of the argument template is the string Magento_Theme::root.phtml.

What are all the possible values of this xsi:type attribute?

2
  • Have you ever tried to use a static instead of a const for such an argument? I can't seem to find a type that works for a static field in my class :-(
    – peedee
    Commented Dec 16, 2016 at 9:25
  • No. I didn't. I don't even think there is support for static
    – Marius
    Commented Dec 16, 2016 at 9:30

2 Answers 2

46

I've found all types by checking <xs:extension base="argumentType" in *.xsd files.

lib/internal/Magento/Framework/Data/etc/argument/types.xsd, these are base types:

  • "array"
  • "string"
  • "boolean"
  • "object"
  • "configurableObject"
  • "number"
  • "null"

lib/internal/Magento/Framework/ObjectManager/etc/config.xsd, can be found in di.xml files:

  • "object"
  • "init_parameter"
  • "const"

lib/internal/Magento/Framework/View/Layout/etc/elements.xsd, can be found in layout *.xml files:

  • "options"
  • "url"
  • "helper"

Magento/Ui/etc/ui_components.xsd, can be found in UI components' *.xml files:

  • "constant"
  • "url"
17

According to my researches, here is what I've found:

The argument interpreter is created in the lib\internal\Magento\Framework\App\ObjectManagerFactory.php :

protected function createArgumentInterpreter(
    \Magento\Framework\Stdlib\BooleanUtils $booleanUtils
) {
    $constInterpreter = new \Magento\Framework\Data\Argument\Interpreter\Constant();
    $result = new \Magento\Framework\Data\Argument\Interpreter\Composite(
        [
            'boolean' => new \Magento\Framework\Data\Argument\Interpreter\Boolean($booleanUtils),
            'string' => new \Magento\Framework\Data\Argument\Interpreter\StringUtils($booleanUtils),
            'number' => new \Magento\Framework\Data\Argument\Interpreter\Number(),
            'null' => new \Magento\Framework\Data\Argument\Interpreter\NullType(),
            'object' => new \Magento\Framework\Data\Argument\Interpreter\DataObject($booleanUtils),
            'const' => $constInterpreter,
            'init_parameter' => new \Magento\Framework\App\Arguments\ArgumentInterpreter($constInterpreter),
        ],
        \Magento\Framework\ObjectManager\Config\Reader\Dom::TYPE_ATTRIBUTE
    );
    // Add interpreters that reference the composite
    $result->addInterpreter('array', new \Magento\Framework\Data\Argument\Interpreter\ArrayType($result));
    return $result;
}

In this code, you can clearly see that different interpreters are used based on the type attribute of the argument \Magento\Framework\ObjectManager\Config\Reader\Dom::TYPE_ATTRIBUTE:

  • boolean => \Magento\Framework\Data\Argument\Interpreter\Boolean
  • string => \Magento\Framework\Data\Argument\Interpreter\StringUtils
  • number => \Magento\Framework\Data\Argument\Interpreter\Number
  • null => \Magento\Framework\Data\Argument\Interpreter\NullType
  • object => \Magento\Framework\Data\Argument\Interpreter\DataObject
  • const => \Magento\Framework\Data\Argument\Interpreter\Constant
  • init_parameter => \Magento\Framework\App\Arguments\ArgumentInterpreter (note that this one takes the \Magento\Framework\Data\Argument\Interpreter\Constant as parameter and not the constructor parameter)

Also an extra interpreter is added on the fly to handle array types:

  • array => \Magento\Framework\Data\Argument\Interpreter\ArrayType

Note: it seems like the init_parameter type is only used in the app\code\Magento\Store\etc\di.xml to initiate some constants:

<argument name="xFrameOpt" xsi:type="init_parameter">Magento\Framework\App\Response\XFrameOptPlugin::DEPLOYMENT_CONFIG_X_FRAME_OPT</argument>
...
<argument name="isCustomEntryPoint" xsi:type="init_parameter">Magento\Store\Model\Store::CUSTOM_ENTRY_POINT_PARAM</argument>
...
<argument name="runMode" xsi:type="init_parameter">Magento\Store\Model\StoreManager::PARAM_RUN_TYPE</argument>
<argument name="scopeCode" xsi:type="init_parameter">Magento\Store\Model\StoreManager::PARAM_RUN_CODE</argument>

Your Answer

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

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