Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
MetadataProvider.php
Go to the documentation of this file.
1 <?php
7 
16 
21 {
25  protected $filter;
26 
30  protected $columns;
31 
35  protected $localeDate;
36 
40  protected $locale;
41 
45  protected $dateFormat;
46 
50  protected $data;
51 
59  public function __construct(
62  ResolverInterface $localeResolver,
63  $dateFormat = 'M j, Y h:i:s A',
64  array $data = []
65  ) {
66  $this->filter = $filter;
67  $this->localeDate = $localeDate;
68  $this->locale = $localeResolver->getLocale();
69  $this->dateFormat = $dateFormat;
70  $this->data = $data;
71  }
72 
80  protected function getColumnsComponent(UiComponentInterface $component)
81  {
82  foreach ($component->getChildComponents() as $childComponent) {
83  if ($childComponent instanceof Columns) {
84  return $childComponent;
85  }
86  }
87  throw new \Exception('No columns found');
88  }
89 
96  protected function getColumns(UiComponentInterface $component)
97  {
98  if (!isset($this->columns[$component->getName()])) {
99  $columns = $this->getColumnsComponent($component);
100  foreach ($columns->getChildComponents() as $column) {
101  if ($column->getData('config/label') && $column->getData('config/dataType') !== 'actions') {
102  $this->columns[$component->getName()][$column->getName()] = $column;
103  }
104  }
105  }
106  return $this->columns[$component->getName()];
107  }
108 
115  public function getHeaders(UiComponentInterface $component)
116  {
117  $row = [];
118  foreach ($this->getColumns($component) as $column) {
119  $row[] = $column->getData('config/label');
120  }
121 
122  array_walk($row, function (&$header) {
123  if (mb_strpos($header, 'ID') === 0) {
124  $header = '"' . $header . '"';
125  }
126  });
127 
128  return $row;
129  }
130 
137  public function getFields(UiComponentInterface $component)
138  {
139  $row = [];
140  foreach ($this->getColumns($component) as $column) {
141  $row[] = $column->getName();
142  }
143  return $row;
144  }
145 
154  public function getRowData(DocumentInterface $document, $fields, $options)
155  {
156  $row = [];
157  foreach ($fields as $column) {
158  if (isset($options[$column])) {
159  $key = $document->getCustomAttribute($column)->getValue();
160  if (isset($options[$column][$key])) {
161  $row[] = $options[$column][$key];
162  } else {
163  $row[] = '';
164  }
165  } else {
166  $row[] = $document->getCustomAttribute($column)->getValue();
167  }
168  }
169  return $row;
170  }
171 
180  protected function getComplexLabel($list, $label, &$output)
181  {
182  foreach ($list as $item) {
183  if (!is_array($item['value'])) {
184  $output[$item['value']] = $label . $item['label'];
185  } else {
186  $this->getComplexLabel($item['value'], $label . $item['label'], $output);
187  }
188  }
189  }
190 
197  protected function getFilterOptions(Select $filter)
198  {
199  $options = [];
200  foreach ($filter->getData('config/options') as $option) {
201  if (!is_array($option['value'])) {
202  $options[$option['value']] = $option['label'];
203  } else {
204  $this->getComplexLabel(
205  $option['value'],
206  $option['label'],
207  $options
208  );
209  }
210  }
211  return $options;
212  }
213 
219  public function getOptions()
220  {
221  $options = [];
222  $component = $this->filter->getComponent();
223  $childComponents = $component->getChildComponents();
224  $listingTop = $childComponents['listing_top'];
225  foreach ($listingTop->getChildComponents() as $child) {
226  if ($child instanceof Filters) {
227  foreach ($child->getChildComponents() as $filter) {
228  if ($filter instanceof Select) {
229  $options[$filter->getName()] = $this->getFilterOptions($filter);
230  }
231  }
232  }
233  }
234  return $options;
235  }
236 
244  public function convertDate($document, $componentName)
245  {
246  if (!isset($this->data[$componentName])) {
247  return;
248  }
249  foreach ($this->data[$componentName] as $field) {
250  $fieldValue = $document->getData($field);
251  if (!$fieldValue) {
252  continue;
253  }
254  $convertedDate = $this->localeDate->date(
255  new \DateTime($fieldValue, new \DateTimeZone('UTC')),
256  $this->locale,
257  true
258  );
259  $document->setData($field, $convertedDate->format($this->dateFormat));
260  }
261  }
262 }
getHeaders(UiComponentInterface $component)
getColumnsComponent(UiComponentInterface $component)
$fields
Definition: details.phtml:14
getFields(UiComponentInterface $component)
getRowData(DocumentInterface $document, $fields, $options)
$label
Definition: details.phtml:21
__construct(Filter $filter, TimezoneInterface $localeDate, ResolverInterface $localeResolver, $dateFormat='M j, Y h:i:s A', array $data=[])
getColumns(UiComponentInterface $component)