Skip to content

Commit 12bf101

Browse files
committed
MAGE-1151: refactor data colunm formatting
1 parent acc3a2a commit 12bf101

File tree

1 file changed

+31
-18
lines changed

1 file changed

+31
-18
lines changed

Ui/Component/Listing/Column/Data.php

Lines changed: 31 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@
44

55
class Data extends \Magento\Ui\Component\Listing\Columns\Column
66
{
7+
8+
const MAX_DEPTH_DISPLAY = 3;
9+
710
/**
811
* @param array $dataSource
912
*
@@ -23,27 +26,37 @@ public function prepareDataSource(array $dataSource)
2326

2427
foreach ($dataSource['data']['items'] as &$item) {
2528
$data = json_decode($item[$fieldName], true);
26-
$formattedData = '';
27-
if (is_array($data)) {
28-
foreach ($data as $key => $value) {
29-
if (is_array($value)) {
30-
$stringValue = '';
31-
foreach ($value as $k => $v) {
32-
if (is_string($k)) {
33-
$v = !is_array($v) ? $v : implode(",", $v);
34-
$stringValue .= '<br>&nbsp;&nbsp;&nbsp; - <strong>' . $k . '</strong> : '. $v;
35-
} else {
36-
$stringValue .= $v . ',';
37-
}
38-
}
39-
$value = rtrim($stringValue,",");
40-
}
41-
$formattedData .= '- <strong>' .$key . '</strong> : ' . $value . '<br>';
29+
$item[$fieldName] = is_array($data) ? $this->formatData($data) : '';;
30+
}
31+
32+
return $dataSource;
33+
}
34+
35+
protected function formatData(array $data, $depth = 0): string
36+
{
37+
if ($depth > self::MAX_DEPTH_DISPLAY) {
38+
return '';
39+
}
40+
41+
$formattedData = '';
42+
43+
foreach ($data as $key => $value) {
44+
$stringKey = '- <strong>' . $key . '</strong>';
45+
46+
if (is_array($value)) {
47+
if ($key === 'entityIds') {
48+
$formattedData .=
49+
str_repeat('&nbsp;&nbsp;&nbsp;', $depth ) . $stringKey . ' : ' . implode(', ', $value) . '<br>';
50+
} else {
51+
$formattedData .= $stringKey . ' :<br>' . $this->formatData($value, ++$depth);
4252
}
53+
54+
continue;
4355
}
44-
$item[$fieldName] = $formattedData;
56+
57+
$formattedData .= str_repeat('&nbsp;&nbsp;&nbsp;', $depth ) . $stringKey . ' : ' . $value . '<br>';
4558
}
4659

47-
return $dataSource;
60+
return $formattedData;
4861
}
4962
}

0 commit comments

Comments
 (0)