ColumnFactory.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Customer\Ui\Component;
  7. use Magento\Customer\Api\Data\AttributeMetadataInterface as AttributeMetadata;
  8. use Magento\Customer\Ui\Component\Listing\Column\InlineEditUpdater;
  9. use Magento\Customer\Api\CustomerMetadataInterface;
  10. class ColumnFactory
  11. {
  12. /**
  13. * @var \Magento\Framework\View\Element\UiComponentFactory
  14. */
  15. protected $componentFactory;
  16. /**
  17. * @var \Magento\Customer\Ui\Component\Listing\Column\InlineEditUpdater
  18. */
  19. protected $inlineEditUpdater;
  20. /**
  21. * @var array
  22. */
  23. protected $jsComponentMap = [
  24. 'text' => 'Magento_Ui/js/grid/columns/column',
  25. 'select' => 'Magento_Ui/js/grid/columns/select',
  26. 'date' => 'Magento_Ui/js/grid/columns/date',
  27. ];
  28. /**
  29. * @var array
  30. */
  31. protected $dataTypeMap = [
  32. 'default' => 'text',
  33. 'text' => 'text',
  34. 'boolean' => 'select',
  35. 'select' => 'select',
  36. 'multiselect' => 'select',
  37. 'date' => 'date',
  38. ];
  39. /**
  40. * @param \Magento\Framework\View\Element\UiComponentFactory $componentFactory
  41. * @param InlineEditUpdater $inlineEditor
  42. */
  43. public function __construct(
  44. \Magento\Framework\View\Element\UiComponentFactory $componentFactory,
  45. InlineEditUpdater $inlineEditor
  46. ) {
  47. $this->componentFactory = $componentFactory;
  48. $this->inlineEditUpdater = $inlineEditor;
  49. }
  50. /**
  51. * @param array $attributeData
  52. * @param string $columnName
  53. * @param \Magento\Framework\View\Element\UiComponent\ContextInterface $context
  54. * @param array $config
  55. * @return \Magento\Ui\Component\Listing\Columns\ColumnInterface
  56. */
  57. public function create(array $attributeData, $columnName, $context, array $config = [])
  58. {
  59. $config = array_merge([
  60. 'label' => __($attributeData[AttributeMetadata::FRONTEND_LABEL]),
  61. 'dataType' => $this->getDataType($attributeData[AttributeMetadata::FRONTEND_INPUT]),
  62. 'align' => 'left',
  63. 'visible' => (bool)$attributeData[AttributeMetadata::IS_VISIBLE_IN_GRID],
  64. 'component' => $this->getJsComponent($this->getDataType($attributeData[AttributeMetadata::FRONTEND_INPUT])),
  65. ], $config);
  66. if ($attributeData[AttributeMetadata::FRONTEND_INPUT] == 'date') {
  67. $config['dateFormat'] = 'MMM d, y';
  68. $config['timezone'] = false;
  69. }
  70. if (count($attributeData[AttributeMetadata::OPTIONS]) && !isset($config[AttributeMetadata::OPTIONS])) {
  71. $config[AttributeMetadata::OPTIONS] = $attributeData[AttributeMetadata::OPTIONS];
  72. }
  73. if ($attributeData[AttributeMetadata::OPTIONS]) {
  74. $config['options'] = $attributeData[AttributeMetadata::OPTIONS];
  75. }
  76. $arguments = [
  77. 'data' => [
  78. 'js_config' => [
  79. 'component' => $this->getJsComponent($config['dataType']),
  80. ],
  81. 'config' => $config,
  82. ],
  83. 'context' => $context,
  84. ];
  85. $column = $this->componentFactory->create($columnName, 'column', $arguments);
  86. $this->inlineEditUpdater->applyEditing(
  87. $column,
  88. $attributeData[AttributeMetadata::FRONTEND_INPUT],
  89. $attributeData[AttributeMetadata::VALIDATION_RULES],
  90. $attributeData[AttributeMetadata::REQUIRED]
  91. );
  92. return $column;
  93. }
  94. /**
  95. * @param string $dataType
  96. * @return string
  97. */
  98. protected function getJsComponent($dataType)
  99. {
  100. return $this->jsComponentMap[$dataType];
  101. }
  102. /**
  103. * @param string $frontendType
  104. * @return string
  105. */
  106. protected function getDataType($frontendType)
  107. {
  108. return isset($this->dataTypeMap[$frontendType])
  109. ? $this->dataTypeMap[$frontendType]
  110. : $this->dataTypeMap['default'];
  111. }
  112. }