Skip to content

Commit

Permalink
7241 Always add empty option for prefix and/or suffix if optional
Browse files Browse the repository at this point in the history
  • Loading branch information
avstudnitz committed Oct 14, 2017
1 parent 08ec8ce commit 11e38f6
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 10 deletions.
10 changes: 7 additions & 3 deletions app/code/Magento/Config/Model/Config/Source/Nooptreq.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,19 @@
*/
class Nooptreq implements \Magento\Framework\Option\ArrayInterface
{
const VALUE_NO = '';
const VALUE_OPTIONAL = 'opt';
const VALUE_REQUIRED = 'req';

/**
* @return array
*/
public function toOptionArray()
{
return [
['value' => '', 'label' => __('No')],
['value' => 'opt', 'label' => __('Optional')],
['value' => 'req', 'label' => __('Required')]
['value' => self::VALUE_NO, 'label' => __('No')],
['value' => self::VALUE_OPTIONAL, 'label' => __('Optional')],
['value' => self::VALUE_REQUIRED, 'label' => __('Required')]
];
}
}
18 changes: 15 additions & 3 deletions app/code/Magento/Customer/Model/Options.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
*/
namespace Magento\Customer\Model;

use Magento\Config\Model\Config\Source\Nooptreq as NooptreqSource;
use Magento\Customer\Helper\Address as AddressHelper;
use Magento\Framework\Escaper;

Expand Down Expand Up @@ -42,7 +43,10 @@ public function __construct(
*/
public function getNamePrefixOptions($store = null)
{
return $this->_prepareNamePrefixSuffixOptions($this->addressHelper->getConfig('prefix_options', $store));
return $this->prepareNamePrefixSuffixOptions(
$this->addressHelper->getConfig('prefix_options', $store),
$this->addressHelper->getConfig('prefix_show', $store) == NooptreqSource::VALUE_OPTIONAL
);
}

/**
Expand All @@ -53,23 +57,31 @@ public function getNamePrefixOptions($store = null)
*/
public function getNameSuffixOptions($store = null)
{
return $this->_prepareNamePrefixSuffixOptions($this->addressHelper->getConfig('suffix_options', $store));
return $this->prepareNamePrefixSuffixOptions(
$this->addressHelper->getConfig('suffix_options', $store),
$this->addressHelper->getConfig('suffix_show', $store) == NooptreqSource::VALUE_OPTIONAL
);
}

/**
* Unserialize and clear name prefix or suffix options
* If field is optional, add an empty first option.
*
* @param string $options
* @param bool $isOptional
* @return array|bool
*/
protected function _prepareNamePrefixSuffixOptions($options)
private function prepareNamePrefixSuffixOptions($options, $isOptional = false)
{
$options = trim($options);
if (empty($options)) {
return false;
}
$result = [];
$options = explode(';', $options);
if ($isOptional && trim(current($options))) {
array_unshift($options, '');
}
foreach ($options as $value) {
$value = $this->escaper->escapeHtml(trim($value));
$result[$value] = $value;
Expand Down
4 changes: 2 additions & 2 deletions app/code/Magento/Customer/etc/adminhtml/system.xml
Original file line number Diff line number Diff line change
Expand Up @@ -209,7 +209,7 @@
<field id="prefix_options" translate="label comment" sortOrder="30" showInDefault="1" showInWebsite="1" showInStore="0">
<label>Prefix Dropdown Options</label>
<comment>
<![CDATA[Semicolon (;) separated values.<br/>Put semicolon in the beginning for empty first option.<br/>Leave empty for open text field.]]>
<![CDATA[Semicolon (;) separated values.<br/>Leave empty for open text field.]]>
</comment>
</field>
<field id="middlename_show" translate="label comment" type="select" sortOrder="40" showInDefault="1" showInWebsite="1" showInStore="0">
Expand All @@ -227,7 +227,7 @@
<field id="suffix_options" translate="label comment" sortOrder="60" showInDefault="1" showInWebsite="1" showInStore="0">
<label>Suffix Dropdown Options</label>
<comment>
<![CDATA[Semicolon (;) separated values.<br/>Put semicolon in the beginning for empty first option.<br/>Leave empty for open text field.]]>
<![CDATA[Semicolon (;) separated values.<br/>Leave empty for open text field.]]>
</comment>
</field>
<field id="dob_show" translate="label" type="select" sortOrder="70" showInDefault="1" showInWebsite="1" showInStore="0">
Expand Down
4 changes: 2 additions & 2 deletions app/code/Magento/Customer/i18n/en_US.csv
Original file line number Diff line number Diff line change
Expand Up @@ -479,9 +479,9 @@ Strong,Strong
"The title that goes before name (Mr., Mrs., etc.)","The title that goes before name (Mr., Mrs., etc.)"
"Prefix Dropdown Options","Prefix Dropdown Options"
"
Semicolon (;) separated values.<br/>Put semicolon in the beginning for empty first option.<br/>Leave empty for open text field.
Semicolon (;) separated values.<br/>Leave empty for open text field.
","
Semicolon (;) separated values.<br/>Put semicolon in the beginning for empty first option.<br/>Leave empty for open text field.
Semicolon (;) separated values.<br/>Leave empty for open text field.
"
"Show Middle Name (initial)","Show Middle Name (initial)"
"Always optional.","Always optional."
Expand Down

0 comments on commit 11e38f6

Please sign in to comment.