ColumnFactory.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Catalog\Ui\Component;
  7. /**
  8. * Column Factory
  9. *
  10. * @api
  11. * @since 100.0.2
  12. */
  13. class ColumnFactory
  14. {
  15. /**
  16. * @var \Magento\Framework\View\Element\UiComponentFactory
  17. */
  18. protected $componentFactory;
  19. /**
  20. * @var array
  21. */
  22. protected $jsComponentMap = [
  23. 'text' => 'Magento_Ui/js/grid/columns/column',
  24. 'select' => 'Magento_Ui/js/grid/columns/select',
  25. 'multiselect' => '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' => 'multiselect',
  37. 'date' => 'date',
  38. ];
  39. /**
  40. * @param \Magento\Framework\View\Element\UiComponentFactory $componentFactory
  41. */
  42. public function __construct(\Magento\Framework\View\Element\UiComponentFactory $componentFactory)
  43. {
  44. $this->componentFactory = $componentFactory;
  45. }
  46. /**
  47. * Create Factory
  48. *
  49. * @param \Magento\Catalog\Api\Data\ProductAttributeInterface $attribute
  50. * @param \Magento\Framework\View\Element\UiComponent\ContextInterface $context
  51. * @param array $config
  52. *
  53. * @return \Magento\Ui\Component\Listing\Columns\ColumnInterface
  54. * @throws \Magento\Framework\Exception\LocalizedException
  55. */
  56. public function create($attribute, $context, array $config = [])
  57. {
  58. $columnName = $attribute->getAttributeCode();
  59. $config = array_merge([
  60. 'label' => __($attribute->getDefaultFrontendLabel()),
  61. 'dataType' => $this->getDataType($attribute),
  62. 'add_field' => true,
  63. 'visible' => $attribute->getIsVisibleInGrid(),
  64. 'filter' => ($attribute->getIsFilterableInGrid())
  65. ? $this->getFilterType($attribute->getFrontendInput())
  66. : null,
  67. ], $config);
  68. if ($attribute->usesSource()) {
  69. $config['options'] = $attribute->getSource()->getAllOptions();
  70. }
  71. $config['component'] = $this->getJsComponent($config['dataType']);
  72. $arguments = [
  73. 'data' => [
  74. 'config' => $config,
  75. ],
  76. 'context' => $context,
  77. ];
  78. return $this->componentFactory->create($columnName, 'column', $arguments);
  79. }
  80. /**
  81. * Get Js Component
  82. *
  83. * @param string $dataType
  84. *
  85. * @return string
  86. */
  87. protected function getJsComponent($dataType)
  88. {
  89. return $this->jsComponentMap[$dataType];
  90. }
  91. /**
  92. * Get Data Type
  93. *
  94. * @param \Magento\Catalog\Api\Data\ProductAttributeInterface $attribute
  95. *
  96. * @return string
  97. */
  98. protected function getDataType($attribute)
  99. {
  100. return $this->dataTypeMap[$attribute->getFrontendInput()] ?? $this->dataTypeMap['default'];
  101. }
  102. /**
  103. * Retrieve filter type by $frontendInput
  104. *
  105. * @param string $frontendInput
  106. * @return string
  107. */
  108. protected function getFilterType($frontendInput)
  109. {
  110. $filtersMap = ['date' => 'dateRange'];
  111. $result = array_replace_recursive($this->dataTypeMap, $filtersMap);
  112. return $result[$frontendInput] ?? $result['default'];
  113. }
  114. }