Advertisement
Guest User

Untitled

a guest
Oct 23rd, 2017
57
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 15.84 KB | None | 0 0
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace MyNamespaceCustomerAttributesPluginBlock;
  7.  
  8. use MagentoCustomerApiAddressMetadataInterface;
  9. use MagentoCustomerApiCustomerMetadataInterface;
  10. use MagentoCustomerApiDataCustomerInterface;
  11. use MagentoCustomerHelperAddress as AddressHelper;
  12. use MagentoCustomerModelOptions;
  13. use MagentoFrameworkViewElementTemplateContext;
  14. use MagentoCustomerBlockWidgetAbstractWidget;
  15.  
  16. /**
  17. * Widget for showing customer name.
  18. *
  19. * @method CustomerInterface getObject()
  20. * @method Name setObject(CustomerInterface $customer)
  21. *
  22. * @SuppressWarnings(PHPMD.DepthOfInheritance)
  23. */
  24. class Name extends AbstractWidget
  25. {
  26. /**
  27. * @var AddressMetadataInterface
  28. */
  29. protected $addressMetadata;
  30.  
  31. /**
  32. * @var Options
  33. */
  34. protected $options;
  35.  
  36. /**
  37. * @param Context $context
  38. * @param AddressHelper $addressHelper
  39. * @param CustomerMetadataInterface $customerMetadata
  40. * @param Options $options
  41. * @param AddressMetadataInterface $addressMetadata
  42. * @param array $data
  43. */
  44. public function __construct(
  45. Context $context,
  46. AddressHelper $addressHelper,
  47. CustomerMetadataInterface $customerMetadata,
  48. Options $options,
  49. AddressMetadataInterface $addressMetadata,
  50. array $data = []
  51. ) {
  52. $this->options = $options;
  53. parent::__construct($context, $addressHelper, $customerMetadata, $data);
  54. $this->addressMetadata = $addressMetadata;
  55. $this->_isScopePrivate = true;
  56. }
  57.  
  58. /**
  59. * @return void
  60. */
  61. public function _construct()
  62. {
  63. parent::_construct();
  64.  
  65. // default template location
  66. //$this->setTemplate('widget/name.phtml');
  67. }
  68.  
  69. /**
  70. * Can show config value
  71. *
  72. * @param string $key
  73. * @return bool
  74. */
  75. protected function _showConfig($key)
  76. {
  77. return (bool)$this->getConfig($key);
  78. }
  79.  
  80. /**
  81. * Can show prefix
  82. *
  83. * @return bool
  84. */
  85. public function showPrefix()
  86. {
  87. return $this->_isAttributeVisible('prefix');
  88. }
  89.  
  90. /**
  91. * Define if prefix attribute is required
  92. *
  93. * @return bool
  94. */
  95. public function isPrefixRequired()
  96. {
  97. return $this->_isAttributeRequired('prefix');
  98. }
  99.  
  100. /**
  101. * Retrieve name prefix drop-down options
  102. *
  103. * @return array|bool
  104. */
  105. public function getPrefixOptions()
  106. {
  107. $prefixOptions = $this->options->getNamePrefixOptions();
  108.  
  109. if ($this->getObject() && !empty($prefixOptions)) {
  110. $oldPrefix = $this->escapeHtml(trim($this->getObject()->getPrefix()));
  111. $prefixOptions[$oldPrefix] = $oldPrefix;
  112. }
  113. return $prefixOptions;
  114. }
  115.  
  116. /**
  117. * Define if middle name attribute can be shown
  118. *
  119. * @return bool
  120. */
  121. public function showMiddlename()
  122. {
  123. return $this->_isAttributeVisible('middlename');
  124. }
  125.  
  126. /**
  127. * Define if middlename attribute is required
  128. *
  129. * @return bool
  130. */
  131. public function isMiddlenameRequired()
  132. {
  133. return $this->_isAttributeRequired('middlename');
  134. }
  135.  
  136. /**
  137. * Define if suffix attribute can be shown
  138. *
  139. * @return bool
  140. */
  141. public function showSuffix()
  142. {
  143. return $this->_isAttributeVisible('suffix');
  144. }
  145.  
  146. /**
  147. * Define if suffix attribute is required
  148. *
  149. * @return bool
  150. */
  151. public function isSuffixRequired()
  152. {
  153. return $this->_isAttributeRequired('suffix');
  154. }
  155.  
  156. /**
  157. * Retrieve name suffix drop-down options
  158. *
  159. * @return array|bool
  160. */
  161. public function getSuffixOptions()
  162. {
  163. $suffixOptions = $this->options->getNameSuffixOptions();
  164. if ($this->getObject() && !empty($suffixOptions)) {
  165. $oldSuffix = $this->escapeHtml(trim($this->getObject()->getSuffix()));
  166. $suffixOptions[$oldSuffix] = $oldSuffix;
  167. }
  168. return $suffixOptions;
  169. }
  170.  
  171. /**
  172. * Class name getter
  173. *
  174. * @return string
  175. */
  176. public function getClassName()
  177. {
  178. if (!$this->hasData('class_name')) {
  179. $this->setData('class_name', 'customer-name');
  180. }
  181. return $this->getData('class_name');
  182. }
  183.  
  184. /**
  185. * Container class name getter
  186. *
  187. * @return string
  188. */
  189. public function getContainerClassName()
  190. {
  191. $class = $this->getClassName();
  192. $class .= $this->showPrefix() ? '-prefix' : '';
  193. $class .= $this->showMiddlename() ? '-middlename' : '';
  194. $class .= $this->showSuffix() ? '-suffix' : '';
  195. return $class;
  196. }
  197.  
  198. /**
  199. * {@inheritdoc}
  200. */
  201. protected function _getAttribute($attributeCode)
  202. {
  203. if ($this->getForceUseCustomerAttributes() || $this->getObject() instanceof CustomerInterface) {
  204. return parent::_getAttribute($attributeCode);
  205. }
  206.  
  207. try {
  208. $attribute = $this->addressMetadata->getAttributeMetadata($attributeCode);
  209. } catch (MagentoFrameworkExceptionNoSuchEntityException $e) {
  210. return null;
  211. }
  212.  
  213. if ($this->getForceUseCustomerRequiredAttributes() && $attribute && !$attribute->isRequired()) {
  214. $customerAttribute = parent::_getAttribute($attributeCode);
  215. if ($customerAttribute && $customerAttribute->isRequired()) {
  216. $attribute = $customerAttribute;
  217. }
  218. }
  219.  
  220. return $attribute;
  221. }
  222.  
  223. /**
  224. * Retrieve store attribute label
  225. *
  226. * @param string $attributeCode
  227. * @return string
  228. */
  229. public function getStoreLabel($attributeCode)
  230. {
  231. $attribute = $this->_getAttribute($attributeCode);
  232. return $attribute ? __($attribute->getStoreLabel()) : '';
  233. }
  234.  
  235. /**
  236. * Get string with frontend validation classes for attribute
  237. *
  238. * @param string $attributeCode
  239. * @return string
  240. */
  241. public function getAttributeValidationClass($attributeCode)
  242. {
  243. return $this->_addressHelper->getAttributeValidationClass($attributeCode);
  244. }
  245.  
  246. /**
  247. * @param string $attributeCode
  248. * @return bool
  249. */
  250. private function _isAttributeRequired($attributeCode)
  251. {
  252. $attributeMetadata = $this->_getAttribute($attributeCode);
  253. return $attributeMetadata ? (bool)$attributeMetadata->isRequired() : false;
  254. }
  255.  
  256. /**
  257. * @param string $attributeCode
  258. * @return bool
  259. */
  260. private function _isAttributeVisible($attributeCode)
  261. {
  262. $attributeMetadata = $this->_getAttribute($attributeCode);
  263. return $attributeMetadata ? (bool)$attributeMetadata->isVisible() : false;
  264. }
  265. }
  266.  
  267. <?xml version="1.0"?>
  268. <page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" layout="1column" xsi:noNamespaceSchemaLocation="../../../../../../../lib/internal/Magento/Framework/View/Layout/etc/page_configuration.xsd">
  269. <referenceBlock name="content">
  270. <block
  271. template="nameandcompany.phtml"
  272. class="MyNamespaceCustomAttributesBlockName"
  273. name="myNamespace_nameandcompany"/>
  274. </referenceBlock>
  275. </page>
  276.  
  277. <?php
  278. /**
  279. * Copyright © Magento, Inc. All rights reserved.
  280. * See COPYING.txt for license details.
  281. */
  282.  
  283. // @codingStandardsIgnoreFile
  284.  
  285. /** @var MagentoCustomerBlockWidgetName $block */
  286.  
  287. /*
  288. <?= $block->getLayout()->createBlock('MagentoCustomerBlockWidgetName')
  289. ->setObject($block->getAddress())
  290. ->toHtml() ?>
  291.  
  292. For checkout/onepage/shipping.phtml:
  293.  
  294. <?= $block->getLayout()->createBlock('MagentoCustomerBlockWidgetName')
  295. ->setObject($block->getAddress())
  296. ->setFieldIdFormat('shipping:%s')
  297. ->setFieldNameFormat('shipping[%s]')
  298. ->toHtml() ?>
  299. */
  300.  
  301. $prefix = $block->showPrefix();
  302. $middle = $block->showMiddlename();
  303. $suffix = $block->showSuffix();
  304. ?>
  305. <?php if (($prefix || $middle || $suffix) && !$block->getNoWrap()): ?>
  306. <div class="field required fullname <?= $block->escapeHtmlAttr($block->getContainerClassName()) ?>">
  307. <label for="<?= $block->escapeHtmlAttr($block->getFieldId('firstname')) ?>" class="label">
  308. <span><?= $block->escapeHtml(__('Name')) ?></span>
  309. </label>
  310. <div class="control">
  311. <fieldset class="fieldset fieldset-fullname">
  312. <div class="fields">
  313. <?php endif; ?>
  314.  
  315. <?php if ($prefix): ?>
  316. <div class="field field-name-prefix<?php if ($block->isPrefixRequired()) echo ' required' ?>">
  317. <label class="label" for="<?= $block->escapeHtmlAttr($block->getFieldId('prefix')) ?>">
  318. <span><?= $block->escapeHtml($block->getStoreLabel('prefix')) ?></span>
  319. </label>
  320.  
  321. <div class="control">
  322. <?php if ($block->getPrefixOptions() === false): ?>
  323. <input type="text" id="<?= $block->escapeHtmlAttr($block->getFieldId('prefix')) ?>"
  324. name="<?= $block->escapeHtmlAttr($block->getFieldName('prefix')) ?>"
  325. value="<?= $block->escapeHtmlAttr($block->getObject()->getPrefix()) ?>"
  326. title="<?= $block->escapeHtmlAttr($block->getStoreLabel('prefix')) ?>"
  327. class="input-text <?= $block->escapeHtmlAttr($block->getAttributeValidationClass('prefix')) ?>" <?php if ($block->isPrefixRequired()) echo ' data-validate="{required:true}"' ?>>
  328. <?php else: ?>
  329. <select id="<?= $block->escapeHtmlAttr($block->getFieldId('prefix')) ?>"
  330. name="<?= $block->escapeHtmlAttr($block->getFieldName('prefix')) ?>"
  331. title="<?= $block->escapeHtmlAttr($block->getStoreLabel('prefix')) ?>"
  332. class="<?= $block->escapeHtmlAttr($block->getAttributeValidationClass('prefix')) ?>" <?php if ($block->isPrefixRequired()) echo ' data-validate="{required:true}"' ?> >
  333. <?php foreach ($block->getPrefixOptions() as $_option): ?>
  334. <option value="<?= $block->escapeHtmlAttr($_option) ?>"<?php if ($block->getObject()->getPrefix() == $_option): ?> selected="selected"<?php endif; ?>>
  335. <?= $block->escapeHtml(__($_option)) ?>
  336. </option>
  337. <?php endforeach; ?>
  338. </select>
  339. <?php endif; ?>
  340. </div>
  341. </div>
  342. <?php endif; ?>
  343. <div class="field field-name-firstname required">
  344. <label class="label" for="<?= $block->escapeHtmlAttr($block->getFieldId('firstname')) ?>">
  345. <span><?= $block->escapeHtml($block->getStoreLabel('firstname')) ?></span>
  346. </label>
  347.  
  348. <div class="control">
  349. <input type="text" id="<?= $block->escapeHtmlAttr($block->getFieldId('firstname')) ?>"
  350. name="<?= $block->escapeHtmlAttr($block->getFieldName('firstname')) ?>"
  351. value="<?= $block->escapeHtmlAttr($block->getObject()->getFirstname()) ?>"
  352. title="<?= $block->escapeHtmlAttr($block->getStoreLabel('firstname')) ?>"
  353. class="input-text <?= $block->escapeHtmlAttr($block->getAttributeValidationClass('firstname')) ?>" <?php if ($block->getAttributeValidationClass('firstname') == 'required-entry') echo ' data-validate="{required:true}"' ?>>
  354. </div>
  355. </div>
  356. <?php if ($middle): ?>
  357. <?php $isMiddlenameRequired = $block->isMiddlenameRequired(); ?>
  358. <div class="field field-name-middlename<?= $isMiddlenameRequired ? ' required' : '' ?>">
  359. <label class="label" for="<?= $block->escapeHtmlAttr($block->getFieldId('middlename')) ?>">
  360. <span><?= $block->escapeHtml($block->getStoreLabel('middlename')) ?></span>
  361. </label>
  362.  
  363. <div class="control">
  364. <input type="text" id="<?= $block->escapeHtmlAttr($block->getFieldId('middlename')) ?>"
  365. name="<?= $block->escapeHtmlAttr($block->getFieldName('middlename')) ?>"
  366. value="<?= $block->escapeHtmlAttr($block->getObject()->getMiddlename()) ?>"
  367. title="<?= $block->escapeHtmlAttr($block->getStoreLabel('middlename')) ?>"
  368. class="input-text <?= $block->escapeHtmlAttr($block->getAttributeValidationClass('middlename')) ?>" <?= $isMiddlenameRequired ? ' data-validate="{required:true}"' : '' ?>>
  369. </div>
  370. </div>
  371. <?php endif; ?>
  372. <div class="field field-name-lastname required">
  373. <label class="label" for="<?= $block->escapeHtmlAttr($block->getFieldId('lastname')) ?>">
  374. <span><?= $block->escapeHtml($block->getStoreLabel('lastname')) ?></span>
  375. </label>
  376.  
  377. <div class="control">
  378. <input type="text" id="<?= $block->escapeHtmlAttr($block->getFieldId('lastname')) ?>"
  379. name="<?= $block->escapeHtmlAttr($block->getFieldName('lastname')) ?>"
  380. value="<?= $block->escapeHtmlAttr($block->getObject()->getLastname()) ?>"
  381. title="<?= $block->escapeHtmlAttr($block->getStoreLabel('lastname')) ?>"
  382. class="input-text <?= $block->escapeHtmlAttr($block->getAttributeValidationClass('lastname')) ?>" <?php if ($block->getAttributeValidationClass('lastname') == 'required-entry') echo ' data-validate="{required:true}"' ?>>
  383. </div>
  384. </div>
  385. <?php if ($suffix): ?>
  386. <div class="field field-name-suffix<?php if ($block->isSuffixRequired()) echo ' required' ?>">
  387. <label class="label" for="<?= $block->escapeHtmlAttr($block->getFieldId('suffix')) ?>">
  388. <span><?= $block->escapeHtml($block->getStoreLabel('suffix')) ?></span>
  389. </label>
  390.  
  391. <div class="control">
  392. <?php if ($block->getSuffixOptions() === false): ?>
  393. <input type="text" id="<?= $block->escapeHtmlAttr($block->getFieldId('suffix')) ?>"
  394. name="<?= $block->escapeHtmlAttr($block->getFieldName('suffix')) ?>"
  395. value="<?= $block->escapeHtmlAttr($block->getObject()->getSuffix()) ?>"
  396. title="<?= $block->escapeHtmlAttr($block->getStoreLabel('suffix')) ?>"
  397. class="input-text <?= $block->escapeHtmlAttr($block->getAttributeValidationClass('suffix')) ?>" <?php if ($block->isSuffixRequired()) echo ' data-validate="{required:true}"' ?>>
  398. <?php else: ?>
  399. <select id="<?= $block->escapeHtmlAttr($block->getFieldId('suffix')) ?>"
  400. name="<?= $block->escapeHtmlAttr($block->getFieldName('suffix')) ?>"
  401. title="<?= $block->escapeHtmlAttr($block->getStoreLabel('suffix')) ?>"
  402. class="<?= $block->escapeHtmlAttr($block->getAttributeValidationClass('suffix')) ?>" <?php if ($block->isSuffixRequired()) echo ' data-validate="{required:true}"' ?>>
  403. <?php foreach ($block->getSuffixOptions() as $_option): ?>
  404. <option value="<?= $block->escapeHtmlAttr($_option) ?>"<?php if ($block->getObject()->getSuffix() == $_option): ?> selected="selected"<?php endif; ?>>
  405. <?= $block->escapeHtml(__($_option)) ?>
  406. </option>
  407. <?php endforeach; ?>
  408. </select>
  409. <?php endif; ?>
  410. </div>
  411. </div>
  412. <?php endif; ?>
  413.  
  414. <?php if (($prefix || $middle || $suffix) && !$block->getNoWrap()): ?>
  415. </div>
  416. </fieldset>
  417. </div>
  418. </div>
  419. <?php endif; ?>
  420.  
  421. <form class="form create account form-create-account" action="https://www.example.com/customer/account/createpost/" method="post" id="form-validate" enctype="multipart/form-data" autocomplete="off">
  422. <input name="form_key" type="hidden" value="TVEgWcjFb03Nzz1L" /> <fieldset class="fieldset create info">
  423. <legend class="legend"><span>Personal Information</span></legend><br>
  424. <input type="hidden" name="success_url" value="">
  425. <input type="hidden" name="error_url" value="">
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement