Whenever I try to use my own custom ViewModel the module stops working. Error: main.CRITICAL: Class Lith\Contacts\ViewModel\AdditionalInfo does not exist [] []
The file structure is simple:
- code
- Lith
- Contacts
- etc
- view
- frontend
- layout
- templates
- web
- ViewModel
Inside the view\frontend\layout 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">Lith\Contacts\ViewModel\AdditionalInfo</argument>
</arguments>
The ViewModel class is created inside ViewModel folder and called AdditionalInfo.php. Its code:
<?php
namespace Lith\Contacts\ViewModel;
use Magento\Framework\View\Element\Block\ArgumentInterface;
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"
>Magento\Contact\ViewModel\UserDataProvider</argument>
</arguments>
Then it works fine.
If i remove the <argument name="view_model" xsi:type="object">Lith\Contacts\ViewModel\AdditionalInfo</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/