Change out of dated README.md file

Created on 1 September 2022, almost 3 years ago
Updated 24 November 2023, over 1 year ago

Problem/Motivation

The README shows an example of how to alter a form field to set as an autocomplete_deluxe form element. But i think that is possibly for D7? I have tried the example code (removing bit about adding '_targer_id' as that made no sense) and, although it adds some styling to the element, the controller is never hit and therefore there are no options returned when typing.

Is it possible that the D8 version does not support altering an existing form element?

Steps to reproduce

Proposed resolution

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
  • ➖ Documentation
  • ➖ 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

  • N/A
🐛 Bug report
Status

Needs work

Version

2.0

Component

Code (miscellaneous)

Created by

🇨🇦Canada liquidcms

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Comments & Activities

Not all content is available!

It's likely this issue predates Contrib.social: some issue and comment data are missing.

  • 🇭🇺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
  • 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,
      ];
    }
    
    
Production build 0.71.5 2024