문제

I am trying to overwrite a core template and add my custom logic using a viewModel as follows:

<?xml version="1.0"?>
<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
<body>
    <referenceBlock name="name.of.the.core.block" template="Vendor_CustomModule::path/to/template.phtml">
        <arguments>
           <argument name="viewModel" xsi:type="object">Vendor\CustomModule\ViewModel\Class</argument>
        </arguments>
    </referenceBlock>
</body>

But it's not working.

I know it is possible to inject viewModels into blocks but not sure if it is possible by referencing an existing block?

도움이 되었습니까?

해결책

It is possible and it works as expected!

The reason why it was not working was because I was not implementing the right interface in my viewModel class.

All viewModels need to to implement ArgumentInterface.

namespace Vendor\CustomModule\ViewModel;

class Class implements \Magento\Framework\View\Element\Block\ArgumentInterface
{
    public function __construct()
    {

    }
}

다른 팁

I'm not exactly clear what's your question and what you're trying to do by <arguments> element. But I can say you can't use "template" attribute inside the "<referenceBlock>" element as it's invalid.

Maybe you can redefine the core block with the same name.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 magento.stackexchange
scroll top