18
18
use yii \helpers \ArrayHelper ;
19
19
use yii \helpers \Html ;
20
20
use yii \helpers \Inflector ;
21
- use yii \helpers \Json ;
22
21
use unclead \multipleinput \renderers \BaseRenderer ;
23
22
24
23
/**
@@ -332,15 +331,8 @@ public function renderInput($name, $options, $contextParams = [])
332
331
$ method = 'render ' . Inflector::camelize ($ this ->type );
333
332
334
333
// @see https://github.com/unclead/yii2-multiple-input/issues/261
335
- // we have to replace placeholder in all nested options and the best way to do it is:
336
- // 1) encode the options to a json string
337
- // 2) replace the placeholder
338
- // 3) decode the json string back to array
339
- // But we have to leave the placeholder in case of processing a row template
340
334
if ($ contextParams ['index ' ] !== null ) {
341
- $ options_string = Json::encode ($ options );
342
- $ options_string = str_replace ('{ ' . $ contextParams ['indexPlaceholder ' ] . '} ' , $ contextParams ['index ' ], $ options_string );
343
- $ options = Json::decode ($ options_string );
335
+ $ options = $ this ->replaceIndexPlaceholderInOptions ($ options , $ contextParams ['indexPlaceholder ' ], $ contextParams ['index ' ]);
344
336
}
345
337
346
338
$ value = null ;
@@ -361,6 +353,19 @@ public function renderInput($name, $options, $contextParams = [])
361
353
return strtr ($ this ->inputTemplate , ['{input} ' => $ input ]);
362
354
}
363
355
356
+ private function replaceIndexPlaceholderInOptions ($ options , $ indexPlaceholder , $ index )
357
+ {
358
+ $ result = [];
359
+ foreach ($ options as $ key => $ value ) {
360
+ if (is_array ($ value )) {
361
+ $ result [$ key ] = $ this ->replaceIndexPlaceholderInOptions ($ value , $ indexPlaceholder , $ index );
362
+ } elseif (is_string ($ value )) {
363
+ $ result [$ key ] = str_replace ('{ ' . $ indexPlaceholder . '} ' , $ index , $ value );
364
+ }
365
+ }
366
+
367
+ return $ result ;
368
+ }
364
369
365
370
/**
366
371
* Renders drop down list.
0 commit comments