1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
namespace Magento\Customer\Block\Widget;
use Magento\Customer\Api\CustomerMetadataInterface;
use Magento\Customer\Api\CustomerRepositoryInterface;
use Magento\Customer\Api\Data\CustomerInterface;
use Magento\Customer\Api\Data\OptionInterface;
/**
* Block to render customer's gender attribute
*
* @SuppressWarnings(PHPMD.DepthOfInheritance)
*/
class Gender extends AbstractWidget
{
/**
* @var \Magento\Customer\Model\Session
*/
protected $_customerSession;
/**
* @var CustomerRepositoryInterface
*/
protected $customerRepository;
/**
* Create an instance of the Gender widget
*
* @param \Magento\Framework\View\Element\Template\Context $context
* @param \Magento\Customer\Helper\Address $addressHelper
* @param CustomerMetadataInterface $customerMetadata
* @param CustomerRepositoryInterface $customerRepository
* @param \Magento\Customer\Model\Session $customerSession
* @param array $data
*/
public function __construct(
\Magento\Framework\View\Element\Template\Context $context,
\Magento\Customer\Helper\Address $addressHelper,
CustomerMetadataInterface $customerMetadata,
CustomerRepositoryInterface $customerRepository,
\Magento\Customer\Model\Session $customerSession,
array $data = []
) {
$this->_customerSession = $customerSession;
$this->customerRepository = $customerRepository;
parent::__construct($context, $addressHelper, $customerMetadata, $data);
$this->_isScopePrivate = true;
}
/**
* Initialize block
*
* @return void
*/
public function _construct()
{
parent::_construct();
$this->setTemplate('Magento_Customer::widget/gender.phtml');
}
/**
* Check if gender attribute enabled in system
*
* @return bool
*/
public function isEnabled()
{
return $this->_getAttribute('gender') ? (bool)$this->_getAttribute('gender')->isVisible() : false;
}
/**
* Check if gender attribute marked as required
*
* @return bool
*/
public function isRequired()
{
return $this->_getAttribute('gender') ? (bool)$this->_getAttribute('gender')->isRequired() : false;
}
/**
* Retrieve store attribute label
*
* @param string $attributeCode
*
* @return string
*/
public function getStoreLabel($attributeCode)
{
$attribute = $this->_getAttribute($attributeCode);
return $attribute ? __($attribute->getStoreLabel()) : '';
}
/**
* Get current customer from session
*
* @return CustomerInterface
*/
public function getCustomer()
{
return $this->customerRepository->getById($this->_customerSession->getCustomerId());
}
/**
* Returns options from gender attribute
*
* @return OptionInterface[]
*/
public function getGenderOptions()
{
return $this->_getAttribute('gender')->getOptions();
}
}