55 */
66namespace Magento \Customer \Model ;
77
8+ use Magento \Config \Model \Config \Source \Nooptreq as NooptreqSource ;
89use Magento \Customer \Helper \Address as AddressHelper ;
910use 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,16 +57,34 @@ 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+ );
64+ }
65+
66+ /**
67+ * @param $options
68+ * @param bool $isOptional
69+ * @return array|bool
70+ *
71+ * @deprecated
72+ * @see prepareNamePrefixSuffixOptions()
73+ */
74+ protected function _prepareNamePrefixSuffixOptions ($ options , $ isOptional = false )
75+ {
76+ return $ this ->prepareNamePrefixSuffixOptions ($ options , $ isOptional );
5777 }
5878
5979 /**
6080 * Unserialize and clear name prefix or suffix options
81+ * If field is optional, add an empty first option.
6182 *
6283 * @param string $options
84+ * @param bool $isOptional
6385 * @return array|bool
6486 */
65- protected function _prepareNamePrefixSuffixOptions ($ options )
87+ private function prepareNamePrefixSuffixOptions ($ options, $ isOptional = false )
6688 {
6789 $ options = trim ($ options );
6890 if (empty ($ options )) {
@@ -74,6 +96,10 @@ protected function _prepareNamePrefixSuffixOptions($options)
7496 $ value = $ this ->escaper ->escapeHtml (trim ($ value ));
7597 $ result [$ value ] = $ value ;
7698 }
99+ if ($ isOptional && trim (current ($ options ))) {
100+ $ result = array_merge ([' ' => ' ' ], $ result );
101+ }
102+
77103 return $ result ;
78104 }
79105}
0 commit comments