Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- /**
- * Copyright © Magento, Inc. All rights reserved.
- * See COPYING.txt for license details.
- */
- namespace MyNamespaceCustomerAttributesPluginBlock;
- use MagentoCustomerApiAddressMetadataInterface;
- use MagentoCustomerApiCustomerMetadataInterface;
- use MagentoCustomerApiDataCustomerInterface;
- use MagentoCustomerHelperAddress as AddressHelper;
- use MagentoCustomerModelOptions;
- use MagentoFrameworkViewElementTemplateContext;
- use MagentoCustomerBlockWidgetAbstractWidget;
- /**
- * Widget for showing customer name.
- *
- * @method CustomerInterface getObject()
- * @method Name setObject(CustomerInterface $customer)
- *
- * @SuppressWarnings(PHPMD.DepthOfInheritance)
- */
- class Name extends AbstractWidget
- {
- /**
- * @var AddressMetadataInterface
- */
- protected $addressMetadata;
- /**
- * @var Options
- */
- protected $options;
- /**
- * @param Context $context
- * @param AddressHelper $addressHelper
- * @param CustomerMetadataInterface $customerMetadata
- * @param Options $options
- * @param AddressMetadataInterface $addressMetadata
- * @param array $data
- */
- public function __construct(
- Context $context,
- AddressHelper $addressHelper,
- CustomerMetadataInterface $customerMetadata,
- Options $options,
- AddressMetadataInterface $addressMetadata,
- array $data = []
- ) {
- $this->options = $options;
- parent::__construct($context, $addressHelper, $customerMetadata, $data);
- $this->addressMetadata = $addressMetadata;
- $this->_isScopePrivate = true;
- }
- /**
- * @return void
- */
- public function _construct()
- {
- parent::_construct();
- // default template location
- //$this->setTemplate('widget/name.phtml');
- }
- /**
- * Can show config value
- *
- * @param string $key
- * @return bool
- */
- protected function _showConfig($key)
- {
- return (bool)$this->getConfig($key);
- }
- /**
- * Can show prefix
- *
- * @return bool
- */
- public function showPrefix()
- {
- return $this->_isAttributeVisible('prefix');
- }
- /**
- * Define if prefix attribute is required
- *
- * @return bool
- */
- public function isPrefixRequired()
- {
- return $this->_isAttributeRequired('prefix');
- }
- /**
- * Retrieve name prefix drop-down options
- *
- * @return array|bool
- */
- public function getPrefixOptions()
- {
- $prefixOptions = $this->options->getNamePrefixOptions();
- if ($this->getObject() && !empty($prefixOptions)) {
- $oldPrefix = $this->escapeHtml(trim($this->getObject()->getPrefix()));
- $prefixOptions[$oldPrefix] = $oldPrefix;
- }
- return $prefixOptions;
- }
- /**
- * Define if middle name attribute can be shown
- *
- * @return bool
- */
- public function showMiddlename()
- {
- return $this->_isAttributeVisible('middlename');
- }
- /**
- * Define if middlename attribute is required
- *
- * @return bool
- */
- public function isMiddlenameRequired()
- {
- return $this->_isAttributeRequired('middlename');
- }
- /**
- * Define if suffix attribute can be shown
- *
- * @return bool
- */
- public function showSuffix()
- {
- return $this->_isAttributeVisible('suffix');
- }
- /**
- * Define if suffix attribute is required
- *
- * @return bool
- */
- public function isSuffixRequired()
- {
- return $this->_isAttributeRequired('suffix');
- }
- /**
- * Retrieve name suffix drop-down options
- *
- * @return array|bool
- */
- public function getSuffixOptions()
- {
- $suffixOptions = $this->options->getNameSuffixOptions();
- if ($this->getObject() && !empty($suffixOptions)) {
- $oldSuffix = $this->escapeHtml(trim($this->getObject()->getSuffix()));
- $suffixOptions[$oldSuffix] = $oldSuffix;
- }
- return $suffixOptions;
- }
- /**
- * Class name getter
- *
- * @return string
- */
- public function getClassName()
- {
- if (!$this->hasData('class_name')) {
- $this->setData('class_name', 'customer-name');
- }
- return $this->getData('class_name');
- }
- /**
- * Container class name getter
- *
- * @return string
- */
- public function getContainerClassName()
- {
- $class = $this->getClassName();
- $class .= $this->showPrefix() ? '-prefix' : '';
- $class .= $this->showMiddlename() ? '-middlename' : '';
- $class .= $this->showSuffix() ? '-suffix' : '';
- return $class;
- }
- /**
- * {@inheritdoc}
- */
- protected function _getAttribute($attributeCode)
- {
- if ($this->getForceUseCustomerAttributes() || $this->getObject() instanceof CustomerInterface) {
- return parent::_getAttribute($attributeCode);
- }
- try {
- $attribute = $this->addressMetadata->getAttributeMetadata($attributeCode);
- } catch (MagentoFrameworkExceptionNoSuchEntityException $e) {
- return null;
- }
- if ($this->getForceUseCustomerRequiredAttributes() && $attribute && !$attribute->isRequired()) {
- $customerAttribute = parent::_getAttribute($attributeCode);
- if ($customerAttribute && $customerAttribute->isRequired()) {
- $attribute = $customerAttribute;
- }
- }
- return $attribute;
- }
- /**
- * Retrieve store attribute label
- *
- * @param string $attributeCode
- * @return string
- */
- public function getStoreLabel($attributeCode)
- {
- $attribute = $this->_getAttribute($attributeCode);
- return $attribute ? __($attribute->getStoreLabel()) : '';
- }
- /**
- * Get string with frontend validation classes for attribute
- *
- * @param string $attributeCode
- * @return string
- */
- public function getAttributeValidationClass($attributeCode)
- {
- return $this->_addressHelper->getAttributeValidationClass($attributeCode);
- }
- /**
- * @param string $attributeCode
- * @return bool
- */
- private function _isAttributeRequired($attributeCode)
- {
- $attributeMetadata = $this->_getAttribute($attributeCode);
- return $attributeMetadata ? (bool)$attributeMetadata->isRequired() : false;
- }
- /**
- * @param string $attributeCode
- * @return bool
- */
- private function _isAttributeVisible($attributeCode)
- {
- $attributeMetadata = $this->_getAttribute($attributeCode);
- return $attributeMetadata ? (bool)$attributeMetadata->isVisible() : false;
- }
- }
- <?xml version="1.0"?>
- <page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" layout="1column" xsi:noNamespaceSchemaLocation="../../../../../../../lib/internal/Magento/Framework/View/Layout/etc/page_configuration.xsd">
- <referenceBlock name="content">
- <block
- template="nameandcompany.phtml"
- class="MyNamespaceCustomAttributesBlockName"
- name="myNamespace_nameandcompany"/>
- </referenceBlock>
- </page>
- <?php
- /**
- * Copyright © Magento, Inc. All rights reserved.
- * See COPYING.txt for license details.
- */
- // @codingStandardsIgnoreFile
- /** @var MagentoCustomerBlockWidgetName $block */
- /*
- <?= $block->getLayout()->createBlock('MagentoCustomerBlockWidgetName')
- ->setObject($block->getAddress())
- ->toHtml() ?>
- For checkout/onepage/shipping.phtml:
- <?= $block->getLayout()->createBlock('MagentoCustomerBlockWidgetName')
- ->setObject($block->getAddress())
- ->setFieldIdFormat('shipping:%s')
- ->setFieldNameFormat('shipping[%s]')
- ->toHtml() ?>
- */
- $prefix = $block->showPrefix();
- $middle = $block->showMiddlename();
- $suffix = $block->showSuffix();
- ?>
- <?php if (($prefix || $middle || $suffix) && !$block->getNoWrap()): ?>
- <div class="field required fullname <?= $block->escapeHtmlAttr($block->getContainerClassName()) ?>">
- <label for="<?= $block->escapeHtmlAttr($block->getFieldId('firstname')) ?>" class="label">
- <span><?= $block->escapeHtml(__('Name')) ?></span>
- </label>
- <div class="control">
- <fieldset class="fieldset fieldset-fullname">
- <div class="fields">
- <?php endif; ?>
- <?php if ($prefix): ?>
- <div class="field field-name-prefix<?php if ($block->isPrefixRequired()) echo ' required' ?>">
- <label class="label" for="<?= $block->escapeHtmlAttr($block->getFieldId('prefix')) ?>">
- <span><?= $block->escapeHtml($block->getStoreLabel('prefix')) ?></span>
- </label>
- <div class="control">
- <?php if ($block->getPrefixOptions() === false): ?>
- <input type="text" id="<?= $block->escapeHtmlAttr($block->getFieldId('prefix')) ?>"
- name="<?= $block->escapeHtmlAttr($block->getFieldName('prefix')) ?>"
- value="<?= $block->escapeHtmlAttr($block->getObject()->getPrefix()) ?>"
- title="<?= $block->escapeHtmlAttr($block->getStoreLabel('prefix')) ?>"
- class="input-text <?= $block->escapeHtmlAttr($block->getAttributeValidationClass('prefix')) ?>" <?php if ($block->isPrefixRequired()) echo ' data-validate="{required:true}"' ?>>
- <?php else: ?>
- <select id="<?= $block->escapeHtmlAttr($block->getFieldId('prefix')) ?>"
- name="<?= $block->escapeHtmlAttr($block->getFieldName('prefix')) ?>"
- title="<?= $block->escapeHtmlAttr($block->getStoreLabel('prefix')) ?>"
- class="<?= $block->escapeHtmlAttr($block->getAttributeValidationClass('prefix')) ?>" <?php if ($block->isPrefixRequired()) echo ' data-validate="{required:true}"' ?> >
- <?php foreach ($block->getPrefixOptions() as $_option): ?>
- <option value="<?= $block->escapeHtmlAttr($_option) ?>"<?php if ($block->getObject()->getPrefix() == $_option): ?> selected="selected"<?php endif; ?>>
- <?= $block->escapeHtml(__($_option)) ?>
- </option>
- <?php endforeach; ?>
- </select>
- <?php endif; ?>
- </div>
- </div>
- <?php endif; ?>
- <div class="field field-name-firstname required">
- <label class="label" for="<?= $block->escapeHtmlAttr($block->getFieldId('firstname')) ?>">
- <span><?= $block->escapeHtml($block->getStoreLabel('firstname')) ?></span>
- </label>
- <div class="control">
- <input type="text" id="<?= $block->escapeHtmlAttr($block->getFieldId('firstname')) ?>"
- name="<?= $block->escapeHtmlAttr($block->getFieldName('firstname')) ?>"
- value="<?= $block->escapeHtmlAttr($block->getObject()->getFirstname()) ?>"
- title="<?= $block->escapeHtmlAttr($block->getStoreLabel('firstname')) ?>"
- class="input-text <?= $block->escapeHtmlAttr($block->getAttributeValidationClass('firstname')) ?>" <?php if ($block->getAttributeValidationClass('firstname') == 'required-entry') echo ' data-validate="{required:true}"' ?>>
- </div>
- </div>
- <?php if ($middle): ?>
- <?php $isMiddlenameRequired = $block->isMiddlenameRequired(); ?>
- <div class="field field-name-middlename<?= $isMiddlenameRequired ? ' required' : '' ?>">
- <label class="label" for="<?= $block->escapeHtmlAttr($block->getFieldId('middlename')) ?>">
- <span><?= $block->escapeHtml($block->getStoreLabel('middlename')) ?></span>
- </label>
- <div class="control">
- <input type="text" id="<?= $block->escapeHtmlAttr($block->getFieldId('middlename')) ?>"
- name="<?= $block->escapeHtmlAttr($block->getFieldName('middlename')) ?>"
- value="<?= $block->escapeHtmlAttr($block->getObject()->getMiddlename()) ?>"
- title="<?= $block->escapeHtmlAttr($block->getStoreLabel('middlename')) ?>"
- class="input-text <?= $block->escapeHtmlAttr($block->getAttributeValidationClass('middlename')) ?>" <?= $isMiddlenameRequired ? ' data-validate="{required:true}"' : '' ?>>
- </div>
- </div>
- <?php endif; ?>
- <div class="field field-name-lastname required">
- <label class="label" for="<?= $block->escapeHtmlAttr($block->getFieldId('lastname')) ?>">
- <span><?= $block->escapeHtml($block->getStoreLabel('lastname')) ?></span>
- </label>
- <div class="control">
- <input type="text" id="<?= $block->escapeHtmlAttr($block->getFieldId('lastname')) ?>"
- name="<?= $block->escapeHtmlAttr($block->getFieldName('lastname')) ?>"
- value="<?= $block->escapeHtmlAttr($block->getObject()->getLastname()) ?>"
- title="<?= $block->escapeHtmlAttr($block->getStoreLabel('lastname')) ?>"
- class="input-text <?= $block->escapeHtmlAttr($block->getAttributeValidationClass('lastname')) ?>" <?php if ($block->getAttributeValidationClass('lastname') == 'required-entry') echo ' data-validate="{required:true}"' ?>>
- </div>
- </div>
- <?php if ($suffix): ?>
- <div class="field field-name-suffix<?php if ($block->isSuffixRequired()) echo ' required' ?>">
- <label class="label" for="<?= $block->escapeHtmlAttr($block->getFieldId('suffix')) ?>">
- <span><?= $block->escapeHtml($block->getStoreLabel('suffix')) ?></span>
- </label>
- <div class="control">
- <?php if ($block->getSuffixOptions() === false): ?>
- <input type="text" id="<?= $block->escapeHtmlAttr($block->getFieldId('suffix')) ?>"
- name="<?= $block->escapeHtmlAttr($block->getFieldName('suffix')) ?>"
- value="<?= $block->escapeHtmlAttr($block->getObject()->getSuffix()) ?>"
- title="<?= $block->escapeHtmlAttr($block->getStoreLabel('suffix')) ?>"
- class="input-text <?= $block->escapeHtmlAttr($block->getAttributeValidationClass('suffix')) ?>" <?php if ($block->isSuffixRequired()) echo ' data-validate="{required:true}"' ?>>
- <?php else: ?>
- <select id="<?= $block->escapeHtmlAttr($block->getFieldId('suffix')) ?>"
- name="<?= $block->escapeHtmlAttr($block->getFieldName('suffix')) ?>"
- title="<?= $block->escapeHtmlAttr($block->getStoreLabel('suffix')) ?>"
- class="<?= $block->escapeHtmlAttr($block->getAttributeValidationClass('suffix')) ?>" <?php if ($block->isSuffixRequired()) echo ' data-validate="{required:true}"' ?>>
- <?php foreach ($block->getSuffixOptions() as $_option): ?>
- <option value="<?= $block->escapeHtmlAttr($_option) ?>"<?php if ($block->getObject()->getSuffix() == $_option): ?> selected="selected"<?php endif; ?>>
- <?= $block->escapeHtml(__($_option)) ?>
- </option>
- <?php endforeach; ?>
- </select>
- <?php endif; ?>
- </div>
- </div>
- <?php endif; ?>
- <?php if (($prefix || $middle || $suffix) && !$block->getNoWrap()): ?>
- </div>
- </fieldset>
- </div>
- </div>
- <?php endif; ?>
- <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">
- <input name="form_key" type="hidden" value="TVEgWcjFb03Nzz1L" /> <fieldset class="fieldset create info">
- <legend class="legend"><span>Personal Information</span></legend><br>
- <input type="hidden" name="success_url" value="">
- <input type="hidden" name="error_url" value="">
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement