Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
Company.php
Go to the documentation of this file.
1 <?php
8 
12 use Magento\Customer\Helper\Address as AddressHelper;
15 
24 class Company extends AbstractWidget
25 {
26 
30  const ATTRIBUTE_CODE = 'company';
31 
35  protected $addressMetadata;
36 
40  protected $options;
41 
50  public function __construct(
51  Context $context,
52  AddressHelper $addressHelper,
56  array $data = []
57  ) {
58  $this->options = $options;
59  parent::__construct($context, $addressHelper, $customerMetadata, $data);
60  $this->addressMetadata = $addressMetadata;
61  $this->_isScopePrivate = true;
62  }
63 
67  public function _construct()
68  {
69  parent::_construct();
70 
71  // default template location
72  $this->setTemplate('Magento_Customer::widget/company.phtml');
73  }
74 
82  protected function _showConfig($key)
83  {
84  return (bool)$this->getConfig($key);
85  }
86 
92  public function showCompany()
93  {
94  return $this->_isAttributeVisible(self::ATTRIBUTE_CODE);
95  }
96 
100  protected function _getAttribute($attributeCode)
101  {
102  if ($this->getForceUseCustomerAttributes() || $this->getObject() instanceof CustomerInterface) {
103  return parent::_getAttribute($attributeCode);
104  }
105 
106  try {
107  $attribute = $this->addressMetadata->getAttributeMetadata($attributeCode);
108  } catch (\Magento\Framework\Exception\NoSuchEntityException $e) {
109  return null;
110  }
111 
112  if ($this->getForceUseCustomerRequiredAttributes() && $attribute && !$attribute->isRequired()) {
113  $customerAttribute = parent::_getAttribute($attributeCode);
114  if ($customerAttribute && $customerAttribute->isRequired()) {
115  $attribute = $customerAttribute;
116  }
117  }
118 
119  return $attribute;
120  }
121 
129  public function getStoreLabel($attributeCode)
130  {
132  return $attribute ? __($attribute->getStoreLabel()) : '';
133  }
134 
143  {
144  return $this->_addressHelper->getAttributeValidationClass($attributeCode);
145  }
146 
152  private function _isAttributeVisible($attributeCode)
153  {
154  $attributeMetadata = $this->_getAttribute($attributeCode);
155  return $attributeMetadata ? (bool)$attributeMetadata->isVisible() : false;
156  }
157 
163  public function isEnabled()
164  {
165  return $this->_getAttribute(self::ATTRIBUTE_CODE) ? (bool)$this->_getAttribute(self::ATTRIBUTE_CODE)->isVisible(
166  ) : false;
167  }
168 
174  public function isRequired()
175  {
176  return $this->_getAttribute(self::ATTRIBUTE_CODE) ? (bool)$this->_getAttribute(self::ATTRIBUTE_CODE)
177  ->isRequired() : false;
178  }
179 }
__()
Definition: __.php:13
$attributeCode
Definition: extend.phtml:12
__construct(Context $context, AddressHelper $addressHelper, CustomerMetadataInterface $customerMetadata, Options $options, AddressMetadataInterface $addressMetadata, array $data=[])
Definition: Company.php:50
getAttributeValidationClass($attributeCode)
Definition: Company.php:142