- 🇭🇺Hungary suchdavid
Hi, I think the problem is mainly, that a keyvalue is needed to be saved on the backend possibly for security reasons.
$key_value_storage = \Drupal::service('keyvalue')->get('entity_autocomplete'); if (!$key_value_storage->has($selection_settings_key)) { $key_value_storage->set($selection_settings_key, $selection_settings); }
And a the url should be generated this way:
$url = Drupal\core\Url::fromRoute( 'autocomplete_deluxe.autocomplete', $route_parameters, ['absolute' => TRUE] )->getInternalPath();
With these modifications it worked for me.
- Status changed to Needs review
over 1 year ago 9:24am 24 November 2023 - First commit to issue fork.
As of Drupal 11.1.7 and Autocomplete Deluxe 2.1.2, the previous code no longer works. Here is an updated version that is working for me. I can create a patch for the readme file is the maintainer is interested:
use Drupal\Component\Utility\Crypt; use Drupal\Core\Site\Settings; /** * Implements hook_form_alter(). */ function your_module_form_alter(&$form, FormStateInterface $form_state, $form_id) { if ($form_id == 'views_exposed_form') { // Add a call for each exposed filter you want to alter, using the field's machine name. your_module_autocomplete_deluxe_filter('field_tags', $form); your_module_autocomplete_deluxe_filter('field_categories', $form); } } /** * Converts the exposed filter of a field into an autocomplete deluxe widget. * * @param string $field_name * Name of the field included as exposed filter in the form. * @param array $form * Instance of the form array. */ function your_module_autocomplete_deluxe_filter(string $field_name, array &$form) { $filter_id = $form['#info']['filter-' . $field_name . '_target_id']['value'] ?? FALSE; if (!isset($form[$filter_id])) { return; } $selection_settings = [ 'target_bundles' => $form[$filter_id]['#selection_settings']['target_bundles'], 'sort' => ['field' => '_none'], 'auto_create' => FALSE, // Even though we've specified '0' for 'auto_create', it seems that // a value for 'auto_crteate_bundle' is required for this to work. 'auto_create_bundle' => 'tags', 'match_operator' => 'CONTAINS', ]; $target_type = $form[$filter_id]['#target_type']; $selection_handler = 'default:taxonomy_term'; $data = serialize($selection_settings) . $target_type . $selection_handler; $selection_settings_key = Crypt::hmacBase64($data, Settings::getHashSalt()); $key_value_storage = \Drupal::service('keyvalue')->get('entity_autocomplete'); if (!$key_value_storage->has($selection_settings_key)) { $key_value_storage->set($selection_settings_key, $selection_settings); } $route_parameters = [ 'target_type' => $target_type, 'selection_handler' => $selection_handler, 'selection_settings_key' => $selection_settings_key, ]; $url = \Drupal::urlGenerator()->getPathFromRoute('autocomplete_deluxe.autocomplete', $route_parameters); $form[$filter_id] = [ '#type' => 'autocomplete_deluxe', '#autocomplete_deluxe_path' => $url, '#selection_settings' => $selection_settings, '#multiple' => TRUE, '#target_type' => $target_type, '#selection_handler' => $selection_handler, '#limit' => 10, '#size' => 60, '#new_terms' => 0, '#min_length' => 0, '#delimiter' => ',', '#not_found_message_allow' => 0, '#not_found_message' => "The term '@term' will be added.", '#cardinality' => -1, ]; }