Add ability to pass options to the autocomplete route URL in Form Element

Created on 24 June 2022, about 2 years ago
Updated 2 October 2023, 9 months ago

Problem/Motivation

Now the function Drupal\Core\Render\Element\FormElement::processAutocomplete() gets only the $element['#autocomplete_route_parameters'] parameter from the form element, that allows only strict list of route parameters.

So we have no ways to pass some dynamic information to the autocomplete url, for example, values from other form fields as GET arguments like /entity_reference_autocomplete/node/default/[token]?tag[0]=1&tag[1]=23&q=keyword.

Steps to reproduce

Try to implement a field_related_articles entity_reference field to the "Article" node bundle, with passing to it the tag ids values from another field field_related_articles_tags. You can't do this easily via hooks!

Proposed resolution

Add processing the additional $element['#autocomplete_route_options'] array item and pass it to the URL constructor in the function processAutocomplete().

Remaining tasks

User interface changes

API changes

Data model changes

Release notes snippet

✨ Feature request
Status

Closed: duplicate

Version

11.0 πŸ”₯

Component
FormΒ  β†’

Last updated about 5 hours ago

Created by

πŸ‡¦πŸ‡²Armenia Murz Yerevan, Armenia

Live updates comments and jobs are added and updated live.
  • Needs tests

    The change is currently missing an automated test that fails when run with the original code, and succeeds when the bug has been fixed.

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.

  • πŸ‡ΊπŸ‡ΈUnited States jumpsuitgreen

    @Murz,
    I became really hopeful when I came across this patch. I have the need to send the dynamic value from one form field as a parameter in the autocomplete route for a second field. Your patch proposes to allow this, but what syntax do I need to make it work? Can you provide an example of the '#autocomplete_route_options' structure as well as the my_module.routing.yml file?

  • Status changed to Needs review over 1 year ago
  • πŸ‡¦πŸ‡²Armenia Murz Yerevan, Armenia

    I've rebased my MR on 10.1.x branch and have added a change record with usage example: https://www.drupal.org/node/3341541 β†’
    Please review again.

  • Status changed to RTBC over 1 year ago
  • πŸ‡­πŸ‡ΊHungary szato

    Thank you for your work.
    I'm using the MR diff with core 9.4.11 and it works.

  • πŸ‡­πŸ‡ΊHungary szato

    Checked with Drupal core 9.5.3 too. Works.

  • Status changed to Needs work over 1 year ago
  • πŸ‡¬πŸ‡§United Kingdom alexpott πŸ‡ͺπŸ‡ΊπŸŒ
    1. +++ b/core/lib/Drupal/Core/Render/Element/FormElement.php
      @@ -174,6 +174,7 @@ public static function validatePattern(&$element, FormStateInterface $form_state
      +   *   - #autocomplete_route_options: The options to be used in the route.
      

      Nice to see docs added. It would be good if this pointed to the fact this is an array and some existing docs for what goes in there. Plus there is example code above that could be updated.

    2. +++ b/core/lib/Drupal/Core/Render/Element/FormElement.php
      @@ -188,7 +189,8 @@ public static function processAutocomplete(&$element, FormStateInterface $form_s
      -      $url = Url::fromRoute($element['#autocomplete_route_name'], $parameters)->toString(TRUE);
      +      $options = $element['#autocomplete_route_options'] ?? [];
      +      $url = Url::fromRoute($element['#autocomplete_route_name'], $parameters, $options)->toString(TRUE);
      

      There's no need to assign $options - we can pass $element['#autocomplete_route_options'] to Url::fromRoute().

    3. We need to add test coverage - see \Drupal\form_test\FormTestAutocompleteForm for where to add some stuff and \Drupal\Tests\system\Functional\Form\ElementTest
  • πŸ‡¬πŸ‡§United Kingdom alexpott πŸ‡ͺπŸ‡ΊπŸŒ

    I think tests where added before but it got lost in moving the MR to 10.1 - fwiw I think we don't need a whole new test here - see #16.3

  • πŸ‡΅πŸ‡±Poland dmitry.korhov Poland, Warsaw

    Rebased on latest 10.1.2 (10.1.x)

  • πŸ‡­πŸ‡ΊHungary szato

    Maybe it's a duplicate of an already fixed issue: Entity autocomplete widget does not pass along entity to AJAX request πŸ› Entity autocomplete widget does not pass along entity to AJAX request Fixed , and we can close this one.

  • Open in Jenkins β†’ Open on Drupal.org β†’
    Environment: PHP 8.1 & MariaDB 10.3.22
    last update 9 months ago
    29,643 pass
  • Status changed to Closed: duplicate 9 months ago
  • πŸ‡¦πŸ‡²Armenia Murz Yerevan, Armenia

    Yeah, this feature is implemented well via πŸ› Entity autocomplete widget does not pass along entity to AJAX request Fixed , so closing this issue.

Production build 0.69.0 2024