Questions tagged [viewmodel]

The tag has no usage guidance.

Filter by
Sorted by
Tagged with
0 votes
1 answer
30 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 '...
user avatar
0 votes
1 answer
118 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/...
user avatar
0 votes
2 answers
41 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:...
user avatar
  • 3,595
1 vote
2 answers
117 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 ...
user avatar
  • 3,554
0 votes
1 answer
185 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 ...
user avatar
0 votes
1 answer
163 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\...
user avatar
0 votes
1 answer
183 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
user avatar
0 votes
1 answer
139 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 ...
user avatar
1 vote
2 answers
1k 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/...
user avatar
  • 91
0 votes
1 answer
374 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 ...
user avatar
5 votes
2 answers
999 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="..."> ...
user avatar
  • 315
0 votes
2 answers
1k 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 ...
user avatar
  • 1,227
1 vote
1 answer
1k 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....
user avatar
2 votes
1 answer
178 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 ...
user avatar
  • 450
2 votes
3 answers
238 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 =...
user avatar
0 votes
1 answer
315 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?
user avatar
1 vote
0 answers
333 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\...
user avatar
0 votes
1 answer
521 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="...
user avatar
1 vote
1 answer
50 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 ...
user avatar
  • 14.4k
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?
user avatar
9 votes
3 answers
5k 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?
user avatar
2 votes
4 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?
user avatar
1 vote
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. ...
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 ...
user avatar
6 votes
2 answers
9k 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&...
user avatar
  • 2,265