AbstractWidget.php 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  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. class AbstractWidget extends \Magento\Framework\View\Element\Template
  9. {
  10. /**
  11. * @var CustomerMetadataInterface
  12. */
  13. protected $customerMetadata;
  14. /**
  15. * @var \Magento\Customer\Helper\Address
  16. */
  17. protected $_addressHelper;
  18. /**
  19. * @param \Magento\Framework\View\Element\Template\Context $context
  20. * @param \Magento\Customer\Helper\Address $addressHelper
  21. * @param CustomerMetadataInterface $customerMetadata
  22. * @param array $data
  23. */
  24. public function __construct(
  25. \Magento\Framework\View\Element\Template\Context $context,
  26. \Magento\Customer\Helper\Address $addressHelper,
  27. CustomerMetadataInterface $customerMetadata,
  28. array $data = []
  29. ) {
  30. $this->_addressHelper = $addressHelper;
  31. $this->customerMetadata = $customerMetadata;
  32. parent::__construct($context, $data);
  33. $this->_isScopePrivate = true;
  34. }
  35. /**
  36. * @param string $key
  37. * @return null|string
  38. */
  39. public function getConfig($key)
  40. {
  41. return $this->_addressHelper->getConfig($key);
  42. }
  43. /**
  44. * @return string
  45. */
  46. public function getFieldIdFormat()
  47. {
  48. if (!$this->hasData('field_id_format')) {
  49. $this->setData('field_id_format', '%s');
  50. }
  51. return $this->getData('field_id_format');
  52. }
  53. /**
  54. * @return string
  55. */
  56. public function getFieldNameFormat()
  57. {
  58. if (!$this->hasData('field_name_format')) {
  59. $this->setData('field_name_format', '%s');
  60. }
  61. return $this->getData('field_name_format');
  62. }
  63. /**
  64. * @param string $field
  65. * @return string
  66. */
  67. public function getFieldId($field)
  68. {
  69. return sprintf($this->getFieldIdFormat(), $field);
  70. }
  71. /**
  72. * @param string $field
  73. * @return string
  74. */
  75. public function getFieldName($field)
  76. {
  77. return sprintf($this->getFieldNameFormat(), $field);
  78. }
  79. /**
  80. * Retrieve customer attribute instance
  81. *
  82. * @param string $attributeCode
  83. * @return \Magento\Customer\Api\Data\AttributeMetadataInterface|null
  84. */
  85. protected function _getAttribute($attributeCode)
  86. {
  87. try {
  88. return $this->customerMetadata->getAttributeMetadata($attributeCode);
  89. } catch (\Magento\Framework\Exception\NoSuchEntityException $e) {
  90. return null;
  91. }
  92. }
  93. }