Skip to content
Snippets Groups Projects
Commit c72793d9 authored by David Galeano's avatar David Galeano
Browse files

Issue #3401241 by gxleano: Fix Number of result when 'On click' option is...

parent 066bdfea
No related branches found
Tags 7.x-4.0-rc1
1 merge request!45Issue #3401241 by gxleano: Fix Number of result when 'On click' option is...
Pipeline #48875 passed
......@@ -64,53 +64,60 @@
tagify.updateValueByDOMTags()
}
function handleAutocomplete(value){
function handleAutocomplete(value, selectedEntities){
tagify.whitelist = null;
// https://developer.mozilla.org/en-US/docs/Web/API/AbortController/abort
controller && controller.abort();
controller = new AbortController();
// Show loading animation meanwhile the dropdown suggestions are hided.
value !== '' ? tagify.loading(true) : tagify.loading(false);
fetch($(input).attr('data-autocomplete-url') + '?q=' + encodeURIComponent(value), {signal: controller.signal})
.then(res => res.json())
.then(function (newWhitelist) {
fetch($(input).attr('data-autocomplete-url') + '?q=' + encodeURIComponent(value) + '&selected=' + selectedEntities, {signal: controller.signal})
.then(res => res.json())
.then(function (newWhitelist) {
let newWhitelistData = [];
newWhitelist.forEach(function (current) {
newWhitelistData.push({
value: current.label,
entity_id: current.entity_id,
...current.attributes
});
newWhitelist.forEach(function (current) {
newWhitelistData.push({
value: current.label,
entity_id: current.entity_id,
...current.attributes
});
});
if (newWhitelistData) {
// build the whitelist with the values coming from the fetch
tagify.whitelist = newWhitelistData; // update whitelist Array in-place
tagify.loading(false).dropdown.show(value) // render the suggestions dropdown
});
}
}).catch(error => {
console.error('Error fetching data:', error);
});
}
// onInput event.
let onInput = Drupal.debounce(function (e) {
let value = e.detail.value;
handleAutocomplete(value);
handleAutocomplete(value, tagify.value.map(item => item.entity_id));
}, 500);
// onEditInput event.
let onEditInput = Drupal.debounce(function (e) {
let value = e.detail.data.newValue;
handleAutocomplete(value);
}, 500);
// onFocusInput event.
let onFocusInput = Drupal.debounce(function () {
handleAutocomplete('');
handleAutocomplete(value, tagify.value.map(item => item.entity_id));
}, 500);
// Edit input event (When user is editing the tag).
tagify.on('edit:input', onEditInput)
// Input event (When user is creating the tag).
tagify.on('input', onInput)
// Focus input event (When user interacts with the field).
tagify.on('focus', onFocusInput)
// If 'On click' dropdown suggestions is enabled.
if (!tagify.settings.dropdown.enabled) {
// Getting 'tagify__input' element.
const tagifyInput = document.querySelector('.tagify__input');
tagifyInput.addEventListener('click', function() {
handleAutocomplete('', tagify.value.map(item => item.entity_id));
});
}
});
}
};
......
......@@ -74,13 +74,16 @@ class TagifyEntityAutocompleteController extends ControllerBase {
*/
public function handleAutocomplete(Request $request, $target_type, $selection_handler, $selection_settings_key) {
$matches = [];
// Get already selected entity ids.
$selected = $request->query->get('selected')
? explode(',', $request->query->get('selected'))
: [];
// Get the typed string from the URL, if it exists.
$input = $request->query->get('q');
if ($input !== NULL) {
// Selection settings are passed in as a hashed key of a serialized array
// stored in the key/value store.
$selection_settings = $this->keyValue('entity_autocomplete')->get($selection_settings_key, FALSE);
// Validate the autocomplete minimum length.
if ($input === '' && $selection_settings['suggestions_dropdown'] !== 0) {
return new JsonResponse([]);
......@@ -99,7 +102,7 @@ class TagifyEntityAutocompleteController extends ControllerBase {
// key/value store.
throw new AccessDeniedHttpException();
}
$matches = $this->matcher->getMatches($target_type, $selection_handler, $selection_settings, mb_strtolower($input), $request->query->all('selected'));
$matches = $this->matcher->getMatches($target_type, $selection_handler, $selection_settings, mb_strtolower($input), $selected);
}
return new JsonResponse($matches);
......
......@@ -85,18 +85,14 @@ class TagifyEntityAutocompleteMatcher implements TagifyEntityAutocompleteMatcher
// Get an array of matching entities.
$match_operator = !empty($selection_settings['match_operator']) ? $selection_settings['match_operator'] : 'CONTAINS';
$match_limit = isset($selection_settings['match_limit']) ? (int) $selection_settings['match_limit'] : 10;
$entity_labels = $handler->getReferenceableEntities($string, $match_operator, $match_limit + count($selected));
$entity_labels = $handler->getReferenceableEntities($string, $match_operator, ($match_limit === 0) ? $match_limit : $match_limit + count($selected));
// Loop through the entities and convert them into autocomplete output.
foreach ($entity_labels as $bundle => $values) {
foreach ($values as $entity_id => $label) {
// Filter out already selected items.
if (in_array($entity_id, $selected)) {
continue;
}
$matches[$entity_id] = $this->buildTagifyItem($entity_id, $label, $bundle);
}
}
......@@ -108,7 +104,6 @@ class TagifyEntityAutocompleteMatcher implements TagifyEntityAutocompleteMatcher
$this->moduleHandler->alter('tagify_autocomplete_matches', $matches, $options);
}
return array_values($matches);
}
......@@ -131,7 +126,7 @@ class TagifyEntityAutocompleteMatcher implements TagifyEntityAutocompleteMatcher
return [
'entity_id' => $entity_id,
'label' => Html::decodeEntities($label),
'type' => $bundle
'type' => $bundle,
];
}
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment