123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179 |
- <?php
- /**
- * Copyright © Magento, Inc. All rights reserved.
- * See COPYING.txt for license details.
- */
- namespace Magento\Customer\Block\Widget;
- use Magento\Customer\Api\AddressMetadataInterface;
- use Magento\Customer\Api\CustomerMetadataInterface;
- use Magento\Customer\Api\Data\CustomerInterface;
- use Magento\Customer\Helper\Address as AddressHelper;
- use Magento\Customer\Model\Options;
- use Magento\Framework\View\Element\Template\Context;
- /**
- * Widget for showing customer telephone.
- *
- * @method CustomerInterface getObject()
- * @method Name setObject(CustomerInterface $customer)
- *
- * @SuppressWarnings(PHPMD.DepthOfInheritance)
- */
- class Telephone extends AbstractWidget
- {
- /**
- * the attribute code
- */
- const ATTRIBUTE_CODE = 'telephone';
- /**
- * @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('Magento_Customer::widget/telephone.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 showTelephone()
- {
- return $this->_isAttributeVisible(self::ATTRIBUTE_CODE);
- }
- /**
- * @inheritdoc
- */
- protected function _getAttribute($attributeCode)
- {
- if ($this->getForceUseCustomerAttributes() || $this->getObject() instanceof CustomerInterface) {
- return parent::_getAttribute($attributeCode);
- }
- try {
- $attribute = $this->addressMetadata->getAttributeMetadata($attributeCode);
- } catch (\Magento\Framework\Exception\NoSuchEntityException $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 _isAttributeVisible($attributeCode)
- {
- $attributeMetadata = $this->_getAttribute($attributeCode);
- return $attributeMetadata ? (bool)$attributeMetadata->isVisible() : false;
- }
- /**
- * Check if telephone attribute enabled in system
- *
- * @return bool
- */
- public function isEnabled()
- {
- return $this->_getAttribute(self::ATTRIBUTE_CODE) ? (bool)$this->_getAttribute(self::ATTRIBUTE_CODE)->isVisible(
- ) : false;
- }
- /**
- * Check if telephone attribute marked as required
- *
- * @return bool
- */
- public function isRequired()
- {
- return $this->_getAttribute(self::ATTRIBUTE_CODE) ? (bool)$this->_getAttribute(self::ATTRIBUTE_CODE)
- ->isRequired() : false;
- }
- }
|