Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
Name.php
Go to the documentation of this file.
1 <?php
7 
11 use Magento\Customer\Helper\Address as AddressHelper;
14 
23 class Name extends AbstractWidget
24 {
28  protected $addressMetadata;
29 
33  protected $options;
34 
43  public function __construct(
44  Context $context,
45  AddressHelper $addressHelper,
49  array $data = []
50  ) {
51  $this->options = $options;
52  parent::__construct($context, $addressHelper, $customerMetadata, $data);
53  $this->addressMetadata = $addressMetadata;
54  $this->_isScopePrivate = true;
55  }
56 
60  public function _construct()
61  {
62  parent::_construct();
63 
64  // default template location
65  $this->setTemplate('Magento_Customer::widget/name.phtml');
66  }
67 
74  protected function _showConfig($key)
75  {
76  return (bool)$this->getConfig($key);
77  }
78 
84  public function showPrefix()
85  {
86  return $this->_isAttributeVisible('prefix');
87  }
88 
94  public function isPrefixRequired()
95  {
96  return $this->_isAttributeRequired('prefix');
97  }
98 
104  public function getPrefixOptions()
105  {
106  $prefixOptions = $this->options->getNamePrefixOptions();
107 
108  if ($this->getObject() && !empty($prefixOptions)) {
109  $prefixOption = $this->getObject()->getPrefix();
110  $oldPrefix = $this->escapeHtml(trim($prefixOption));
111  if ($prefixOption !== null && !isset($prefixOptions[$oldPrefix]) && !isset($prefixOptions[$prefixOption])) {
112  $prefixOptions[$oldPrefix] = $oldPrefix;
113  }
114  }
115  return $prefixOptions;
116  }
117 
123  public function showMiddlename()
124  {
125  return $this->_isAttributeVisible('middlename');
126  }
127 
133  public function isMiddlenameRequired()
134  {
135  return $this->_isAttributeRequired('middlename');
136  }
137 
143  public function showSuffix()
144  {
145  return $this->_isAttributeVisible('suffix');
146  }
147 
153  public function isSuffixRequired()
154  {
155  return $this->_isAttributeRequired('suffix');
156  }
157 
163  public function getSuffixOptions()
164  {
165  $suffixOptions = $this->options->getNameSuffixOptions();
166  if ($this->getObject() && !empty($suffixOptions)) {
167  $suffixOption = $this->getObject()->getSuffix();
168  $oldSuffix = $this->escapeHtml(trim($suffixOption));
169  if ($suffixOption !== null && !isset($suffixOptions[$oldSuffix]) && !isset($suffixOptions[$suffixOption])) {
170  $suffixOptions[$oldSuffix] = $oldSuffix;
171  }
172  }
173  return $suffixOptions;
174  }
175 
181  public function getClassName()
182  {
183  if (!$this->hasData('class_name')) {
184  $this->setData('class_name', 'customer-name');
185  }
186  return $this->getData('class_name');
187  }
188 
194  public function getContainerClassName()
195  {
196  $class = $this->getClassName();
197  $class .= $this->showPrefix() ? '-prefix' : '';
198  $class .= $this->showMiddlename() ? '-middlename' : '';
199  $class .= $this->showSuffix() ? '-suffix' : '';
200  return $class;
201  }
202 
206  protected function _getAttribute($attributeCode)
207  {
208  if ($this->getForceUseCustomerAttributes() || $this->getObject() instanceof CustomerInterface) {
209  return parent::_getAttribute($attributeCode);
210  }
211 
212  try {
213  $attribute = $this->addressMetadata->getAttributeMetadata($attributeCode);
214  } catch (\Magento\Framework\Exception\NoSuchEntityException $e) {
215  return null;
216  }
217 
218  if ($this->getForceUseCustomerRequiredAttributes() && $attribute && !$attribute->isRequired()) {
219  $customerAttribute = parent::_getAttribute($attributeCode);
220  if ($customerAttribute && $customerAttribute->isRequired()) {
221  $attribute = $customerAttribute;
222  }
223  }
224 
225  return $attribute;
226  }
227 
234  public function getStoreLabel($attributeCode)
235  {
237  return $attribute ? __($attribute->getStoreLabel()) : '';
238  }
239 
247  {
248  return $this->_addressHelper->getAttributeValidationClass($attributeCode);
249  }
250 
255  private function _isAttributeRequired($attributeCode)
256  {
257  $attributeMetadata = $this->_getAttribute($attributeCode);
258  return $attributeMetadata ? (bool)$attributeMetadata->isRequired() : false;
259  }
260 
265  private function _isAttributeVisible($attributeCode)
266  {
267  $attributeMetadata = $this->_getAttribute($attributeCode);
268  return $attributeMetadata ? (bool)$attributeMetadata->isVisible() : false;
269  }
270 }
getData($key='', $index=null)
Definition: DataObject.php:119
getAttributeValidationClass($attributeCode)
Definition: Name.php:246
getStoreLabel($attributeCode)
Definition: Name.php:234
__()
Definition: __.php:13
$_option $_optionId $class
Definition: date.phtml:13
$attributeCode
Definition: extend.phtml:12
setData($key, $value=null)
Definition: DataObject.php:72
__construct(Context $context, AddressHelper $addressHelper, CustomerMetadataInterface $customerMetadata, Options $options, AddressMetadataInterface $addressMetadata, array $data=[])
Definition: Name.php:43
_getAttribute($attributeCode)
Definition: Name.php:206