Entity autocomplete widget does not pass along entity to AJAX request

Created on 12 November 2016, about 8 years ago
Updated 16 August 2023, over 1 year ago

Widgets for entity reference fields are responsible for passing along the entity the widget is rendered on (aka host entity) so entity reference selection plugins know about the entity making the request.

The options buttons (checkbox/radio) and options select pass along the entity. These widgets create selection settings via:

  1. \Drupal\Core\Field\Plugin\Field\FieldWidget\OptionsWidgetBase::getOptions
  2. \Drupal\Core\TypedData\OptionsProviderInterface::getSettableOptions
  3. \Drupal\Core\Entity\EntityReferenceSelection\SelectionPluginManager::getSelectionHandler

The entity autocomplete is not passing along the entity. Its selection settings are created in \Drupal\Core\Field\Plugin\Field\FieldWidget\EntityReferenceAutocompleteWidget::formElement

Steps to reproduce:

Select widget

  1. Set up an entity reference field and reference it to content.
  2. Use the select list widget for the entity reference field.
  3. Edit an entity with the entity reference field.
  4. When the form is being rendered, inspect \Drupal\Core\Entity\Plugin\EntityReferenceSelection\DefaultSelection::getReferenceableEntities, notice $this->configuration['entity'] contains the entity the field is rendered on.

Autocomplete widget

  1. Set up an entity reference field and reference it to content.
  2. Use the autocomplete widget for the entity reference field.
  3. Edit an entity with the entity reference field.
  4. When the form is being rendered, inspect \Drupal\Core\Entity\Plugin\EntityReferenceSelection\DefaultSelection::getReferenceableEntities, notice $this->configuration['entity'] does not exist.

Proposed solution

- In the EntityReferenceAutocompleteWidget FieldWidget, pass the entity to the $selection_settings.
- Add a new #autocomplete_query_parameters property on the Element EntityAutocomplete if the $selection_settings['entity'] is an instance of EntityInterface. This property can handle the entity_type_id and the entity_id of the current entity being edited. We unset then the $selection_settings['entity'] set before to not have a different $selection_settings_key for each entity
- In the FormElement Element, for element with the property #autocomplete_route_name, check if the element has also the #autocomplete_query_parameters property, and if so pass theses parameters as query parameters to the Url built.
- In the EntityAutocompleteController of the system module, we can check now for query parameter 'entity_type_id' and 'entity_id' present in the URL and so load the entity and re-pass it to the $selection_settings
- And now we have the current entity in any EntityReferenceSelection plugin with an autocomplete widget

πŸ› Bug report
Status

Fixed

Version

10.1 ✨

Component
EntityΒ  β†’

Last updated 1 day ago

Created by

Live updates comments and jobs are added and updated live.
  • Contributed project blocker

    It denotes an issue that prevents porting of a contributed project to the stable version of Drupal due to missing APIs, regressions, and so on.

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