4
4
5
5
class Data extends \Magento \Ui \Component \Listing \Columns \Column
6
6
{
7
+
8
+ const MAX_DEPTH_DISPLAY = 3 ;
9
+
7
10
/**
8
11
* @param array $dataSource
9
12
*
@@ -23,27 +26,37 @@ public function prepareDataSource(array $dataSource)
23
26
24
27
foreach ($ dataSource ['data ' ]['items ' ] as &$ item ) {
25
28
$ 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> - <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 (' ' , $ depth ) . $ stringKey . ' : ' . implode (', ' , $ value ) . '<br> ' ;
50
+ } else {
51
+ $ formattedData .= $ stringKey . ' :<br> ' . $ this ->formatData ($ value , ++$ depth );
42
52
}
53
+
54
+ continue ;
43
55
}
44
- $ item [$ fieldName ] = $ formattedData ;
56
+
57
+ $ formattedData .= str_repeat (' ' , $ depth ) . $ stringKey . ' : ' . $ value . '<br> ' ;
45
58
}
46
59
47
- return $ dataSource ;
60
+ return $ formattedData ;
48
61
}
49
62
}
0 commit comments