Skip to content

Commit 6370073

Browse files
committed
add render_html as configurable option
1 parent 5eb4f5e commit 6370073

File tree

5 files changed

+37
-4
lines changed

5 files changed

+37
-4
lines changed

DependencyInjection/Configuration.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ public function getConfigTreeBuilder()
4040
->integerNode('cache_timeout')->defaultValue(60000)->min(0)->end()
4141
->scalarNode('width')->defaultNull()->end()
4242
->scalarNode('object_manager')->defaultNull()->end()
43+
->booleanNode('render_html')->defaultFalse()->end()
4344
->end();
4445

4546
return $treeBuilder;

Form/Type/Select2EntityType.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -173,7 +173,8 @@ public function configureOptions(OptionsResolver $resolver)
173173
'property' => null,
174174
'callback' => null,
175175
'class_type' => null,
176-
'query_parameters' => []
176+
'query_parameters' => [],
177+
'render_html' => $this->config['render_html'] ?? false
177178
]
178179
);
179180
}

README.md

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,7 @@ If text_property is omitted then the entity is cast to a string. This requires i
138138
* `autostart` Determines whether or not the select2 jQuery code is called automatically on document ready. Defaults to true which provides normal operation.
139139
* `width` Sets a data-width attribute if not null. Defaults to null.
140140
* `class_type` Optional value that will be added to the ajax request as a query string parameter.
141+
* `render_html` This will render your results returned under ['html'].
141142

142143
The url of the remote query can be given by either of two ways: `remote_route` is the Symfony route.
143144
`remote_params` can be optionally specified to provide parameters. Alternatively, `remote_path` can be used to specify
@@ -158,6 +159,7 @@ tetranz_select2_entity:
158159
cache_timeout: 0
159160
scroll: true
160161
object_manager: 'manager_alias'
162+
render_html: true
161163
```
162164
163165
## AJAX Response
@@ -340,6 +342,15 @@ Because the handling of requests is usually very similar you can use a service w
340342

341343
### Templating
342344

345+
General templating has now been added to the bundle. If you need to render html code inside your selection results, set the `render_html` option to true and in your controller return data like this:
346+
```javascript
347+
[
348+
{ id: 1, text: 'United Kingdom (Europe)', html: '<img src="images/flags/en.png" />' },
349+
{ id: 2, text: 'China (Asia)', html: '<img src="images/flags/ch.png">' }
350+
]
351+
```
352+
353+
<details><summary>If you need further templating, you'll need to override the .select2entity() method as follows.</summary>
343354
If you need [Templating](https://select2.org/dropdown#templating) in Select2, you could consider the following example that shows the country flag next to each option.
344355

345356
Your custom transformer should return data like this:
@@ -395,7 +406,7 @@ You also will need to override the following block in your template:
395406
</option>
396407
{% endblock %}
397408
```
398-
This block adds all additional data needed to the JavaScript function `select2entityAjax`, like data attribute. In this case we are passing `data-img`.
409+
This block adds all additional data needed to the JavaScript function `select2entityAjax`, like data attribute. In this case we are passing `data-img`.</details>
399410

400411
## Embed Collection Forms
401412
If you use [Embedded Collection Forms](http://symfony.com/doc/current/cookbook/form/form_collections.html) and [data-prototype](http://symfony.com/doc/current/cookbook/form/form_collections.html#allowing-new-tags-with-the-prototype) to add new elements in your form, you will need the following JavaScript that will listen for adding an element `.select2entity`:

Resources/public/js/select2entity.js

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
scroll = $s2.data('scroll'),
1111
prefix = Date.now(),
1212
query_parameters = $s2.data('query-parameters'),
13+
render_html = $s2.data('render-html'),
1314
cache = [];
1415

1516
let reqParams = $s2.data('req_params');
@@ -23,7 +24,7 @@
2324
}
2425

2526
// Deep-merge the options
26-
$s2.select2($.extend(true, {
27+
let mergedOptions = $.extend(true, {
2728
// Tags support
2829
createTag: function (data) {
2930
if ($s2.data('tags') && data.term.length > 0) {
@@ -119,7 +120,22 @@
119120
return response;
120121
}
121122
}
122-
}, options || {}));
123+
}, options || {});
124+
if (render_html) {
125+
mergedOptions = $.extend({
126+
escapeMarkup: function (text) {
127+
return text;
128+
},
129+
templateResult: function (option) {
130+
return option.html ? option.html : option.text;
131+
},
132+
templateSelection: function (option) {
133+
return option.text;
134+
}
135+
}, mergedOptions);
136+
}
137+
138+
$s2.select2(mergedOptions);
123139
});
124140
return this;
125141
};

Resources/views/Form/fields.html.twig

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,10 @@
4242
{% set attr = attr|merge({'data-width': width}) %}
4343
{% endif %}
4444

45+
{% if render_html %}
46+
{% set attr = attr|merge({'data-render-html': 'true'}) %}
47+
{% endif %}
48+
4549
{% if class_type %}
4650
{% set attr = attr|merge({'data-classtype': class_type}) %}
4751
{% endif %}

0 commit comments

Comments
 (0)