Name.php 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Customer\Block\Widget;
  7. use Magento\Customer\Api\AddressMetadataInterface;
  8. use Magento\Customer\Api\CustomerMetadataInterface;
  9. use Magento\Customer\Api\Data\CustomerInterface;
  10. use Magento\Customer\Helper\Address as AddressHelper;
  11. use Magento\Customer\Model\Options;
  12. use Magento\Framework\View\Element\Template\Context;
  13. /**
  14. * Widget for showing customer name.
  15. *
  16. * @method CustomerInterface getObject()
  17. * @method Name setObject(CustomerInterface $customer)
  18. *
  19. * @SuppressWarnings(PHPMD.DepthOfInheritance)
  20. */
  21. class Name extends AbstractWidget
  22. {
  23. /**
  24. * @var AddressMetadataInterface
  25. */
  26. protected $addressMetadata;
  27. /**
  28. * @var Options
  29. */
  30. protected $options;
  31. /**
  32. * @param Context $context
  33. * @param AddressHelper $addressHelper
  34. * @param CustomerMetadataInterface $customerMetadata
  35. * @param Options $options
  36. * @param AddressMetadataInterface $addressMetadata
  37. * @param array $data
  38. */
  39. public function __construct(
  40. Context $context,
  41. AddressHelper $addressHelper,
  42. CustomerMetadataInterface $customerMetadata,
  43. Options $options,
  44. AddressMetadataInterface $addressMetadata,
  45. array $data = []
  46. ) {
  47. $this->options = $options;
  48. parent::__construct($context, $addressHelper, $customerMetadata, $data);
  49. $this->addressMetadata = $addressMetadata;
  50. $this->_isScopePrivate = true;
  51. }
  52. /**
  53. * @inheritdoc
  54. */
  55. public function _construct()
  56. {
  57. parent::_construct();
  58. // default template location
  59. $this->setTemplate('Magento_Customer::widget/name.phtml');
  60. }
  61. /**
  62. * Can show config value
  63. *
  64. * @param string $key
  65. * @return bool
  66. */
  67. protected function _showConfig($key)
  68. {
  69. return (bool)$this->getConfig($key);
  70. }
  71. /**
  72. * Can show prefix
  73. *
  74. * @return bool
  75. */
  76. public function showPrefix()
  77. {
  78. return $this->_isAttributeVisible('prefix');
  79. }
  80. /**
  81. * Define if prefix attribute is required
  82. *
  83. * @return bool
  84. */
  85. public function isPrefixRequired()
  86. {
  87. return $this->_isAttributeRequired('prefix');
  88. }
  89. /**
  90. * Retrieve name prefix drop-down options
  91. *
  92. * @return array|bool
  93. */
  94. public function getPrefixOptions()
  95. {
  96. $prefixOptions = $this->options->getNamePrefixOptions();
  97. if ($this->getObject() && !empty($prefixOptions)) {
  98. $prefixOption = $this->getObject()->getPrefix();
  99. $oldPrefix = $this->escapeHtml(trim($prefixOption));
  100. if ($prefixOption !== null && !isset($prefixOptions[$oldPrefix]) && !isset($prefixOptions[$prefixOption])) {
  101. $prefixOptions[$oldPrefix] = $oldPrefix;
  102. }
  103. }
  104. return $prefixOptions;
  105. }
  106. /**
  107. * Define if middle name attribute can be shown
  108. *
  109. * @return bool
  110. */
  111. public function showMiddlename()
  112. {
  113. return $this->_isAttributeVisible('middlename');
  114. }
  115. /**
  116. * Define if middlename attribute is required
  117. *
  118. * @return bool
  119. */
  120. public function isMiddlenameRequired()
  121. {
  122. return $this->_isAttributeRequired('middlename');
  123. }
  124. /**
  125. * Define if suffix attribute can be shown
  126. *
  127. * @return bool
  128. */
  129. public function showSuffix()
  130. {
  131. return $this->_isAttributeVisible('suffix');
  132. }
  133. /**
  134. * Define if suffix attribute is required
  135. *
  136. * @return bool
  137. */
  138. public function isSuffixRequired()
  139. {
  140. return $this->_isAttributeRequired('suffix');
  141. }
  142. /**
  143. * Retrieve name suffix drop-down options
  144. *
  145. * @return array|bool
  146. */
  147. public function getSuffixOptions()
  148. {
  149. $suffixOptions = $this->options->getNameSuffixOptions();
  150. if ($this->getObject() && !empty($suffixOptions)) {
  151. $suffixOption = $this->getObject()->getSuffix();
  152. $oldSuffix = $this->escapeHtml(trim($suffixOption));
  153. if ($suffixOption !== null && !isset($suffixOptions[$oldSuffix]) && !isset($suffixOptions[$suffixOption])) {
  154. $suffixOptions[$oldSuffix] = $oldSuffix;
  155. }
  156. }
  157. return $suffixOptions;
  158. }
  159. /**
  160. * Class name getter
  161. *
  162. * @return string
  163. */
  164. public function getClassName()
  165. {
  166. if (!$this->hasData('class_name')) {
  167. $this->setData('class_name', 'customer-name');
  168. }
  169. return $this->getData('class_name');
  170. }
  171. /**
  172. * Container class name getter
  173. *
  174. * @return string
  175. */
  176. public function getContainerClassName()
  177. {
  178. $class = $this->getClassName();
  179. $class .= $this->showPrefix() ? '-prefix' : '';
  180. $class .= $this->showMiddlename() ? '-middlename' : '';
  181. $class .= $this->showSuffix() ? '-suffix' : '';
  182. return $class;
  183. }
  184. /**
  185. * @inheritdoc
  186. */
  187. protected function _getAttribute($attributeCode)
  188. {
  189. if ($this->getForceUseCustomerAttributes() || $this->getObject() instanceof CustomerInterface) {
  190. return parent::_getAttribute($attributeCode);
  191. }
  192. try {
  193. $attribute = $this->addressMetadata->getAttributeMetadata($attributeCode);
  194. } catch (\Magento\Framework\Exception\NoSuchEntityException $e) {
  195. return null;
  196. }
  197. if ($this->getForceUseCustomerRequiredAttributes() && $attribute && !$attribute->isRequired()) {
  198. $customerAttribute = parent::_getAttribute($attributeCode);
  199. if ($customerAttribute && $customerAttribute->isRequired()) {
  200. $attribute = $customerAttribute;
  201. }
  202. }
  203. return $attribute;
  204. }
  205. /**
  206. * Retrieve store attribute label
  207. *
  208. * @param string $attributeCode
  209. * @return string
  210. */
  211. public function getStoreLabel($attributeCode)
  212. {
  213. $attribute = $this->_getAttribute($attributeCode);
  214. return $attribute ? __($attribute->getStoreLabel()) : '';
  215. }
  216. /**
  217. * Get string with frontend validation classes for attribute
  218. *
  219. * @param string $attributeCode
  220. * @return string
  221. */
  222. public function getAttributeValidationClass($attributeCode)
  223. {
  224. $attributeMetadata = $this->_getAttribute($attributeCode);
  225. return $attributeMetadata ? $attributeMetadata->getFrontendClass() : '';
  226. }
  227. /**
  228. * Check if attribute is required
  229. *
  230. * @param string $attributeCode
  231. * @return bool
  232. */
  233. private function _isAttributeRequired($attributeCode)
  234. {
  235. $attributeMetadata = $this->_getAttribute($attributeCode);
  236. return $attributeMetadata ? (bool)$attributeMetadata->isRequired() : false;
  237. }
  238. /**
  239. * Check if attribute is visible
  240. *
  241. * @param string $attributeCode
  242. * @return bool
  243. */
  244. private function _isAttributeVisible($attributeCode)
  245. {
  246. $attributeMetadata = $this->_getAttribute($attributeCode);
  247. return $attributeMetadata ? (bool)$attributeMetadata->isVisible() : false;
  248. }
  249. }