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 callback with possibility to modify each individual item in res… #6789

Merged
merged 3 commits into from
Jan 20, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
10 changes: 9 additions & 1 deletion src/Action/RetrieveAutocompleteItemsAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ public function __invoke(Request $request): JsonResponse
$reqParamPageNumber = $filterAutocomplete->getFieldOption('req_param_name_page_number', '_page');
$toStringCallback = $filterAutocomplete->getFieldOption('to_string_callback');
$targetAdminAccessAction = $filterAutocomplete->getFieldOption('target_admin_access_action', 'list');
$responseItemCallback = $filterAutocomplete->getFieldOption('response_item_callback');
} else {
// create/edit form
$fieldDescription = $this->retrieveFormFieldDescription($admin, $request->get('field'));
Expand All @@ -88,6 +89,7 @@ public function __invoke(Request $request): JsonResponse
$reqParamPageNumber = $formAutocompleteConfig->getAttribute('req_param_name_page_number');
$toStringCallback = $formAutocompleteConfig->getAttribute('to_string_callback');
$targetAdminAccessAction = $formAutocompleteConfig->getAttribute('target_admin_access_action');
$responseItemCallback = $formAutocompleteConfig->getAttribute('response_item_callback');
}

$searchText = $request->get('q', '');
Expand Down Expand Up @@ -173,10 +175,16 @@ public function __invoke(Request $request): JsonResponse
$label = $resultMetadata->getTitle();
}

$items[] = [
$item = [
'id' => $admin->id($model),
'label' => $label,
];

if (\is_callable($responseItemCallback)) {
\call_user_func($responseItemCallback, $admin, $model, $item);
}

$items[] = $item;
}

return new JsonResponse([
Expand Down
2 changes: 2 additions & 0 deletions src/Form/Type/ModelAutocompleteType.php
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,7 @@ public function configureOptions(OptionsResolver $resolver)
$resolver->setDefaults([
'attr' => [],
'compound' => $compound,
'error_bubbling' => false,
'model_manager' => null,
'class' => null,
'admin_code' => null,
Expand All @@ -123,6 +124,7 @@ public function configureOptions(OptionsResolver $resolver)
'cache' => false,

'to_string_callback' => null,
'response_item_callback' => null,

// add button
// NEXT_MAJOR: Set this value to 'link_add' to display button by default
Expand Down
14 changes: 8 additions & 6 deletions src/Resources/public/Admin.js
Original file line number Diff line number Diff line change
Expand Up @@ -571,22 +571,24 @@ var Admin = {
return '100%';
},

setup_sortable_select2: function(subject, data) {
setup_sortable_select2: function(subject, data, customOptions) {
var transformedData = [];
for (var i = 0 ; i < data.length ; i++) {
transformedData[i] = {id: data[i].data, text: data[i].label};
}

subject.select2({
var options = Object.assign({
width: function(){
// Select2 v3 and v4 BC. If window.Select2 is defined, then the v3 is installed.
// NEXT_MAJOR: Remove Select2 v3 support.
return Admin.get_select2_width(window.Select2 ? this.element : subject);
// Select2 v3 and v4 BC. If window.Select2 is defined, then the v3 is installed.
// NEXT_MAJOR: Remove Select2 v3 support.
return Admin.get_select2_width(window.Select2 ? this.element : subject);
OskarStark marked this conversation as resolved.
Show resolved Hide resolved
},
dropdownAutoWidth: true,
data: transformedData,
multiple: true
});
}, customOptions);

subject.select2(options);

subject.select2("container").find("ul.select2-choices").sortable({
containment: 'parent',
Expand Down
6 changes: 5 additions & 1 deletion src/Resources/views/Form/form_admin_fields.html.twig
Original file line number Diff line number Diff line change
Expand Up @@ -577,7 +577,11 @@ file that was distributed with this source code.

<script>
jQuery(document).ready(function() {
Admin.setup_sortable_select2(jQuery('#{{ id }}'), {{ form.vars.choices|json_encode|raw }});
var options = {};

{% block sonata_type_model_autocomplete_select2_options_js %}{% endblock %}

Admin.setup_sortable_select2(jQuery('#{{ id }}'), {{ form.vars.choices|json_encode|raw }}, options);
});
</script>
{% endblock %}
3 changes: 3 additions & 0 deletions tests/Action/RetrieveAutocompleteItemsActionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -292,6 +292,7 @@ private function configureFormConfig(string $field, bool $disabled = false): voi
['req_param_name_page_number', null, '_page'],
['to_string_callback', null, null],
['target_admin_access_action', null, 'list'],
['response_item_callback', null, null],
]);
}

Expand All @@ -312,6 +313,7 @@ private function configureFormConfigComplexProperty(string $field): void
['items_per_page', null, 10],
['req_param_name_page_number', null, '_page'],
['target_admin_access_action', null, 'list'],
['response_item_callback', null, null],
]);
}

Expand All @@ -332,6 +334,7 @@ private function configureFormConfigComplexPropertyArray(string $field): void
['items_per_page', null, 10],
['req_param_name_page_number', null, '_page'],
['target_admin_access_action', null, 'list'],
['response_item_callback', null, null],
]);
}
}
1 change: 1 addition & 0 deletions tests/Form/Type/ModelAutocompleteTypeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ public function testGetDefaultOptions(): void
$this->assertSame('_per_page', $options['req_param_name_items_per_page']);

$this->assertSame('list', $options['target_admin_access_action']);
$this->assertNull($options['response_item_callback']);

$this->assertSame('', $options['container_css_class']);
$this->assertSame('', $options['dropdown_css_class']);
Expand Down