1

I'm trying to get viewModel in two areas inside phtml. I'm getting an error (Fatal Error: 'Uncaught Error: Call to a member function getviewmodelText() on null). My code for ViewModel is : Phtml file :

$viewModel = $block->getViewModel();
     $heelDetail = $viewModel->getviewmodelText($item);

Default.xml :

    <referenceContainer name="content">
                 <block name="size">
                    <arguments> 
                        <argument name="viewModel" xsi:type="object">Mycode\Sales\ViewModel\Size</argument> 
                    </arguments> 
                </block>
            </referenceContainer>

Block Code :

    namespace Mycode\Sales\ViewModel; 
    
    use Magento\Framework\View\Element\Block\ArgumentInterface; 
    
    class Size implements ArgumentInterface 
    {
          
        public function getviewmodelText($item)
        {
             return $item; // add a dollar sign here
    
        }
     }

2 Answers 2

0

I think you've not attached the phtml file to the block in the layout, that's why you're getting this error. Attach the phtml using following code and make sure you're running the viewModel functions in the attached phtml itself;

 <referenceContainer name="content">
                 <block name="size" template="Mycode_Sales::your_template.phtml">
                    <arguments> 
                        <argument name="viewModel" xsi:type="object">Mycode\Sales\ViewModel\Size</argument> 
                    </arguments> 
                </block>
            </referenceContainer>
4
  • by setting the template, whether it will be accessed on other phtml file ? if yes how it's possible?Because i want to access viewModel in two phtmls files. Commented Sep 1, 2022 at 6:57
  • got error after setting template (Fatal error: Uncaught Error: Call to a member function getOrder() on null ) .$viewModel = $block->getData('viewModel'); $_item = $block->getItem(); $_order = $_item->getOrder(); Commented Sep 1, 2022 at 7:00
  • Does your block have these functions? Commented Sep 1, 2022 at 7:06
  • Yes, and I just removed it from there but no luck. Commented Sep 1, 2022 at 7:18
0

Try using this

<?xml version="1.0"?>
<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" layout="admin-2columns-left" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
    <body>      
        <referenceContainer name="content">
            <block  name="order.export" template="SwiftOtter_OrderExport::export.phtml">
                <arguments>
                    <argument name="view_model" xsi:type="object"> SwiftOtter\OrderExport\ViewModel\OrderDetails</argument>
                </arguments>
            </block>
        </referenceContainer>
    </body>
</page>

Then in export.phtml file:

<?php 
    $view = $block->getData("view_model");
    $name = $view->getName();
?>

echo $view->getviewmodelText($item);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.