GenericMetadata.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Customer\Block\Adminhtml\Edit\Tab;
  7. use Magento\Customer\Api\Data\AttributeMetadataInterface;
  8. /**
  9. * Generic block that uses customer metatdata attributes.
  10. *
  11. * @SuppressWarnings(PHPMD.DepthOfInheritance)
  12. */
  13. class GenericMetadata extends \Magento\Backend\Block\Widget\Form\Generic
  14. {
  15. /**
  16. * @var \Magento\Framework\Reflection\DataObjectProcessor
  17. */
  18. protected $dataObjectProcessor;
  19. /**
  20. * @param \Magento\Backend\Block\Template\Context $context
  21. * @param \Magento\Framework\Registry $registry
  22. * @param \Magento\Framework\Data\FormFactory $formFactory
  23. * @param \Magento\Framework\Reflection\DataObjectProcessor $dataObjectProcessor
  24. * @param array $data
  25. */
  26. public function __construct(
  27. \Magento\Backend\Block\Template\Context $context,
  28. \Magento\Framework\Registry $registry,
  29. \Magento\Framework\Data\FormFactory $formFactory,
  30. \Magento\Framework\Reflection\DataObjectProcessor $dataObjectProcessor,
  31. array $data = []
  32. ) {
  33. $this->dataObjectProcessor = $dataObjectProcessor;
  34. parent::__construct($context, $registry, $formFactory, $data);
  35. }
  36. /**
  37. * Set Fieldset to Form
  38. *
  39. * @param AttributeMetadataInterface[] $attributes attributes that are to be added
  40. * @param \Magento\Framework\Data\Form\Element\Fieldset $fieldset
  41. * @param array $exclude attributes that should be skipped
  42. * @return void
  43. */
  44. protected function _setFieldset($attributes, $fieldset, $exclude = [])
  45. {
  46. $this->_addElementTypes($fieldset);
  47. foreach ($attributes as $attribute) {
  48. // Note, ignoring whether its visible or not,
  49. if (($inputType = $attribute->getFrontendInput()) && !in_array(
  50. $attribute->getAttributeCode(),
  51. $exclude
  52. ) && ('media_image' != $inputType || $attribute->getAttributeCode() == 'image')
  53. ) {
  54. $fieldType = $inputType;
  55. $element = $fieldset->addField(
  56. $attribute->getAttributeCode(),
  57. $fieldType,
  58. [
  59. 'name' => $attribute->getAttributeCode(),
  60. 'label' => __($attribute->getFrontendLabel()),
  61. 'class' => $attribute->getFrontendClass(),
  62. 'required' => $attribute->isRequired(),
  63. 'note' => $attribute->getNote()
  64. ]
  65. );
  66. $element->setAfterElementHtml($this->_getAdditionalElementHtml($element));
  67. $this->_applyTypeSpecificConfigCustomer($inputType, $element, $attribute);
  68. }
  69. }
  70. }
  71. /**
  72. * Apply configuration specific for different element type
  73. *
  74. * @param string $inputType
  75. * @param \Magento\Framework\Data\Form\Element\AbstractElement $element
  76. * @param AttributeMetadataInterface $attribute
  77. * @return void
  78. */
  79. protected function _applyTypeSpecificConfigCustomer(
  80. $inputType,
  81. $element,
  82. AttributeMetadataInterface $attribute
  83. ) {
  84. switch ($inputType) {
  85. case 'select':
  86. $element->setValues($this->_getAttributeOptionsArray($attribute));
  87. break;
  88. case 'multiselect':
  89. $element->setValues($this->_getAttributeOptionsArray($attribute));
  90. $element->setCanBeEmpty(true);
  91. break;
  92. case 'date':
  93. $element->setDateFormat($this->_localeDate->getDateFormatWithLongYear());
  94. break;
  95. case 'multiline':
  96. $element->setLineCount($attribute->getMultilineCount());
  97. break;
  98. default:
  99. break;
  100. }
  101. }
  102. /**
  103. * @param AttributeMetadataInterface $attribute
  104. * @return array
  105. */
  106. protected function _getAttributeOptionsArray(AttributeMetadataInterface $attribute)
  107. {
  108. $options = $attribute->getOptions();
  109. $result = [];
  110. foreach ($options as $option) {
  111. $result[] = $this->dataObjectProcessor->buildOutputDataArray(
  112. $option,
  113. \Magento\Customer\Api\Data\OptionInterface::class
  114. );
  115. }
  116. return $result;
  117. }
  118. }