Questions tagged [viewmodel]

The tag has no usage guidance.

Filter by
Sorted by
Tagged with
1 vote
1 answer
446 views

Magento 2 How can we associate a single template with multiple view models?

I want to associate another custom view model with a template. The template already has a view model in the core file. Can I associate an other custom view model with this template?
Hafiz Muzamal's user avatar
1 vote
2 answers
929 views

Magento2 error giving call to member function on null when declare View Model for product/view/addtocart.phtml

1.catalog_product_view.xml inside custom theme Magento_Catalog folder <referenceBlock name="product.info.addtocart"> <arguments> <argument name="...
Preetesh's user avatar
0 votes
2 answers
756 views

Trying to get viewModel in phtml

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 :...
Mahmood Rehman's user avatar
0 votes
1 answer
45 views

How can I get the admin edit user url key for a specific user if I have the user_id and account info, what are the classes/methods for di?

Example: http://localhost/admin/admin/user/edit/user_id/5/key/db1082c352e51cfb817ce0335dd060cf6e953568daf2da25cca24eadb04dc884/ I want to return '...
Justin Collins's user avatar
0 votes
1 answer
775 views

Correct way of using ViewModel inside of pagebuilder elements?

I have a ViewModel that returns if a user is logged in or not and we need to add this functionality to vendor/magento/module-page-builder/view/frontend/templates/catalog/product/widget/content/...
unknown's user avatar
0 votes
2 answers
360 views

View model works only in the theme layout issue

I've made this file: catalog_category_view.xml in the app/code/Namespace/Products/view/frontend/layout/ folder , with this content: <?xml version="1.0"?> <page xmlns:xsi="http:...
Attila Naghi's user avatar
  • 3,903
1 vote
2 answers
527 views

Using Customer SessionFactory in ViewModel

I am trying to retrieve customer data from the Customer session in ViewModel. app/code/Magento/Module/ViewModel/Template.php Due to FPC enabled, the session value is not retrieved. However, when I use ...
Dexter's user avatar
  • 3,613
0 votes
1 answer
876 views

How do I pass in a viewModel to a core block that has been overridden?

I am rewritting the /vendor/magento/module-catalog/view/base/templates/product/price/amount/default.phtml file. The file MyCompany_MyApp::product/default.phtml works fine, but I want to pass a ...
Justin Collins's user avatar
0 votes
1 answer
814 views

Magento 2.4 How to get category of current product from within ViewModel without using objectManager?

This works but uses objectManager namespace Myvendor\Myprogram\ViewModel; class CatLogic extends \Magento\Framework\View\Element\Template implements \Magento\Framework\View\Element\Block\...
Justin Collins's user avatar
0 votes
1 answer
433 views

Magento2: Override ViewModel

Is there a way to override ViewModel Function I Have a installed a Module in vendor but I want to override Function of ViewModel
Paras Arora's user avatar
0 votes
1 answer
267 views

ViewModel modification makes existing list.phtml break

I recently made a modification to catalog-category-view.xml and overrode category.products.list. It seemed to work at the time, I had badges displaying on a rendered page. Now the template file errors ...
Scott Anderson's user avatar
1 vote
2 answers
2k views

Uncaught Error: Call to a member function getSomething() on null in phtml ViewModel magento2

I have created a ViewModel, when I am calling it in the phtml it giving me the below error Fatal error: Uncaught Error: Call to a member function getSomething() on null in Magento_Catalog/templates/...
Noshad's user avatar
  • 121
0 votes
1 answer
691 views

How to add View Model to a Custom Widget?

I am extending ProductList widget and I want to call a View Model from another module testing\ProductFeatures\ViewModel in my own custom template. I have tried researching on how to use the View Model ...
Magento_learner's user avatar
5 votes
2 answers
2k views

Pass data or arguments to ViewModel in layout

When we worked directly with blocks, it was easy to pass arguments to the block in layout (e.g. default.xml) like: <block template="..." class="..." name="..."> ...
Creepin's user avatar
  • 335
0 votes
2 answers
2k views

Magento 2 add ViewModel to product widget

I use ViewModel for custom data in category product listing and everything working ok but i need add ViewModel to widget with new products. Is it possible to add ViewModel to widget? Check my ...
omelandr's user avatar
  • 1,267
1 vote
1 answer
2k views

Magento 2.3.* - How to override ViewModel for override block?

Magento 2.3.* I want to override ViewModel for the override block (custom.block.name). But the issue is my .phtml a file called from the block and it is calling on some condition. Not every time call....
Dhaduk Mitesh's user avatar
2 votes
1 answer
282 views

Magento 2 Paginate custom collection using ViewModel

Magento 2 encourages the usage of view models instead of filling the blocks with business logic. A view model is an abstraction of the view exposing public properties and commands. It allows ...
Radu's user avatar
  • 460
2 votes
3 answers
408 views

The correct implementation of the output of subcategories in the main category

i want to display subcategories in the root category, i found this way that works for me. app/design/frontend/[Vendor]/[theme]/Magento_Catalog/templates/category/products.phtml <?php $category =...
ann karsksya's user avatar
0 votes
1 answer
628 views

When use block vs view model?

I'm creating a module using blocks and view models, but when is it better to use view models instead of blocks?
Rafael Corrêa Gomes's user avatar
1 vote
0 answers
452 views

Editing breadcrumbs ViewModel method

I am trying to override a method from /vendor/magento/module-catalog/ViewModel/Product/Breadcrumbs.php I get an error Type Error occurred when creating object: Piranha\TagRemover\ViewModel\Product\...
MaddocksL's user avatar
0 votes
1 answer
811 views

How to pass view model class argument when call block from static block

<block class="Magento\Store\Block\Switcher" name="store_language" as="store_language" template="Magento_Store::switch/languages.phtml"> <arguments> <argument name="view_model" xsi:type="...
Navin Bhudiya's user avatar
1 vote
1 answer
103 views

Nice way to inject the main block to a viewmodel

We want to follow the best practice to add additional functionality to a block template without "rewriting" (i.e. a DI preference) the block. Is there a nice way to inject the main block (which ...
Alex's user avatar
  • 13.7k
1 vote
1 answer
2k views

When ViewModel class need to create in Magento2?

I have followed Magento_Catalog Module and I have found ViewModel folder there. Can anyone please explain when we need to create ViewModel classes when we creating our custom module?
Rutvee Sojitra's user avatar
11 votes
3 answers
7k views

When it is good to use viewModel ? What is difference between viewModel and Block?

I am bit confuse about using viewModel in Magento. I am not getting exact difference between viewModel and Block. In which case using viewModel is best case?
Dhaval Solanki's user avatar
2 votes
5 answers
2k views

Can I and should I call Block methods from its' View Model?

I have a template and should write some logic. I have made a view model and I'm calling it from a template. Should I call block methods from the view model? And if so, how can I do it?
TheKitMurkit's user avatar
2 votes
2 answers
3k views

Knockout.js What is the correct way to reference $parent on js file?

I am new on Knockout.js, especially on how to use it in the Magento way. I create a custom ViewModel with a js file called storepickup-select.js and an HTML template called storepickup-select.html. ...
Eliacim DAVILA - CENSERE's user avatar
1 vote
1 answer
2k views

Magento 2 : What is use of observableArray in JS file with knockout JS?

I have checked observableArray word declared in js file at top position. I have been seen this function like this items: ko.observableArray([]). Is this function related to AJAX while response is ...
Niraj Patel's user avatar
7 votes
2 answers
11k views

How to Add a viewModel to a core block?

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&...
diazwatson's user avatar
  • 2,410