7

I'm creating a Magento 2 module that adds some functionality to the backend. It does not touch the frontend at all.
But if I place some virtual type in the etc/adminhtml/di.xml (the admin di file) file I get an error that the class is not found.
If I add the same think in etc/di.xml (the global di file) everything works properly.
Other things like form modifiers work properly when added to the adminhtml di file.
From what I understood, the etc/di.xml file and etc/adminhtml/di.xml file should both be loaded on the backend pages.
Apparently this does not always work.

Here is the markup that doesn't work in the adminhtml di file.

<virtualType name="UmcConfigClassConfigSchemaLocator" type="Umc\Base\Model\Config\SchemaLocator\SchemaLocator">
    <arguments>
        <argument name="fileSchema" xsi:type="string">class</argument>
        <argument name="mergedSchema" xsi:type="string">class</argument>
    </arguments>
</virtualType>
<virtualType name="UmcConfigClassConfigMapperFactory" type="Umc\Base\Model\Config\Mapper\Factory">
    <arguments>
        <argument name="typeMap" xsi:type="array" />
    </arguments>
</virtualType>
<virtualType name="UmcConfigClassConfigConverter" type="Umc\Base\Model\Config\Converter\Converter">
    <arguments>
        <argument name="mapperFactory" xsi:type="object">UmcConfigClassConfigMapperFactory</argument>
        <argument name="mapperList" xsi:type="array" />
        <argument name="idNodes" xsi:type="array">
            <item name="0" xsi:type="string">class</item>
        </argument>
    </arguments>
</virtualType>
<virtualType name="UmcConfigClassConfigReader" type="Magento\Framework\Config\Reader\Filesystem">
    <arguments>
        <argument name="schemaLocator" xsi:type="object">UmcConfigClassConfigSchemaLocator</argument>
        <argument name="converter" xsi:type="object">UmcConfigClassConfigConverter</argument>
        <argument name="fileName" xsi:type="string">class.xml</argument>
        <argument name="idAttributes" xsi:type="array">
            <item name="/classes/class" xsi:type="string">id</item>
        </argument>
        <argument name="defaultScope" xsi:type="string">umc</argument>
    </arguments>
</virtualType>
<type name="Umc\Base\Model\Config\ClassConfig">
    <arguments>
        <argument name="reader" xsi:type="object">UmcConfigClassConfigReader</argument>
    </arguments>
</type>

Again, all the classes I have are used only in the backend.
I get this error:

RuntimeException: Source class "\UmcConfigClassConfig" for "\UmcConfigClassConfigMapper" generation does not exist.....

But this one works.

<virtualType name="UmcBaseUiDataProviderModuleFormModifierPool" type="Magento\Ui\DataProvider\Modifier\Pool">
    <arguments>
        <argument name="modifiers" xsi:type="array">
            <item name="system" xsi:type="array">
                <item name="class" xsi:type="string">Umc\Base\Ui\DataProvider\Module\Form\Modifier\System</item>
                <item name="sortOrder" xsi:type="number">1</item>
            </item>
        </argument>
    </arguments>
</virtualType>
<type name="Umc\Base\Ui\DataProvider\Module\Form\DataProvider">
    <arguments>
        <argument name="pool" xsi:type="object">UmcBaseUiDataProviderModuleFormModifierPool</argument>
    </arguments>
</type>

What should be placed in the general di.xml and what should be placed in the area specific di.xml?

Note: I've cleared the cache and the generation folder.

6

The common rule is:

  • All modular DI settings (except for presentation layer configuration) SHOULD be put in <module_dir>/etc/di.xml
  • All modular presentation layer DI settings SHOULD be put to <module_dir>/etc/<area_code>/di.xml

For you concrete case, also need to add how to configuration merged:

  • the configuration in modules dimension is merged values

  • the configuration in areas dimension is overwritten values of global scope

So, I don't catch what the concrete problem in your case, but it looks like problem with values overwriting

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.