Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
ColumnFactory.php
Go to the documentation of this file.
1 <?php
7 
13 {
17  protected $componentFactory;
18 
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 
32  protected $dataTypeMap = [
33  'default' => 'text',
34  'text' => 'text',
35  'boolean' => 'select',
36  'select' => 'select',
37  'multiselect' => 'multiselect',
38  'date' => 'date',
39  ];
40 
44  public function __construct(\Magento\Framework\View\Element\UiComponentFactory $componentFactory)
45  {
46  $this->componentFactory = $componentFactory;
47  }
48 
55  public function create($attribute, $context, array $config = [])
56  {
57  $columnName = $attribute->getAttributeCode();
58  $config = array_merge([
59  'label' => __($attribute->getDefaultFrontendLabel()),
60  'dataType' => $this->getDataType($attribute),
61  'add_field' => true,
62  'visible' => $attribute->getIsVisibleInGrid(),
63  'filter' => ($attribute->getIsFilterableInGrid())
64  ? $this->getFilterType($attribute->getFrontendInput())
65  : null,
66  ], $config);
67 
68  if ($attribute->usesSource()) {
69  $config['options'] = $attribute->getSource()->getAllOptions();
70  }
71 
72  $config['component'] = $this->getJsComponent($config['dataType']);
73 
74  $arguments = [
75  'data' => [
76  'config' => $config,
77  ],
78  'context' => $context,
79  ];
80 
81  return $this->componentFactory->create($columnName, 'column', $arguments);
82  }
83 
88  protected function getJsComponent($dataType)
89  {
90  return $this->jsComponentMap[$dataType];
91  }
92 
97  protected function getDataType($attribute)
98  {
99  return isset($this->dataTypeMap[$attribute->getFrontendInput()])
100  ? $this->dataTypeMap[$attribute->getFrontendInput()]
101  : $this->dataTypeMap['default'];
102  }
103 
110  protected function getFilterType($frontendInput)
111  {
112  $filtersMap = ['date' => 'dateRange'];
113  $result = array_replace_recursive($this->dataTypeMap, $filtersMap);
114  return isset($result[$frontendInput]) ? $result[$frontendInput] : $result['default'];
115  }
116 }
$config
Definition: fraud_order.php:17
__()
Definition: __.php:13
__construct(\Magento\Framework\View\Element\UiComponentFactory $componentFactory)
create($attribute, $context, array $config=[])
$arguments