From 11e38f68a9956cbdc1d7c48284bc712729f43a90 Mon Sep 17 00:00:00 2001 From: Andreas von Studnitz Date: Sat, 14 Oct 2017 11:46:28 +0200 Subject: [PATCH 1/6] 7241 Always add empty option for prefix and/or suffix if optional --- .../Config/Model/Config/Source/Nooptreq.php | 10 +++++++--- app/code/Magento/Customer/Model/Options.php | 18 +++++++++++++++--- .../Magento/Customer/etc/adminhtml/system.xml | 4 ++-- app/code/Magento/Customer/i18n/en_US.csv | 4 ++-- 4 files changed, 26 insertions(+), 10 deletions(-) diff --git a/app/code/Magento/Config/Model/Config/Source/Nooptreq.php b/app/code/Magento/Config/Model/Config/Source/Nooptreq.php index 03fe5ca2abccc..1c9eb801dfec7 100644 --- a/app/code/Magento/Config/Model/Config/Source/Nooptreq.php +++ b/app/code/Magento/Config/Model/Config/Source/Nooptreq.php @@ -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')] ]; } } diff --git a/app/code/Magento/Customer/Model/Options.php b/app/code/Magento/Customer/Model/Options.php index ee109dac08104..bdd5fd9fd1137 100644 --- a/app/code/Magento/Customer/Model/Options.php +++ b/app/code/Magento/Customer/Model/Options.php @@ -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; @@ -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 + ); } /** @@ -53,16 +57,21 @@ 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)) { @@ -70,6 +79,9 @@ protected function _prepareNamePrefixSuffixOptions($options) } $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; diff --git a/app/code/Magento/Customer/etc/adminhtml/system.xml b/app/code/Magento/Customer/etc/adminhtml/system.xml index daf9240677588..46d7f589022b8 100644 --- a/app/code/Magento/Customer/etc/adminhtml/system.xml +++ b/app/code/Magento/Customer/etc/adminhtml/system.xml @@ -209,7 +209,7 @@ - Put semicolon in the beginning for empty first option.
Leave empty for open text field.]]> + Leave empty for open text field.]]>
@@ -227,7 +227,7 @@ - Put semicolon in the beginning for empty first option.
Leave empty for open text field.]]> + Leave empty for open text field.]]>
diff --git a/app/code/Magento/Customer/i18n/en_US.csv b/app/code/Magento/Customer/i18n/en_US.csv index aececbf6deeb4..8768426b5c949 100644 --- a/app/code/Magento/Customer/i18n/en_US.csv +++ b/app/code/Magento/Customer/i18n/en_US.csv @@ -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.
Put semicolon in the beginning for empty first option.
Leave empty for open text field. + Semicolon (;) separated values.
Leave empty for open text field. "," - Semicolon (;) separated values.
Put semicolon in the beginning for empty first option.
Leave empty for open text field. + Semicolon (;) separated values.
Leave empty for open text field. " "Show Middle Name (initial)","Show Middle Name (initial)" "Always optional.","Always optional." From 7800e5dd8aac704da3f2b1799baadf2a01269c61 Mon Sep 17 00:00:00 2001 From: Andreas von Studnitz Date: Wed, 15 Nov 2017 11:22:02 +0100 Subject: [PATCH 2/6] 7241 Re-add removed protected method and mark it as deprecated This is due to Backwards Compatibility reasons. --- app/code/Magento/Customer/Model/Options.php | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/app/code/Magento/Customer/Model/Options.php b/app/code/Magento/Customer/Model/Options.php index bdd5fd9fd1137..23a8c87a84ba4 100644 --- a/app/code/Magento/Customer/Model/Options.php +++ b/app/code/Magento/Customer/Model/Options.php @@ -63,6 +63,18 @@ public function getNameSuffixOptions($store = null) ); } + /** + * @param $options + * @param bool $isOptional + * @return array|bool + * + * @deprecated See prepareNamePrefixSuffixOptions + */ + protected function _prepareNamePrefixSuffixOptions($options, $isOptional = false) + { + return $this->prepareNamePrefixSuffixOptions($options, $isOptional); + } + /** * Unserialize and clear name prefix or suffix options * If field is optional, add an empty first option. From 519b805c2853f895d804c49e08bd7a23b108c38c Mon Sep 17 00:00:00 2001 From: Andreas von Studnitz Date: Fri, 1 Dec 2017 08:52:44 +0100 Subject: [PATCH 3/6] 7241 Change syntax of @see annotation for depracated method --- app/code/Magento/Customer/Model/Options.php | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/app/code/Magento/Customer/Model/Options.php b/app/code/Magento/Customer/Model/Options.php index 23a8c87a84ba4..b074adf39004f 100644 --- a/app/code/Magento/Customer/Model/Options.php +++ b/app/code/Magento/Customer/Model/Options.php @@ -68,7 +68,8 @@ public function getNameSuffixOptions($store = null) * @param bool $isOptional * @return array|bool * - * @deprecated See prepareNamePrefixSuffixOptions + * @deprecated + * @see prepareNamePrefixSuffixOptions() */ protected function _prepareNamePrefixSuffixOptions($options, $isOptional = false) { From eb79b38beb6b79411fe95d30e324d7d094ab5874 Mon Sep 17 00:00:00 2001 From: Andreas von Studnitz Date: Fri, 1 Dec 2017 08:56:59 +0100 Subject: [PATCH 4/6] 7241 Undo creation of constants due to @api reasons See https://github.com/magento/magento2/pull/11462#pullrequestreview-76828329 --- .../Magento/Config/Model/Config/Source/Nooptreq.php | 10 +++------- app/code/Magento/Customer/Model/Options.php | 4 ++-- 2 files changed, 5 insertions(+), 9 deletions(-) diff --git a/app/code/Magento/Config/Model/Config/Source/Nooptreq.php b/app/code/Magento/Config/Model/Config/Source/Nooptreq.php index 1c9eb801dfec7..03fe5ca2abccc 100644 --- a/app/code/Magento/Config/Model/Config/Source/Nooptreq.php +++ b/app/code/Magento/Config/Model/Config/Source/Nooptreq.php @@ -11,19 +11,15 @@ */ 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' => self::VALUE_NO, 'label' => __('No')], - ['value' => self::VALUE_OPTIONAL, 'label' => __('Optional')], - ['value' => self::VALUE_REQUIRED, 'label' => __('Required')] + ['value' => '', 'label' => __('No')], + ['value' => 'opt', 'label' => __('Optional')], + ['value' => 'req', 'label' => __('Required')] ]; } } diff --git a/app/code/Magento/Customer/Model/Options.php b/app/code/Magento/Customer/Model/Options.php index b074adf39004f..b034e4fcf590d 100644 --- a/app/code/Magento/Customer/Model/Options.php +++ b/app/code/Magento/Customer/Model/Options.php @@ -45,7 +45,7 @@ public function getNamePrefixOptions($store = null) { return $this->prepareNamePrefixSuffixOptions( $this->addressHelper->getConfig('prefix_options', $store), - $this->addressHelper->getConfig('prefix_show', $store) == NooptreqSource::VALUE_OPTIONAL + $this->addressHelper->getConfig('prefix_show', $store) == 'opt' ); } @@ -59,7 +59,7 @@ public function getNameSuffixOptions($store = null) { return $this->prepareNamePrefixSuffixOptions( $this->addressHelper->getConfig('suffix_options', $store), - $this->addressHelper->getConfig('suffix_show', $store) == NooptreqSource::VALUE_OPTIONAL + $this->addressHelper->getConfig('suffix_show', $store) == 'opt' ); } From fce3afe6390be6c0eb2d5fcdb2f76108d6a44e77 Mon Sep 17 00:00:00 2001 From: Andreas von Studnitz Date: Fri, 1 Dec 2017 10:55:17 +0100 Subject: [PATCH 5/6] 7241 Revert commit eb79b38 and re-add constants See https://github.com/magento/magento2/pull/11462#issuecomment-348441645 --- .../Magento/Config/Model/Config/Source/Nooptreq.php | 10 +++++++--- app/code/Magento/Customer/Model/Options.php | 4 ++-- 2 files changed, 9 insertions(+), 5 deletions(-) diff --git a/app/code/Magento/Config/Model/Config/Source/Nooptreq.php b/app/code/Magento/Config/Model/Config/Source/Nooptreq.php index 03fe5ca2abccc..1c9eb801dfec7 100644 --- a/app/code/Magento/Config/Model/Config/Source/Nooptreq.php +++ b/app/code/Magento/Config/Model/Config/Source/Nooptreq.php @@ -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')] ]; } } diff --git a/app/code/Magento/Customer/Model/Options.php b/app/code/Magento/Customer/Model/Options.php index b034e4fcf590d..b074adf39004f 100644 --- a/app/code/Magento/Customer/Model/Options.php +++ b/app/code/Magento/Customer/Model/Options.php @@ -45,7 +45,7 @@ public function getNamePrefixOptions($store = null) { return $this->prepareNamePrefixSuffixOptions( $this->addressHelper->getConfig('prefix_options', $store), - $this->addressHelper->getConfig('prefix_show', $store) == 'opt' + $this->addressHelper->getConfig('prefix_show', $store) == NooptreqSource::VALUE_OPTIONAL ); } @@ -59,7 +59,7 @@ public function getNameSuffixOptions($store = null) { return $this->prepareNamePrefixSuffixOptions( $this->addressHelper->getConfig('suffix_options', $store), - $this->addressHelper->getConfig('suffix_show', $store) == 'opt' + $this->addressHelper->getConfig('suffix_show', $store) == NooptreqSource::VALUE_OPTIONAL ); } From 3e38118c1e7f92e77963a6ae4f42e66520e6f58f Mon Sep 17 00:00:00 2001 From: David Manners Date: Fri, 15 Dec 2017 10:45:24 +0000 Subject: [PATCH 6/6] magento/magento2#7241: No option to start with blank option for prefix and suffix in checkout. - replace usage of empty string for optional value with space so that the knockout.js picks up the optional values --- app/code/Magento/Customer/Block/Widget/Name.php | 14 ++++++++++---- app/code/Magento/Customer/Model/Options.php | 7 ++++--- 2 files changed, 14 insertions(+), 7 deletions(-) diff --git a/app/code/Magento/Customer/Block/Widget/Name.php b/app/code/Magento/Customer/Block/Widget/Name.php index ecd09319cd85e..35f3bbefb8f00 100644 --- a/app/code/Magento/Customer/Block/Widget/Name.php +++ b/app/code/Magento/Customer/Block/Widget/Name.php @@ -106,8 +106,11 @@ public function getPrefixOptions() $prefixOptions = $this->options->getNamePrefixOptions(); if ($this->getObject() && !empty($prefixOptions)) { - $oldPrefix = $this->escapeHtml(trim($this->getObject()->getPrefix())); - $prefixOptions[$oldPrefix] = $oldPrefix; + $prefixOption = $this->getObject()->getPrefix(); + $oldPrefix = $this->escapeHtml(trim($prefixOption)); + if ($prefixOption !== null && !isset($prefixOptions[$oldPrefix]) && !isset($prefixOptions[$prefixOption])) { + $prefixOptions[$oldPrefix] = $oldPrefix; + } } return $prefixOptions; } @@ -161,8 +164,11 @@ public function getSuffixOptions() { $suffixOptions = $this->options->getNameSuffixOptions(); if ($this->getObject() && !empty($suffixOptions)) { - $oldSuffix = $this->escapeHtml(trim($this->getObject()->getSuffix())); - $suffixOptions[$oldSuffix] = $oldSuffix; + $suffixOption = $this->getObject()->getSuffix(); + $oldSuffix = $this->escapeHtml(trim($suffixOption)); + if ($suffixOption !== null && !isset($suffixOptions[$oldSuffix]) && !isset($suffixOptions[$suffixOption])) { + $suffixOptions[$oldSuffix] = $oldSuffix; + } } return $suffixOptions; } diff --git a/app/code/Magento/Customer/Model/Options.php b/app/code/Magento/Customer/Model/Options.php index b074adf39004f..7747e309d82a6 100644 --- a/app/code/Magento/Customer/Model/Options.php +++ b/app/code/Magento/Customer/Model/Options.php @@ -92,13 +92,14 @@ private function prepareNamePrefixSuffixOptions($options, $isOptional = 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; } + if ($isOptional && trim(current($options))) { + $result = array_merge([' ' => ' '], $result); + } + return $result; } }