Skip to content

Commit

Permalink
Fixes starts with v/s Contains. Also enforces min match limit + Compl…
Browse files Browse the repository at this point in the history
…ete limit

I just wonder here if we need to sort on the "contains" on the position of appearance of the query @alliomeria ? Right now i am listing back the autocomplete list based on the actual "ocurrence" in the ROWS. So row 2 in the CSV appears first than row 10... (kinda obvious)
  • Loading branch information
DiegoPino committed Jun 18, 2024
1 parent 17f83aa commit 1d27cfe
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 15 deletions.
42 changes: 28 additions & 14 deletions src/Controller/RowAutocompleteController.php
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ public static function create(ContainerInterface $container) {
* Filters against Labels
*
*/
public function handleAutocomplete(Request $request, ContentEntityInterface $node, $label_header, $url_header) {
public function handleAutocomplete(Request $request, ContentEntityInterface $node, $label_header, $url_header, $match = 'STARTS_WITH', $limit = 10, $min = 2) {
$results = [];
$input = $request->query->get('q');
$input = Xss::filter($input);
Expand All @@ -76,14 +76,15 @@ public function handleAutocomplete(Request $request, ContentEntityInterface $nod

// Find a CSV file in this ADO.
// Get the typed string from the URL, if it exists.
if (!$input) {
if (!$input && strlen(trim($input)) < $min) {
return new JsonResponse($results);
}

$file = null;
if ($sbf_fields = \Drupal::service('strawberryfield.utility')->bearsStrawberryfield($node)) {
$files = $node->get('field_file_drop')->getValue();
foreach($files as $offset => $fileinfo) {
/** @var \Drupal\file\FileInterface $file|null */
foreach ($files as $offset => $fileinfo) {
/** @var \Drupal\file\FileInterface $file |null */
$file = $this->entityTypeManager
->getStorage('file')
->load($fileinfo['target_id']);
Expand All @@ -98,18 +99,31 @@ public function handleAutocomplete(Request $request, ContentEntityInterface $nod
$label_original_index = array_search($label_header, $column_keys);
$url_original_index = array_search($url_header, $column_keys);
$i = 0;
if ($label_original_index !== FALSE && $url_original_index !== FALSE ) {
if ($label_original_index !== FALSE && $url_original_index !== FALSE) {
foreach ($file_data_all['data'] as $id => &$row) {
if (isset($row[$label_original_index]) && stripos($row[$label_original_index], $input) === 0) {
$i++;
if (isset($row[$label_original_index])) {

if ($match == 'STARTS_WITH' && stripos($row[$label_original_index], $input) === 0) {
$i++;

$results[] = [
'value' => $row[$url_original_index],
'label' => $row[$label_original_index],
];
if ($i == 10) {
break;
}
$results[] = [
'value' => $row[$url_original_index],
'label' => $row[$label_original_index],
];
if ($i == $limit) {
break;
}
} else
if ($match == 'CONTAINS' && stripos($row[$label_original_index], $input) !== FALSE) {
$i++;
$results[] = [
'value' => $row[$url_original_index],
'label' => $row[$label_original_index],
];
if ($i == $limit) {
break;
}
}
}
}
}
Expand Down
3 changes: 3 additions & 0 deletions src/Plugin/WebformElement/WebformLoDfromCSV.php
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,9 @@ public function prepare(
'node' => $element['#autocomplete_items'],
'label_header' => $element['#autocomplete_label_header'] ?? $properties['autocomplete_label_header'],
'url_header' => $element['#autocomplete_url_header'] ?? $properties['autocomplete_url_header'],
'match' => $element['#autocomplete_match_operator'] ?? $properties['autocomplete_match_operator'],
'limit' => $element['#autocomplete_limit'] ?? $properties['autocomplete_limit'],
'min' => $element['#autocomplete_match'] ?? $properties['autocomplete_match'],
];
}
}
Expand Down
5 changes: 4 additions & 1 deletion webform_strawberryfield.routing.yml
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ webform_strawberryfield.element.autocomplete:
_entity_access: 'webform.submission_create'

webform_strawberryfield.rowsbylabel.autocomplete:
path: '/webform_strawberry/csv_autocomplete/{node}/{label_header}/{url_header}'
path: '/webform_strawberry/csv_autocomplete/{node}/{label_header}/{url_header}/{match}/{limit}/{min}'
options:
parameters:
node:
Expand All @@ -70,6 +70,9 @@ webform_strawberryfield.rowsbylabel.autocomplete:
defaults:
label_header: 'label'
url_header: 'url'
match: 'STARTS_WITH'
limit: 10
min: 2
_controller: '\Drupal\webform_strawberryfield\Controller\RowAutocompleteController::handleAutocomplete'
_format: json
requirements:
Expand Down

0 comments on commit 1d27cfe

Please sign in to comment.