From f3eedc080a2c4c7faa7d3aed6efd9e7d77efcc38 Mon Sep 17 00:00:00 2001 From: pas Date: Thu, 24 Dec 2020 15:05:12 +0700 Subject: [PATCH] add render_selection option --- DependencyInjection/Configuration.php | 1 + Form/Type/Select2EntityType.php | 3 ++- README.md | 3 ++- Resources/public/js/select2entity.js | 7 ++++++- Resources/views/Form/fields.html.twig | 4 ++++ 5 files changed, 15 insertions(+), 3 deletions(-) diff --git a/DependencyInjection/Configuration.php b/DependencyInjection/Configuration.php index 5bd9542..54fb092 100644 --- a/DependencyInjection/Configuration.php +++ b/DependencyInjection/Configuration.php @@ -42,6 +42,7 @@ public function getConfigTreeBuilder() ->scalarNode('width')->defaultNull()->end() ->scalarNode('object_manager')->defaultNull()->end() ->booleanNode('render_html')->defaultFalse()->end() + ->booleanNode('render_selection')->defaultFalse()->end() ->end(); return $treeBuilder; diff --git a/Form/Type/Select2EntityType.php b/Form/Type/Select2EntityType.php index ad7f8d4..7ee52d6 100644 --- a/Form/Type/Select2EntityType.php +++ b/Form/Type/Select2EntityType.php @@ -175,7 +175,8 @@ public function configureOptions(OptionsResolver $resolver) 'callback' => null, 'class_type' => null, 'query_parameters' => [], - 'render_html' => $this->config['render_html'] ?? false + 'render_html' => $this->config['render_html'] ?? false, + 'render_selection' => $this->config['render_selection'] ?? false ] ); } diff --git a/README.md b/README.md index 377ecd1..1fef79d 100644 --- a/README.md +++ b/README.md @@ -139,7 +139,8 @@ If text_property is omitted then the entity is cast to a string. This requires i * `autostart` Determines whether or not the select2 jQuery code is called automatically on document ready. Defaults to true which provides normal operation. * `width` Sets a data-width attribute if not null. Defaults to null. * `class_type` Optional value that will be added to the ajax request as a query string parameter. -* `render_html` This will render your results returned under ['html']. +* `render_html` This will render your results returned under ['html']. +* `render_selection` This will render your selection returned under ['html']. The url of the remote query can be given by either of two ways: `remote_route` is the Symfony route. `remote_params` can be optionally specified to provide parameters. Alternatively, `remote_path` can be used to specify diff --git a/Resources/public/js/select2entity.js b/Resources/public/js/select2entity.js index 6bc1b97..7041cd1 100644 --- a/Resources/public/js/select2entity.js +++ b/Resources/public/js/select2entity.js @@ -11,6 +11,7 @@ prefix = Date.now(), query_parameters = $s2.data('query-parameters'), render_html = $s2.data('render-html'), + render_selection = $s2.data('render-selection'), cache = []; let reqParams = $s2.data('req_params'); @@ -130,7 +131,11 @@ return option.html ? option.html : option.text; }, templateSelection: function (option) { - return option.text; + if (render_selection) { + return option.html ? option.html : option.text; + } else { + return option.text; + } } }, mergedOptions); } diff --git a/Resources/views/Form/fields.html.twig b/Resources/views/Form/fields.html.twig index c508e7e..4e52aaf 100644 --- a/Resources/views/Form/fields.html.twig +++ b/Resources/views/Form/fields.html.twig @@ -47,6 +47,10 @@ {% set attr = attr|merge({'data-render-html': 'true'}) %} {% endif %} + {% if render_selection %} + {% set attr = attr|merge({'data-render-selection': 'true'}) %} + {% endif %} + {% if class_type %} {% set attr = attr|merge({'data-classtype': class_type}) %} {% endif %}