Problem/Motivation

The README shows how to turn a views exposed filter into an Autocomplete Deluxe element, but the example was written against an older API. It adds the styling and the element renders, while the autocomplete route is never reached, so nothing is suggested. The example also carries a typo, auto_crteate_bundle, and a line that is not needed.

Steps to reproduce

  1. Add a taxonomy filter to a view and expose it.
  2. Follow the README example in a custom module.
  3. The field looks right and no suggestion comes back.

Proposed resolution

Rewrite the example against the current code, and document the same wiring for a form of your own. The part that matters is that the selection settings have to be in the entity_autocomplete key value store under their hash before the route is called.

The exposed filter of a view rendered as the Autocomplete Deluxe element

Remaining tasks

  • ✅ File an issue
  • ✅ Addition/Change/Update/Fix
  • ✅ Testing to ensure no regression
  • ➖ Automated unit testing coverage
  • ➖ Automated functional testing coverage
  • ➖ UX/UI designer responsibilities
  • ✅ Readability
  • ➖ Accessibility
  • ➖ Performance
  • ➖ Security
  • ✅ Developer Documentation
  • ➖ User Guide Documentation
  • ➖ Reviewed by human
  • ➖ Code review by maintainers
  • ➖ Full testing and approval
  • ➖ Credit contributors
  • ➖ Review with the product owner
  • ➖ Release notes snippet
  • ❌ Release

User interface changes

  • N/A

API changes

  • N/A

Data model changes

  • N/A

Release notes snippet

  • The README examples for a views exposed filter and for a form of your own are up to date.
Command icon Show commands

Start within a Git clone of the project using the version control instructions.

Or, if you do not have SSH keys set up on git.drupalcode.org:

Comments

liquidcms created an issue. See original summary.

rajab natshah’s picture

Title: README is out of date. » Change out of date README.md file

Thank you, Peter, for reporting.
Indeed it is out of date.

Please, advise and help with that.
Do you suggest any changes?

Anyone can do a basic ( Create Issue fork for this ) and use the Web IDE from Gitlab.

Changes are welcomed and they will be committed :)

rajab natshah’s picture

Title: Change out of date README.md file » Change out of dated README.md file
rajab natshah’s picture

Version: 2.0.2 » 2.0.x-dev
Status: Active » Needs work
suchdavid’s picture

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.

suchdavid’s picture

Title: Change out of dated README.md file » Views exposed filter example. README.md file is not up to date.
Status: Needs work » Needs review

thakurnishant_06 made their first commit to this issue’s fork.

proteo’s picture

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 if 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' => FALSE,
    '#min_length' => FALSE,
    '#delimiter' => ',',
    '#not_found_message_allow' => FALSE,
    '#not_found_message' => "The term '@term' will be added.",
    '#cardinality' => -1,
  ];
}

rajab natshah’s picture

Version: 2.0.x-dev » 2.1.x-dev
Assigned: Unassigned » rajab natshah
Issue summary: View changes
StatusFileSize
new57.44 KB

Thank you, David for tracing it to the key value store, Nishant for the first commit on the fork, and Proteo for the working code.

Built both examples in a scratch module on Drupal 11.4 and ran them.

A form of my own: the element renders, the suggestion list comes back from the controller, a tag is added and the form submits. It also showed the undefined array key warning for #cardinality, which is #3501341: Fix Warning: Undefined array key cardinality and has a merge request of its own.

A view with an exposed taxonomy filter: the element replaces the filter, suggestions come back, and applying the filter with Drupal (7) returns exactly the three nodes tagged with that term.

The merge request rewrites the FAQ with both examples, drops the dead auto_create_bundle line and its typo, and builds the route through the URL generator.

The exposed filter of a view rendered as the Autocomplete Deluxe element

To review, test, then merge.

  • rajab natshah committed cc422ea4 on 2.1.x
    docs: #3307367 Update the readme examples for the element and the...
rajab natshah’s picture

Status: Needs review » Fixed

Now that this issue is closed, review the contribution record.

As a contributor, attribute any organization that helped you, or if you volunteered your own time.

Maintainers, credit people who helped resolve this issue.

rajab natshah’s picture

Assigned: rajab natshah » Unassigned

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.