Skip to content
This repository was archived by the owner on Nov 19, 2024. It is now read-only.

Commit 2afa6b4

Browse files
diazwatsonjeff-matthews
authored andcommitted
#5115 Add ViewModels section to 2.2 dev-guide (#5147)
* #5115 Add ViewModels section to 2.2 dev-guide * Grammar fixes. * #5115 delete template url * #5115 editorial changes view models * #5115 add symlink to 2.3 * #5115 more editorial changes
1 parent 2f49e63 commit 2afa6b4

File tree

4 files changed

+70
-0
lines changed

4 files changed

+70
-0
lines changed

_data/toc/php-developer-guide.yml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -252,6 +252,10 @@ pages:
252252
- label: Adapters
253253
url: /extension-dev-guide/adapters.html
254254

255+
- label: View models
256+
include_versions: ["2.2", "2.3"]
257+
url: /extension-dev-guide/view-models.html
258+
255259
- label: Variable Pool
256260
include_versions: ["2.3"]
257261
url: /extension-dev-guide/variable-pool/

guides/v2.2/extension-dev-guide/module-development.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,3 +21,4 @@ Magento 2 is flexible and as a result has varied functionality, so developing a
2121
* [Routing]({{ page.baseurl }}/extension-dev-guide/routing.html)
2222
* [Indexing]({{ page.baseurl }}/extension-dev-guide/indexing.html)
2323
* [Configure a service as a web API]({{ page.baseurl }}/extension-dev-guide/service-contracts/service-to-web-service.html#configure-webapi)
24+
* [ViewModels]({{ page.baseurl }}/extension-dev-guide/view-models.html)
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
---
2+
title: View models
3+
contributor_name: Space 48
4+
contributor_link: https://www.space48.com/
5+
group: php-developer-guide
6+
---
7+
8+
A view model is an abstraction of the view exposing public properties and commands. It allows developers to offload features and business logic from block classes into separate classes that are easier to maintain, test, and reuse.
9+
10+
## When to use view models
11+
12+
Use this approach anytime you need to inject functionality into template files and your code does not need to be backwards compatible with Magento 2.1.
13+
14+
{: .bs-callout-info }
15+
View models are available in Magento 2.2 onwards. If your code must be compatible with older versions of Magento, consider adding your logic to blocks. For more information about backward compatibility, see [Backward compatibility]({{ page.baseurl }}/contributor-guide/backward-compatible-development/).
16+
17+
## How to write view models
18+
19+
The following example shows how to add functionality to a core template with custom logic using a view model in the `cart/item/default.phtml` template, which is located in the `Magento/Checkout/view/frontend/layout/checkout_cart_item_renderers.xml` file:
20+
21+
```xml
22+
<?xml version="1.0"?>
23+
<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
24+
<body>
25+
<referenceBlock name="checkout.cart.item.renderers.default">
26+
<arguments>
27+
<argument name="viewModel" xsi:type="object">Vendor\CustomModule\ViewModel\MyClass</argument>
28+
</arguments>
29+
</referenceBlock>
30+
</body>
31+
```
32+
33+
You must implement the right interface in your `viewModel` class (for example `ArgumentInterface`):
34+
35+
```php
36+
namespace Vendor\CustomModule\ViewModel;
37+
38+
class MyClass implements \Magento\Framework\View\Element\Block\ArgumentInterface
39+
{
40+
public function getTitle()
41+
{
42+
return 'Hello World'
43+
{
44+
}
45+
```
46+
47+
Finally, in the `cart/item/default.phtml` template, you can access the public methods of your view model:
48+
49+
```html
50+
<?php
51+
52+
/** @var $viewModel \Vendor\CustomModule\ViewModel\MyClass */
53+
54+
$viewModel = $block->getViewModel();
55+
56+
?>
57+
<h1><?= $block->escapeHtml($viewModel->getTitle()); ?></h1>
58+
59+
```
60+
61+
## Examples of View models in Magento
62+
63+
- [Magento_Catalog]({{ site.mage2bloburl }}/{{ page.guide_version }}/app/code/Magento/Catalog/view/frontend/layout/catalog_product_view.xml#L34){:target="_blank"}
64+
This `viewModel` is used to inject breadcrumb JSON with HTML-escaped names into the template file.
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
../../../guides/v2.2/extension-dev-guide/view-models.md

0 commit comments

Comments
 (0)