Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added support for select2 tags #8213

Merged
merged 2 commits into from
Nov 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions assets/js/admin.js
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ const Admin = {
const popover = select.data('popover');
let maximumSelectionLength = null;
let minimumResultsForSearch = 10;
let allowTags = false;

select.removeClass('form-control');

Expand All @@ -101,6 +102,10 @@ const Admin = {
allowClearEnabled = false;
}

if (select.attr('data-sonata-select2-allow-tags') === 'true') {
allowTags = true;
}

if (select.attr('data-sonata-select2-maximumSelectionLength')) {
maximumSelectionLength = select.attr('data-sonata-select2-maximumSelectionLength');
}
Expand All @@ -117,6 +122,7 @@ const Admin = {
placeholder: allowClearEnabled ? ' ' : '', // allowClear needs placeholder to work properly
allowClear: allowClearEnabled,
maximumSelectionLength,
tags: allowTags,
});

if (undefined !== popover) {
Expand Down
23 changes: 23 additions & 0 deletions docs/cookbook/recipe_select2.rst
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,29 @@ to enable ``allowClear`` or ``data-sonata-select2-allow-clear = "false"`` to dis

You have to use false as string! ``"false"``!

AllowTags
----------

Select2 parameter ``allowTags`` can be set using the data attribute ``data-sonata-select2-allow-tags="true"``
to enable ``allowTags``::

use Sonata\AdminBundle\Form\Type\ModelType;

protected function configureFormFields(FormMapper $form): void
{
$form
->add('category', ModelType::class, [
'attr' => [
'data-sonata-select2-allow-tags' => 'true'
]
])
;
}

.. note::

You have to use true as string! ``"true"``!

Minimum results for search
--------------------------

Expand Down
2 changes: 1 addition & 1 deletion src/Resources/public/app.js

Large diffs are not rendered by default.