Gender.php 3.1 KB

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