getAttribute('formatResult'); $formatCondition = null; if (!empty($formatResult)) { $formatCondition = ', templateResult : '.$formatResult.', templateSelection : '.$formatResult; } $defaultValues = $this->getAttribute('defaults'); $defaultValues = empty($defaultValues) ? [] : $defaultValues; $width = 'element'; $givenWidth = '300'; if (!empty($givenWidth)) { $width = $givenWidth; } //Get the minimumInputLength for select2 $minimumInputLength = $this->getAttribute('minimumInputLength') > 3 ? $this->getAttribute('minimumInputLength') : 3 ; $plHolder = $this->getAttribute('placeholder'); if (empty($plHolder)) { $plHolder = get_lang('SelectAnOption'); } $html .= << $(function(){ $('#{$this->getAttribute('name')}').select2({ $languageCondition placeholder_: '$plHolder', allowClear: true, width: '$width', minimumInputLength: '$minimumInputLength', // instead of writing the function to execute the request we use Select2s convenient helper ajax: { url: '{$this->getAttribute('url')}', dataType: 'json', data: function(params) { return { q: params.term, // search term page_limit: 10, }; }, processResults: function (data, page) { //parse the results into the format expected by Select2 return { results: data.items }; } $formatCondition } }); }); JS; $html .= Display::select( $this->getAttribute('name'), $defaultValues, array_keys($defaultValues), [ 'id' => $this->getAttribute('name'), 'style' => 'width: 100%;' ], false ); return $html; } }