Skip to content
Advertisement

Custom ViewModel is not found

Whenever I try to use my own custom ViewModel the module stops working. Error: main.CRITICAL: Class LithContactsViewModelAdditionalInfo does not exist [] []

The file structure is simple:

- code
  - Lith
    - Contacts
      - etc
      - view
        - frontend
          - layout
          - templates
          - web
      - ViewModel

Inside the viewfrontendlayout I created a file contact_index_index.xml Where I defined a new block inside a referenceContainer with a static message – Works fine. But when I add an argument, to pass in a ViewModel, it stops working (the static message doesn’t appear) with my custom ViewModel. NOTE works fine if I pass existing Magento ViewModel.

The argument:

<arguments>
    <argument name="view_model" xsi:type="object">LithContactsViewModelAdditionalInfo</argument>
</arguments>

The ViewModel class is created inside ViewModel folder and called AdditionalInfo.php. Its code:

<?php

namespace LithContactsViewModel;

use MagentoFrameworkViewElementBlockArgumentInterface;

class AdditionalInfo implements ArgumentInterface
{
    public function getMessage(): string
    {
        return 'Addition info text test - from a ViewModel';
    }
}

The template itself doesn’t use the ViewModel – its just a static div with a string text hence it’s not important.

This all when loading the website doesn’t work. It does not find a ViewModel.

But whenever I change the argument from the one above to this:

<arguments>
    <argument
name="view_model"
xsi:type="object"
>MagentoContactViewModelUserDataProvider</argument>
</arguments>

Then it works fine.

If i remove the <argument name="view_model" xsi:type="object">LithContactsViewModelAdditionalInfo</argument>, since ViewModel in the template is not used right now, it also works fine as it renders the text.

Why it doesn’t find my custom ViewModel? What did I do wrong?

EDIT: Heres a public repository of the files if any additional info is required https://bitbucket.org/Edvbar/magento2/src/master/

Advertisement

Answer

Your Lith_Contacts module on Bitbucket is added to the folder app/design/code/Lith/Contacts. But this should not be put in the design folder.

Please move the module to app/code/Lith/Contacts. Afterwards run php bin/magento setup:upgrade from command line to enable module.

User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement