yireo/Yireo_GoogleTagManager2

RuntimeException thrown on product view

Closed this issue · 5 comments

I get the exception:
Source class "\Yireo\GoogleTagManager2\ViewModel\Product" for "Yireo\GoogleTagManager2\ViewModel\Product\Interceptor" generation does not exist.

When I take a look at the ViewModel directory, the Product file/class is missing indeed.

Adding the following line to \Yireo\GoogleTagManager2\Block\Generic::getViewModel() solves the problem for me.

    public function getViewModel()
    {
        $viewModelClass = str_replace('\Block\\', '\ViewModel\\', get_class($this));
+       $viewModelClass = preg_replace('/\\\Interceptor$/', '', $viewModelClass);

        return $this->viewModelFactory->create($viewModelClass);
    }

This problem only occurs when writing a plugin for either \Yireo\GoogleTagManager2\Block\Generic, or \Yireo\GoogleTagManager2\Block\Product.

I already had the feeling it had to do with specific plugins that were trying to add something. The issue is that I'm trying to do something new here, with automatically created ViewModels - it is new behaviour that works differently in Magento 2.2, but I wanted to have something similar already in Magento 2.1. 5ac692d

Great, thank you!

Just curious - can you show me a Magento 2.2 example where this behavior is being implemented?