3

I am overriding the layout and adding ViewModel in that layout but when I try to fetch the ViewModel getting below error

PHP Fatal error: Uncaught Error: Call to a member function getCodes() on null

My XML files look life which resides under

/app/design/frontend/Custom/Theme/Magento_Review/layout/override/base/review_product_list.xml

    <page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" layout="1column" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">+
   <body>
        <referenceBlock name="product_additional_data">
            <arguments>
                <argument name="view_model" xsi:type="object">Custom\ProductModel\ViewModel\LocaleInfo</argument>
            </arguments>
        </referenceBlock>
     </body>
</page>

and my ViewModel resides under

Custom/ProductModel/ViewModel/LocaleInfo.php

and files look like

use Magento\Framework\View\Element\Block\ArgumentInterface;
use Magento\Framework\App\Config\ScopeConfigInterface;
use Magento\Framework\Locale\Resolver as LocaleResolver;
use Magento\Store\Model\ScopeInterface;

class LocaleInfo implements ArgumentInterface
{
    protected $scopeConfig;
    protected $localeResolver;

    public function __construct(
        ScopeConfigInterface $scopeConfig,
        LocaleResolver $localeResolver
    ) {
        $this->scopeConfig = $scopeConfig;
        $this->localeResolver = $localeResolver;
    }

    public function getCodes($storeId = null)
    {
        $configPath = $this->localeResolver->getDefaultLocalePath();
        $localeCode = $this->scopeConfig->getValue($configPath, ScopeInterface::SCOPE_STORE, $storeId);

        return $localeCode;
    }
}

trying to fetch like below

$myViewModel = $block->getViewModel();
echo $myViewModel->getCodes();
  • Please provide the phtml file location and also do you have namespace declaration like namespace Custom\ProductModel\ViewModel; ? You also need to clear your caches and perform compilation – Rajeev K Tomy May 25 '18 at 9:33
  • app/design/frontend/Custom/Theme/Magento_Review/templates/product/view/list.phtml thats the phtml file location – Daniel_12 May 25 '18 at 10:10
  • Yes I have declared namespace as well – Daniel_12 May 25 '18 at 10:10
0

This is the block you are referencing in your layout handle XML file.

<block class="Magento\Review\Block\Product\View\ListView" 
       name="product.info.product_additional_data" 
       as="product_additional_data" 
       template="Magento_Review::product/view/list.phtml"
/>

You are referencing this block with its alias name; i.e, product_additional_data which is wrong. You should refer it with its name; i.e, product.info.product_additional_data.

So you should change your code like this:

<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" layout="1column" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">+
   <body>
        <referenceBlock name="product.info.product_additional_data">
            <arguments>
                <argument name="view_model" xsi:type="object">Custom\ProductModel\ViewModel\LocaleInfo</argument>
            </arguments>
        </referenceBlock>
     </body>
</page>

After this changes made please clear your cache and then you are good to go.

EDIT

Your layout update XML file location also seems wrong. It should be like:

app/design/frontend/Custom/Theme/Magento_Review/layout/review_product_list.xml

  • No luck :( still have same error nothing change – Daniel_12 May 25 '18 at 11:54
  • @Daniel_12 see my edit. – Rajeev K Tomy May 25 '18 at 11:57
  • I am overriding layout and I think its location is correct. check this link devdocs.magento.com/guides/v2.2/frontend-dev-guide/layouts/… – Daniel_12 May 25 '18 at 12:05
  • But I tried as you say but still no luck same error – Daniel_12 May 25 '18 at 12:05
  • @Daniel_12 you here did a theme override. So the location should be as shown in my answer. You can see that in the given link too. With this, your code should work. I cannot see any other mistakes. check your logs for any errors. – Rajeev K Tomy May 25 '18 at 12:07

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.