TransWikia.com

$block->getViewModel() returning NULL

Magento Asked by Daniel_12 on November 30, 2021

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">CustomProductModelViewModelLocaleInfo</argument>
            </arguments>
        </referenceBlock>
     </body>
</page>

and my ViewModel resides under

Custom/ProductModel/ViewModel/LocaleInfo.php

and files look like

use MagentoFrameworkViewElementBlockArgumentInterface;
use MagentoFrameworkAppConfigScopeConfigInterface;
use MagentoFrameworkLocaleResolver as LocaleResolver;
use MagentoStoreModelScopeInterface;

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();

3 Answers

I also faced the same situation and it worked once I used the view model argument name with underscore. Same as below as per the M2 Docs.

<referenceBlock name="checkout.cart.item.renderers.default">
    <arguments>
        <argument name="view_model" xsi:type="object">OrangeCompanyCatalogViewModelMyNewViewModel</argument>
    </arguments>
</referenceBlock>

Reference: https://devdocs.magento.com/guides/v2.4/extension-dev-guide/view-models.html

Because on phtml file it was only possible to use $block->getViewModel() when we use name="view_model" otherwise we couldn't access this instance using get() instead of that we may have to use getData('unique_name').

In your sample code, I notice that <referenceBlock name="product_additional_data"> but I think it should be <referenceBlock name="product.additional.data"> as per the M2 Docs and also it should be the same name where that you refer the block name.

Cheers!

Answered by Dasitha Abeysinghe on November 30, 2021

instead of view_model please write viewModel

<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="viewModel" xsi:type="object">CustomProductModelViewModelLocaleInfo</argument>
             </arguments>
        </referenceBlock>
   </body>
</page>

Answered by Murtuza Zabuawala on November 30, 2021

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

<block class="MagentoReviewBlockProductViewListView" 
       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">CustomProductModelViewModelLocaleInfo</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

Answered by Rajeev K Tomy on November 30, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP