EntityAutocomplete should pass the original URI to the selection handler

Created on 1 August 2016, over 8 years ago
Updated 9 August 2024, 5 months ago

Problem/Motivation

If en entity reference field is using a ViewsSelection handler, the underlying view is not initialised with the request of the page where the field is exposed. This is because the view is built in a subsequent request of the autocomplete widget. This is bad because that view cannot use default arguments from the original request (like path arguments or query parameters). We can imagine a lot of use-cases where the autocomplete list needs to be narrowed by a parameter from the URL.

There's a "test only" patch showing this bug.

Proposed resolution

Ideally we should store the entire original Symfony request when exposing the ER field and pass it later to the view. But this would lead to a very big variance that would had flood the storage, so I went with the next proposal:

  1. In EntityAutocomplete: save the request URI in expirable key/value store, keyed by a hash built from URI.
  2. Pass the hashed key along the URL of autocomplete request to EntityAutocompleteController::handleAutocomplete()
  3. In EntityAutocompleteController::handleAutocomplete() retrieve the original URI from the key/value expirable store.
  4. Pass the original URI to the matcher (EntityAutocompleteMatcher::getMatches()).
  5. The selection handler plugin will be instantiated also with the original URI.
  6. In ViewsSelection::initializeView() (right now only this handler has a legitimate interest on using this value) create a mock of the original request based only on the URI and pass is as request context to the view.

Remaining tasks

None.

User interface changes

None.

API changes

  • The route system.entity_autocomplete takes an additional, optional parameter {original_uri_key}
  • EntityAutocompleteController::handleAutocomplete accepts an additional, optional argument $original_uri_key
  • EntityAutocompleteMatcher::getMatches accepts an additional, optional argument $original_uri

Data model changes

SelectionInterface plugins receive also the original URI as original_uri config.

🐛 Bug report
Status

Needs work

Version

11.0 🔥

Component
Field 

Last updated 1 day ago

Created by

🇷🇴Romania claudiu.cristea Arad 🇷🇴

Live updates comments and jobs are added and updated live.
  • VDC

    Related to the Views in Drupal Core initiative.

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.

Production build 0.71.5 2024