Skip to content

Commit 8b5818d

Browse files
committed
Contact form > Adding ViewModel
1 parent 3666e03 commit 8b5818d

File tree

3 files changed

+82
-4
lines changed

3 files changed

+82
-4
lines changed
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
7+
namespace Magento\Contact\ViewModel;
8+
9+
use Magento\Contact\Helper\Data;
10+
use Magento\Framework\View\Element\Block\ArgumentInterface;
11+
12+
/**
13+
* Provides the user data to fill the form.
14+
*/
15+
class UserDataProvider implements ArgumentInterface
16+
{
17+
18+
/**
19+
* @var Data
20+
*/
21+
private $helper;
22+
23+
/**
24+
* UserDataProvider constructor.
25+
* @param Data $helper
26+
*/
27+
public function __construct(
28+
Data $helper
29+
) {
30+
$this->helper = $helper;
31+
}
32+
33+
/**
34+
* Get user name
35+
*
36+
* @return string
37+
*/
38+
public function getUserName()
39+
{
40+
return $this->helper->getPostValue('name') ?: $this->helper->getUserName();
41+
}
42+
43+
/**
44+
* Get user email
45+
*
46+
* @return string
47+
*/
48+
public function getUserEmail()
49+
{
50+
return $this->helper->getPostValue('email') ?: $this->helper->getUserEmail();
51+
}
52+
53+
/**
54+
* Get user telephone
55+
*
56+
* @return string
57+
*/
58+
public function getUserTelephone()
59+
{
60+
return $this->helper->getPostValue('telephone');
61+
}
62+
63+
/**
64+
* Get user comment
65+
*
66+
* @return string
67+
*/
68+
public function getUserComment()
69+
{
70+
return $this->helper->getPostValue('comment');
71+
}
72+
}

app/code/Magento/Contact/view/frontend/layout/contact_index_index.xml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,9 @@
1313
<referenceContainer name="content">
1414
<block class="Magento\Contact\Block\ContactForm" name="contactForm" template="Magento_Contact::form.phtml">
1515
<container name="form.additional.info" label="Form Additional Info"/>
16+
<arguments>
17+
<argument name="view_model" xsi:type="object">Magento\Contact\ViewModel\UserDataProvider</argument>
18+
</arguments>
1619
</block>
1720
</referenceContainer>
1821
</body>

app/code/Magento/Contact/view/frontend/templates/form.phtml

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@
66

77
// @codingStandardsIgnoreFile
88
/** @var \Magento\Contact\Block\ContactForm $block */
9+
/** @var \Magento\Contact\ViewModel\UserDataProvider $viewModel */
10+
11+
$viewModel = $block->getViewModel();
912
?>
1013
<form class="form contact"
1114
action="<?= $block->escapeUrl($block->getFormAction()) ?>"
@@ -19,25 +22,25 @@
1922
<div class="field name required">
2023
<label class="label" for="name"><span><?= $block->escapeHtml(__('Name')) ?></span></label>
2124
<div class="control">
22-
<input name="name" id="name" title="<?= $block->escapeHtmlAttr(__('Name')) ?>" value="<?= $block->escapeHtmlAttr($this->helper('Magento\Contact\Helper\Data')->getPostValue('name') ?: $this->helper('Magento\Contact\Helper\Data')->getUserName()) ?>" class="input-text" type="text" data-validate="{required:true}"/>
25+
<input name="name" id="name" title="<?= $block->escapeHtmlAttr(__('Name')) ?>" value="<?= $block->escapeHtmlAttr($viewModel->getUserName()) ?>" class="input-text" type="text" data-validate="{required:true}"/>
2326
</div>
2427
</div>
2528
<div class="field email required">
2629
<label class="label" for="email"><span><?= $block->escapeHtml(__('Email')) ?></span></label>
2730
<div class="control">
28-
<input name="email" id="email" title="<?= $block->escapeHtmlAttr(__('Email')) ?>" value="<?= $block->escapeHtmlAttr($this->helper('Magento\Contact\Helper\Data')->getPostValue('email') ?: $this->helper('Magento\Contact\Helper\Data')->getUserEmail()) ?>" class="input-text" type="email" data-validate="{required:true, 'validate-email':true}"/>
31+
<input name="email" id="email" title="<?= $block->escapeHtmlAttr(__('Email')) ?>" value="<?= $block->escapeHtmlAttr($viewModel->getUserEmail()) ?>" class="input-text" type="email" data-validate="{required:true, 'validate-email':true}"/>
2932
</div>
3033
</div>
3134
<div class="field telephone">
3235
<label class="label" for="telephone"><span><?= $block->escapeHtml(__('Phone Number')) ?></span></label>
3336
<div class="control">
34-
<input name="telephone" id="telephone" title="<?= $block->escapeHtmlAttr(__('Phone Number')) ?>" value="<?= $block->escapeHtmlAttr($this->helper('Magento\Contact\Helper\Data')->getPostValue('telephone')) ?>" class="input-text" type="text" />
37+
<input name="telephone" id="telephone" title="<?= $block->escapeHtmlAttr(__('Phone Number')) ?>" value="<?= $block->escapeHtmlAttr($viewModel->getUserTelephone()) ?>" class="input-text" type="text" />
3538
</div>
3639
</div>
3740
<div class="field comment required">
3841
<label class="label" for="comment"><span><?= $block->escapeHtml(__('What’s on your mind?')) ?></span></label>
3942
<div class="control">
40-
<textarea name="comment" id="comment" title="<?= $block->escapeHtmlAttr(__('What’s on your mind?')) ?>" class="input-text" cols="5" rows="3" data-validate="{required:true}"><?= $block->escapeHtml($this->helper('Magento\Contact\Helper\Data')->getPostValue('comment')) ?></textarea>
43+
<textarea name="comment" id="comment" title="<?= $block->escapeHtmlAttr(__('What’s on your mind?')) ?>" class="input-text" cols="5" rows="3" data-validate="{required:true}"><?= $block->escapeHtml($viewModel->getUserComment()) ?></textarea>
4144
</div>
4245
</div>
4346
<?= $block->getChildHtml('form.additional.info') ?>

0 commit comments

Comments
 (0)