6
6
7
7
class Data extends \Magento \Framework \App \Helper \AbstractHelper
8
8
{
9
+ const STORE_VIEW_SCOPE_CODE = 'stores ' ;
10
+ const WEBSITE_SCOPE_CODE = 'websites ' ;
11
+
9
12
/** @var \Magento\Framework\App\Helper\Context */
10
13
protected $ context ;
11
14
@@ -18,16 +21,29 @@ class Data extends \Magento\Framework\App\Helper\AbstractHelper
18
21
* @var \Magento\Backend\Model\Url
19
22
*/
20
23
protected $ urlBuilder ;
24
+ /**
25
+ * @var \Magento\Config\Model\Config\Structure\SearchInterface
26
+ */
27
+ protected $ configStructure ;
28
+ /**
29
+ * @var \Magento\Framework\Escaper
30
+ */
31
+ protected $ escaper ;
21
32
22
33
/**
34
+ * Data constructor.
23
35
* @param \Magento\Framework\App\Helper\Context $context
24
36
* @param \Magento\Store\Model\StoreManagerInterface $storeManager
25
37
* @param \Magento\Backend\Model\Url $urlBuilder
38
+ * @param \Magento\Config\Model\Config\Structure\SearchInterface $configStructure
39
+ * @param \Magento\Framework\Escaper $escaper
26
40
*/
27
41
public function __construct (
28
42
\Magento \Framework \App \Helper \Context $ context ,
29
43
\Magento \Store \Model \StoreManagerInterface $ storeManager ,
30
- \Magento \Backend \Model \Url $ urlBuilder
44
+ \Magento \Backend \Model \Url $ urlBuilder ,
45
+ \Magento \Config \Model \Config \Structure \SearchInterface $ configStructure ,
46
+ \Magento \Framework \Escaper $ escaper
31
47
) {
32
48
parent ::__construct ($ context );
33
49
@@ -37,6 +53,8 @@ public function __construct(
37
53
// an instance of \Magento\Framework\Url instead of \Magento\Backend\Model\Url, we must explicitly request it
38
54
// via DI.
39
55
$ this ->urlBuilder = $ urlBuilder ;
56
+ $ this ->configStructure = $ configStructure ;
57
+ $ this ->escaper = $ escaper ;
40
58
}
41
59
42
60
/**
@@ -46,17 +64,17 @@ public function __construct(
46
64
* @return array
47
65
*/
48
66
public function getScopeTree () {
49
- $ tree = array (' websites ' => array ());
67
+ $ tree = array (self :: WEBSITE_SCOPE_CODE => array ());
50
68
51
69
$ websites = $ this ->storeManager ->getWebsites ();
52
70
53
71
/* @var $website Website */
54
72
foreach ($ websites as $ website ) {
55
- $ tree [' websites ' ][$ website ->getId ()] = array (' stores ' => array ());
73
+ $ tree [self :: WEBSITE_SCOPE_CODE ][$ website ->getId ()] = array (self :: STORE_VIEW_SCOPE_CODE => array ());
56
74
57
75
/* @var $store Store */
58
76
foreach ($ website ->getStores () as $ store ) {
59
- $ tree [' websites ' ][$ website ->getId ()][' stores ' ][] = $ store ->getId ();
77
+ $ tree [self :: WEBSITE_SCOPE_CODE ][$ website ->getId ()][self :: STORE_VIEW_SCOPE_CODE ][] = $ store ->getId ();
60
78
}
61
79
}
62
80
@@ -66,15 +84,51 @@ public function getScopeTree() {
66
84
/**
67
85
* Wrapper method to get config value at path, scope, and scope code provided
68
86
*
69
- * @param $path
70
- * @param $contextScope
71
- * @param $contextScopeId
72
- * @return mixed
87
+ * @param string $path
88
+ * @param string $contextScope
89
+ * @param string|int $contextScopeId
90
+ * @return string
73
91
*/
74
92
protected function _getConfigValue ($ path , $ contextScope , $ contextScopeId ) {
75
93
return $ this ->context ->getScopeConfig ()->getValue ($ path , $ contextScope , $ contextScopeId );
76
94
}
77
95
96
+ /**
97
+ * Gets human-friendly display value(s) for given config path
98
+ *
99
+ * @param string $path
100
+ * @param string $contextScope
101
+ * @param string|int $contextScopeId
102
+ * @return array
103
+ */
104
+ public function getConfigDisplayValue ($ path , $ contextScope , $ contextScopeId ) {
105
+ $ value = $ this ->_getConfigValue ($ path , $ contextScope , $ contextScopeId );
106
+
107
+ $ labels = [$ value ]; //default labels to raw value
108
+
109
+ /** @var \Magento\Config\Model\Config\Structure\Element\Field $field */
110
+ $ field = $ this ->configStructure ->getElement ($ path );
111
+
112
+ if ($ field ->getOptions ()) {
113
+ $ labels = []; //reset labels so we can add human-friendly labels
114
+
115
+ $ optionsByValue = [];
116
+ foreach ($ field ->getOptions () as $ option ) {
117
+ $ optionsByValue [$ option ['value ' ]] = $ option ;
118
+ }
119
+
120
+ $ values = explode (', ' , $ value );
121
+
122
+ foreach ($ values as $ valueInstance ) {
123
+ $ labels [] = isset ($ optionsByValue [$ valueInstance ])
124
+ ? $ optionsByValue [$ valueInstance ]['label ' ] : $ valueInstance ;
125
+
126
+ }
127
+ }
128
+
129
+ return $ labels ;
130
+ }
131
+
78
132
/**
79
133
* Gets array of scopes and scope IDs where path value is different
80
134
* than supplied context scope and context scope ID.
@@ -90,41 +144,43 @@ public function getOverriddenLevels($path, $contextScope, $contextScopeId) {
90
144
91
145
$ currentValue = $ this ->_getConfigValue ($ path , $ contextScope , $ contextScopeId );
92
146
93
- if (is_null ($ currentValue )) {
94
- return array (); //something is off, let's bail gracefully.
95
- }
96
-
97
147
$ overridden = array ();
98
148
99
149
switch ($ contextScope ) {
100
- case ' websites ' :
101
- $ stores = array_values ($ tree [' websites ' ][$ contextScopeId ][' stores ' ]);
150
+ case self :: WEBSITE_SCOPE_CODE :
151
+ $ stores = array_values ($ tree [self :: WEBSITE_SCOPE_CODE ][$ contextScopeId ][self :: STORE_VIEW_SCOPE_CODE ]);
102
152
foreach ($ stores as $ storeId ) {
103
- $ value = $ this ->_getConfigValue ($ path , ' stores ' , $ storeId );
153
+ $ value = $ this ->_getConfigValue ($ path , self :: STORE_VIEW_SCOPE_CODE , $ storeId );
104
154
if ($ value != $ currentValue ) {
105
155
$ overridden [] = array (
106
156
'scope ' => 'store ' ,
107
- 'scope_id ' => $ storeId
157
+ 'scope_id ' => $ storeId ,
158
+ 'value ' => $ value ,
159
+ 'display_value ' => $ this ->getConfigDisplayValue ($ path , self ::STORE_VIEW_SCOPE_CODE , $ storeId )
108
160
);
109
161
}
110
162
}
111
163
break ;
112
164
case 'default ' :
113
- foreach ($ tree [' websites ' ] as $ websiteId => $ website ) {
114
- $ websiteValue = $ this ->_getConfigValue ($ path , ' websites ' , $ websiteId );
165
+ foreach ($ tree [self :: WEBSITE_SCOPE_CODE ] as $ websiteId => $ website ) {
166
+ $ websiteValue = $ this ->_getConfigValue ($ path , self :: WEBSITE_SCOPE_CODE , $ websiteId );
115
167
if ($ websiteValue != $ currentValue ) {
116
168
$ overridden [] = array (
117
169
'scope ' => 'website ' ,
118
- 'scope_id ' => $ websiteId
170
+ 'scope_id ' => $ websiteId ,
171
+ 'value ' => $ websiteValue ,
172
+ 'display_value ' => $ this ->getConfigDisplayValue ($ path , self ::WEBSITE_SCOPE_CODE , $ websiteId )
119
173
);
120
174
}
121
175
122
- foreach ($ website [' stores ' ] as $ storeId ) {
123
- $ value = $ this ->_getConfigValue ($ path , ' stores ' , $ storeId );
176
+ foreach ($ website [self :: STORE_VIEW_SCOPE_CODE ] as $ storeId ) {
177
+ $ value = $ this ->_getConfigValue ($ path , self :: STORE_VIEW_SCOPE_CODE , $ storeId );
124
178
if ($ value != $ currentValue && $ value != $ websiteValue ) {
125
179
$ overridden [] = array (
126
180
'scope ' => 'store ' ,
127
- 'scope_id ' => $ storeId
181
+ 'scope_id ' => $ storeId ,
182
+ 'value ' => $ value ,
183
+ 'display_value ' => $ this ->getConfigDisplayValue ($ path , self ::STORE_VIEW_SCOPE_CODE , $ storeId )
128
184
);
129
185
}
130
186
}
@@ -135,6 +191,31 @@ public function getOverriddenLevels($path, $contextScope, $contextScopeId) {
135
191
return $ overridden ;
136
192
}
137
193
194
+ /**
195
+ * Get HTML formatted value label(s)
196
+ *
197
+ * @param array $labels
198
+ * @return string
199
+ */
200
+ protected function getFormattedValueLabels (array $ labels ) {
201
+ if (count ($ labels ) == 1 ) {
202
+ //if only one value, simply return it
203
+ return '<span class="override-value-hint-label"> ' .
204
+ nl2br ($ this ->escaper ->escapeHtml ($ labels [0 ])) .
205
+ '</span> ' ;
206
+ }
207
+
208
+ $ formattedLabels = '' ;
209
+
210
+ foreach ($ labels as $ label ) {
211
+ $ formattedLabels .= '<li class="override-value-hint-label"> ' .
212
+ nl2br ($ this ->escaper ->escapeHtml ($ label )) .
213
+ '</li> ' ;
214
+ }
215
+
216
+ return '<ul class="override-value-hint-labels"> ' . $ formattedLabels . '</ul> ' ;
217
+ }
218
+
138
219
/**
139
220
* Get HTML output for override hint UI
140
221
*
@@ -145,11 +226,13 @@ public function getOverriddenLevels($path, $contextScope, $contextScopeId) {
145
226
public function formatOverriddenScopes ($ section , array $ overridden ) {
146
227
$ formatted = '<div class="overridden-hint-wrapper"> ' .
147
228
'<p class="lead-text"> ' . __ ('This config field is overridden at the following scope(s): ' ) . '</p> ' .
148
- '<ul class="overridden-hint-list"> ' ;
229
+ '<dl class="overridden-hint-list"> ' ;
149
230
150
231
foreach ($ overridden as $ overriddenScope ) {
151
232
$ scope = $ overriddenScope ['scope ' ];
152
233
$ scopeId = $ overriddenScope ['scope_id ' ];
234
+ $ value = $ overriddenScope ['value ' ];
235
+ $ valueLabel = $ overriddenScope ['display_value ' ];
153
236
$ scopeLabel = $ scopeId ;
154
237
155
238
$ url = '# ' ;
@@ -170,6 +253,7 @@ public function formatOverriddenScopes($section, array $overridden) {
170
253
171
254
break ;
172
255
case 'store ' :
256
+ /** @var \Magento\Store\Model\Store $store */
173
257
$ store = $ this ->storeManager ->getStore ($ scopeId );
174
258
$ website = $ store ->getWebsite ();
175
259
$ url = $ this ->urlBuilder ->getUrl (
@@ -187,10 +271,14 @@ public function formatOverriddenScopes($section, array $overridden) {
187
271
break ;
188
272
}
189
273
190
- $ formatted .= '<li class=" ' . $ scope . '"> ' . $ scopeLabel .'</li> ' ;
274
+ $ formatted .=
275
+ '<dt class="override-scope ' . $ scope . '" title=" ' . __ ('Click to see overridden value ' ) .'"> '
276
+ . $ scopeLabel .
277
+ '</dt> ' .
278
+ '<dd class="override-value"> ' . $ this ->getFormattedValueLabels ($ valueLabel ) . '</dd> ' ;
191
279
}
192
280
193
- $ formatted .= '</ul ></div> ' ;
281
+ $ formatted .= '</dl ></div> ' ;
194
282
195
283
return $ formatted ;
196
284
}
0 commit comments