Skip to content

Commit db12db0

Browse files
ENGCOM-6335: Contact form > Adding ViewModel #25523
2 parents 8b0f7fa + 1750615 commit db12db0

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
@@ -5,6 +5,9 @@
55
*/
66

77
/** @var \Magento\Contact\Block\ContactForm $block */
8+
/** @var \Magento\Contact\ViewModel\UserDataProvider $viewModel */
9+
10+
$viewModel = $block->getViewModel();
811
?>
912
<form class="form contact"
1013
action="<?= $block->escapeUrl($block->getFormAction()) ?>"
@@ -18,25 +21,25 @@
1821
<div class="field name required">
1922
<label class="label" for="name"><span><?= $block->escapeHtml(__('Name')) ?></span></label>
2023
<div class="control">
21-
<input name="name" id="name" title="<?= $block->escapeHtmlAttr(__('Name')) ?>" value="<?= $block->escapeHtmlAttr($this->helper(\Magento\Contact\Helper\Data::class)->getPostValue('name') ?: $this->helper(\Magento\Contact\Helper\Data::class)->getUserName()) ?>" class="input-text" type="text" data-validate="{required:true}"/>
24+
<input name="name" id="name" title="<?= $block->escapeHtmlAttr(__('Name')) ?>" value="<?= $block->escapeHtmlAttr($viewModel->getUserName()) ?>" class="input-text" type="text" data-validate="{required:true}"/>
2225
</div>
2326
</div>
2427
<div class="field email required">
2528
<label class="label" for="email"><span><?= $block->escapeHtml(__('Email')) ?></span></label>
2629
<div class="control">
27-
<input name="email" id="email" title="<?= $block->escapeHtmlAttr(__('Email')) ?>" value="<?= $block->escapeHtmlAttr($this->helper(\Magento\Contact\Helper\Data::class)->getPostValue('email') ?: $this->helper(\Magento\Contact\Helper\Data::class)->getUserEmail()) ?>" class="input-text" type="email" data-validate="{required:true, 'validate-email':true}"/>
30+
<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}"/>
2831
</div>
2932
</div>
3033
<div class="field telephone">
3134
<label class="label" for="telephone"><span><?= $block->escapeHtml(__('Phone Number')) ?></span></label>
3235
<div class="control">
33-
<input name="telephone" id="telephone" title="<?= $block->escapeHtmlAttr(__('Phone Number')) ?>" value="<?= $block->escapeHtmlAttr($this->helper(\Magento\Contact\Helper\Data::class)->getPostValue('telephone')) ?>" class="input-text" type="text" />
36+
<input name="telephone" id="telephone" title="<?= $block->escapeHtmlAttr(__('Phone Number')) ?>" value="<?= $block->escapeHtmlAttr($viewModel->getUserTelephone()) ?>" class="input-text" type="text" />
3437
</div>
3538
</div>
3639
<div class="field comment required">
3740
<label class="label" for="comment"><span><?= $block->escapeHtml(__('What’s on your mind?')) ?></span></label>
3841
<div class="control">
39-
<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::class)->getPostValue('comment')) ?></textarea>
42+
<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>
4043
</div>
4144
</div>
4245
<?= $block->getChildHtml('form.additional.info') ?>

0 commit comments

Comments
 (0)