Skip to content

Commit 11e38f6

Browse files
committed
7241 Always add empty option for prefix and/or suffix if optional
1 parent 08ec8ce commit 11e38f6

File tree

4 files changed

+26
-10
lines changed

4 files changed

+26
-10
lines changed

app/code/Magento/Config/Model/Config/Source/Nooptreq.php

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,15 +11,19 @@
1111
*/
1212
class Nooptreq implements \Magento\Framework\Option\ArrayInterface
1313
{
14+
const VALUE_NO = '';
15+
const VALUE_OPTIONAL = 'opt';
16+
const VALUE_REQUIRED = 'req';
17+
1418
/**
1519
* @return array
1620
*/
1721
public function toOptionArray()
1822
{
1923
return [
20-
['value' => '', 'label' => __('No')],
21-
['value' => 'opt', 'label' => __('Optional')],
22-
['value' => 'req', 'label' => __('Required')]
24+
['value' => self::VALUE_NO, 'label' => __('No')],
25+
['value' => self::VALUE_OPTIONAL, 'label' => __('Optional')],
26+
['value' => self::VALUE_REQUIRED, 'label' => __('Required')]
2327
];
2428
}
2529
}

app/code/Magento/Customer/Model/Options.php

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
*/
66
namespace Magento\Customer\Model;
77

8+
use Magento\Config\Model\Config\Source\Nooptreq as NooptreqSource;
89
use Magento\Customer\Helper\Address as AddressHelper;
910
use Magento\Framework\Escaper;
1011

@@ -42,7 +43,10 @@ public function __construct(
4243
*/
4344
public function getNamePrefixOptions($store = null)
4445
{
45-
return $this->_prepareNamePrefixSuffixOptions($this->addressHelper->getConfig('prefix_options', $store));
46+
return $this->prepareNamePrefixSuffixOptions(
47+
$this->addressHelper->getConfig('prefix_options', $store),
48+
$this->addressHelper->getConfig('prefix_show', $store) == NooptreqSource::VALUE_OPTIONAL
49+
);
4650
}
4751

4852
/**
@@ -53,23 +57,31 @@ public function getNamePrefixOptions($store = null)
5357
*/
5458
public function getNameSuffixOptions($store = null)
5559
{
56-
return $this->_prepareNamePrefixSuffixOptions($this->addressHelper->getConfig('suffix_options', $store));
60+
return $this->prepareNamePrefixSuffixOptions(
61+
$this->addressHelper->getConfig('suffix_options', $store),
62+
$this->addressHelper->getConfig('suffix_show', $store) == NooptreqSource::VALUE_OPTIONAL
63+
);
5764
}
5865

5966
/**
6067
* Unserialize and clear name prefix or suffix options
68+
* If field is optional, add an empty first option.
6169
*
6270
* @param string $options
71+
* @param bool $isOptional
6372
* @return array|bool
6473
*/
65-
protected function _prepareNamePrefixSuffixOptions($options)
74+
private function prepareNamePrefixSuffixOptions($options, $isOptional = false)
6675
{
6776
$options = trim($options);
6877
if (empty($options)) {
6978
return false;
7079
}
7180
$result = [];
7281
$options = explode(';', $options);
82+
if ($isOptional && trim(current($options))) {
83+
array_unshift($options, '');
84+
}
7385
foreach ($options as $value) {
7486
$value = $this->escaper->escapeHtml(trim($value));
7587
$result[$value] = $value;

app/code/Magento/Customer/etc/adminhtml/system.xml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -209,7 +209,7 @@
209209
<field id="prefix_options" translate="label comment" sortOrder="30" showInDefault="1" showInWebsite="1" showInStore="0">
210210
<label>Prefix Dropdown Options</label>
211211
<comment>
212-
<![CDATA[Semicolon (;) separated values.<br/>Put semicolon in the beginning for empty first option.<br/>Leave empty for open text field.]]>
212+
<![CDATA[Semicolon (;) separated values.<br/>Leave empty for open text field.]]>
213213
</comment>
214214
</field>
215215
<field id="middlename_show" translate="label comment" type="select" sortOrder="40" showInDefault="1" showInWebsite="1" showInStore="0">
@@ -227,7 +227,7 @@
227227
<field id="suffix_options" translate="label comment" sortOrder="60" showInDefault="1" showInWebsite="1" showInStore="0">
228228
<label>Suffix Dropdown Options</label>
229229
<comment>
230-
<![CDATA[Semicolon (;) separated values.<br/>Put semicolon in the beginning for empty first option.<br/>Leave empty for open text field.]]>
230+
<![CDATA[Semicolon (;) separated values.<br/>Leave empty for open text field.]]>
231231
</comment>
232232
</field>
233233
<field id="dob_show" translate="label" type="select" sortOrder="70" showInDefault="1" showInWebsite="1" showInStore="0">

app/code/Magento/Customer/i18n/en_US.csv

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -479,9 +479,9 @@ Strong,Strong
479479
"The title that goes before name (Mr., Mrs., etc.)","The title that goes before name (Mr., Mrs., etc.)"
480480
"Prefix Dropdown Options","Prefix Dropdown Options"
481481
"
482-
Semicolon (;) separated values.<br/>Put semicolon in the beginning for empty first option.<br/>Leave empty for open text field.
482+
Semicolon (;) separated values.<br/>Leave empty for open text field.
483483
","
484-
Semicolon (;) separated values.<br/>Put semicolon in the beginning for empty first option.<br/>Leave empty for open text field.
484+
Semicolon (;) separated values.<br/>Leave empty for open text field.
485485
"
486486
"Show Middle Name (initial)","Show Middle Name (initial)"
487487
"Always optional.","Always optional."

0 commit comments

Comments
 (0)