AttributeMetadataResolver.php 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * Copyright © Magento, Inc. All rights reserved.
  5. * See COPYING.txt for license details.
  6. */
  7. namespace Magento\Customer\Model;
  8. use Magento\Customer\Model\ResourceModel\Address\Attribute\Source\CountryWithWebsites;
  9. use Magento\Eav\Model\Entity\Attribute\AbstractAttribute;
  10. use Magento\Customer\Api\Data\AddressInterface;
  11. use Magento\Ui\DataProvider\EavValidationRules;
  12. use Magento\Ui\Component\Form\Field;
  13. use Magento\Eav\Model\Entity\Type;
  14. use Magento\Eav\Api\Data\AttributeInterface;
  15. use Magento\Framework\View\Element\UiComponent\ContextInterface;
  16. use Magento\Customer\Api\Data\CustomerInterface;
  17. use Magento\Customer\Model\Config\Share as ShareConfig;
  18. /**
  19. * Class to build meta data of the customer or customer address attribute
  20. * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
  21. */
  22. class AttributeMetadataResolver
  23. {
  24. /**
  25. * EAV attribute properties to fetch from meta storage
  26. * @var array
  27. */
  28. private static $metaProperties = [
  29. 'dataType' => 'frontend_input',
  30. 'visible' => 'is_visible',
  31. 'required' => 'is_required',
  32. 'label' => 'frontend_label',
  33. 'sortOrder' => 'sort_order',
  34. 'notice' => 'note',
  35. 'default' => 'default_value',
  36. 'size' => 'multiline_count',
  37. ];
  38. /**
  39. * Form element mapping
  40. *
  41. * @var array
  42. */
  43. private static $formElement = [
  44. 'text' => 'input',
  45. 'hidden' => 'input',
  46. 'boolean' => 'checkbox',
  47. ];
  48. /**
  49. * @var CountryWithWebsites
  50. */
  51. private $countryWithWebsiteSource;
  52. /**
  53. * @var EavValidationRules
  54. */
  55. private $eavValidationRules;
  56. /**
  57. * @var FileUploaderDataResolver
  58. */
  59. private $fileUploaderDataResolver;
  60. /**
  61. * @var ContextInterface
  62. */
  63. private $context;
  64. /**
  65. * @var ShareConfig
  66. */
  67. private $shareConfig;
  68. /**
  69. * @param CountryWithWebsites $countryWithWebsiteSource
  70. * @param EavValidationRules $eavValidationRules
  71. * @param \Magento\Customer\Model\FileUploaderDataResolver $fileUploaderDataResolver
  72. * @param ContextInterface $context
  73. * @param ShareConfig $shareConfig
  74. */
  75. public function __construct(
  76. CountryWithWebsites $countryWithWebsiteSource,
  77. EavValidationRules $eavValidationRules,
  78. fileUploaderDataResolver $fileUploaderDataResolver,
  79. ContextInterface $context,
  80. ShareConfig $shareConfig
  81. ) {
  82. $this->countryWithWebsiteSource = $countryWithWebsiteSource;
  83. $this->eavValidationRules = $eavValidationRules;
  84. $this->fileUploaderDataResolver = $fileUploaderDataResolver;
  85. $this->context = $context;
  86. $this->shareConfig = $shareConfig;
  87. }
  88. /**
  89. * Get meta data of the customer or customer address attribute
  90. *
  91. * @param AbstractAttribute $attribute
  92. * @param Type $entityType
  93. * @param bool $allowToShowHiddenAttributes
  94. * @param string $requestFieldName
  95. * @return array
  96. * @throws \Magento\Framework\Exception\LocalizedException
  97. */
  98. public function getAttributesMeta(
  99. AbstractAttribute $attribute,
  100. Type $entityType,
  101. bool $allowToShowHiddenAttributes,
  102. string $requestFieldName
  103. ): array {
  104. $meta = $this->modifyBooleanAttributeMeta($attribute);
  105. // use getDataUsingMethod, since some getters are defined and apply additional processing of returning value
  106. foreach (self::$metaProperties as $metaName => $origName) {
  107. $value = $attribute->getDataUsingMethod($origName);
  108. $meta['arguments']['data']['config'][$metaName] = ($metaName === 'label') ? __($value) : $value;
  109. if ('frontend_input' === $origName) {
  110. $meta['arguments']['data']['config']['formElement'] = self::$formElement[$value] ?? $value;
  111. }
  112. }
  113. if ($attribute->usesSource()) {
  114. if ($attribute->getAttributeCode() === AddressInterface::COUNTRY_ID) {
  115. $meta['arguments']['data']['config']['options'] = $this->countryWithWebsiteSource
  116. ->getAllOptions();
  117. } else {
  118. $meta['arguments']['data']['config']['options'] = $attribute->getSource()->getAllOptions();
  119. }
  120. }
  121. $rules = $this->eavValidationRules->build($attribute, $meta['arguments']['data']['config']);
  122. if (!empty($rules)) {
  123. $meta['arguments']['data']['config']['validation'] = $rules;
  124. }
  125. $meta['arguments']['data']['config']['componentType'] = Field::NAME;
  126. $meta['arguments']['data']['config']['visible'] = $this->canShowAttribute(
  127. $attribute,
  128. $requestFieldName,
  129. $allowToShowHiddenAttributes
  130. );
  131. $this->fileUploaderDataResolver->overrideFileUploaderMetadata(
  132. $entityType,
  133. $attribute,
  134. $meta['arguments']['data']['config']
  135. );
  136. return $meta;
  137. }
  138. /**
  139. * Detect can we show attribute on specific form or not
  140. *
  141. * @param AbstractAttribute $customerAttribute
  142. * @param string $requestFieldName
  143. * @param bool $allowToShowHiddenAttributes
  144. * @return bool
  145. */
  146. private function canShowAttribute(
  147. AbstractAttribute $customerAttribute,
  148. string $requestFieldName,
  149. bool $allowToShowHiddenAttributes
  150. ) {
  151. $userDefined = (bool)$customerAttribute->getIsUserDefined();
  152. if (!$userDefined) {
  153. return $customerAttribute->getIsVisible();
  154. }
  155. $canShowOnForm = $this->canShowAttributeInForm($customerAttribute, $requestFieldName);
  156. return ($allowToShowHiddenAttributes && $canShowOnForm) ||
  157. (!$allowToShowHiddenAttributes && $canShowOnForm && $customerAttribute->getIsVisible());
  158. }
  159. /**
  160. * Check whether the specific attribute can be shown in form: customer registration, customer edit, etc...
  161. *
  162. * @param AbstractAttribute $customerAttribute
  163. * @param string $requestFieldName
  164. * @return bool
  165. */
  166. private function canShowAttributeInForm(AbstractAttribute $customerAttribute, string $requestFieldName): bool
  167. {
  168. $isRegistration = $this->context->getRequestParam($requestFieldName) === null;
  169. if ($customerAttribute->getEntityType()->getEntityTypeCode() === 'customer') {
  170. return \is_array($customerAttribute->getUsedInForms()) &&
  171. (
  172. (\in_array('customer_account_create', $customerAttribute->getUsedInForms(), true)
  173. && $isRegistration) ||
  174. (\in_array('customer_account_edit', $customerAttribute->getUsedInForms(), true)
  175. && !$isRegistration)
  176. );
  177. }
  178. return \is_array($customerAttribute->getUsedInForms()) &&
  179. \in_array('customer_address_edit', $customerAttribute->getUsedInForms(), true);
  180. }
  181. /**
  182. * Modify boolean attribute meta data
  183. *
  184. * @param AttributeInterface $attribute
  185. * @return array
  186. */
  187. private function modifyBooleanAttributeMeta(AttributeInterface $attribute): array
  188. {
  189. $meta = [];
  190. if ($attribute->getFrontendInput() === 'boolean') {
  191. $meta['arguments']['data']['config']['prefer'] = 'toggle';
  192. $meta['arguments']['data']['config']['valueMap'] = [
  193. 'true' => '1',
  194. 'false' => '0',
  195. ];
  196. }
  197. return $meta;
  198. }
  199. /**
  200. * Add global scope parameter and filter options to website meta
  201. *
  202. * @param array $meta
  203. * @return void
  204. */
  205. public function processWebsiteMeta(&$meta): void
  206. {
  207. if (isset($meta[CustomerInterface::WEBSITE_ID]) && $this->shareConfig->isGlobalScope()) {
  208. $meta[CustomerInterface::WEBSITE_ID]['arguments']['data']['config']['isGlobalScope'] = 1;
  209. }
  210. if (isset($meta[AddressInterface::COUNTRY_ID]) && !$this->shareConfig->isGlobalScope()) {
  211. $meta[AddressInterface::COUNTRY_ID]['arguments']['data']['config']['filterBy'] = [
  212. 'target' => 'customer_form.customer_form_data_source:data.customer.website_id',
  213. 'field' => 'website_ids'
  214. ];
  215. }
  216. }
  217. }