Allow multiple target entity types in the 'entity_autocomplete' Form API element

Created on 9 February 2015, about 10 years ago
Updated 6 February 2023, over 2 years ago

Problem/Motivation

#1959806: Provide a generic 'entity_autocomplete' Form API element → introduced a generic 'entity_autocomplete' Form API element. But it hardcodes a single entity type. While linking, users should (optionally) be able to select which entity type they want to link to.

Proposed resolution

Introduce the equivalent of DER (Dynamic Entity Reference), sans the storage/formatter/widget, only the Form API element.

Remaining tasks

TBD

User interface changes

TBD

API changes

TBD

📌 Task
Status

Needs work

Version

10.1 ✨

Component
Entity  →

Last updated about 4 hours ago

  • Maintained by
  • 🇬🇧United Kingdom @catch
  • 🇨🇭Switzerland @berdir
  • 🇩🇪Germany @hchonov
Created by

🇧🇪Belgium wim leers Ghent 🇧🇪🇪🇺

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

    Makes Drupal easier to use. Preferred over UX, D7UX, etc.

Sign in to follow issues

Merge Requests

Comments & Activities

Not all content is available!

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

  • 🇫🇷France louis-cuny

    #55 works for me if I disable reference validation :

        $form['contact'] = [
          '#type' => 'entity_autocomplete',
          '#tags' => TRUE,
          '#target_type' => 'node,user',
          '#validate_reference' => FALSE,
          ....
    ];

    Patches that are suggesting using dynamic_entity_reference does not feel right

    I put it pack to needs work because of the broken reference validation

  • 🇧🇪Belgium brentg Ghent

    I encountered some issues with #55 where when saving a taxonomy term link, it was giving an unexpected error, because $node was null, so added a small extra patch that fixes this issue.

  • 🇧🇪Belgium brentg Ghent

    Had some issues when using a link field inside layout builder on a translated page.
    Issue was that the langcode was not passed along, so added a fallback for the langcode on layout builder pages

    However, I have the feeling this patch is atm not the way to go as the str_pos is way too risky.
    It's probably better to extend on one of the existing modules (e.g. linkit or link_field_autocomplete_filter) so that they support linking towards multiple entity types at the same time.

  • 🇨🇭Switzerland Siegrist

    Re-role for 10.2.3

  • First commit to issue fork.
  • 🇮🇳India sakthi_dev

    Created an MR against 11.x.

  • Pipeline finished with Failed
    about 1 year ago
    Total: 161s
    #91317
  • 🇩🇪Germany Anybody Porta Westfalica

    Looks like the suggestion from #47 by @leymannx have not been incorporated into the MR yet?

    Furthermore, this will need tests.

  • 🇨🇭Switzerland tcrawford

    I am seeing a type error in SelectionPluginManager as getSelectionGroups does not return an entry for the concatenated entity entity type ID of 'node,taxonomy_term'.

    The website encountered an unexpected error. Try again later.
    
    TypeError: uasort(): Argument #1 ($array) must be of type array, null given in uasort() (line 65 of core/lib/Drupal/Core/Entity/EntityReferenceSelection/SelectionPluginManager.php).
    
    Drupal\Core\Entity\EntityReferenceSelection\SelectionPluginManager->getPluginId('node,taxonomy_term', 'default') (Line: 41)
    Drupal\Core\Entity\EntityReferenceSelection\SelectionPluginManager->getInstance(Array) (Line: 41)
    Drupal\Core\Entity\EntityAutocompleteMatcher->getMatches('node,taxonomy_term', 'default', Array, 're') (Line: 118)
    Drupal\system\Controller\EntityAutocompleteController->handleAutocomplete(Object, 'node,taxonomy_term', 'default', '3kwQYPisvvlkGtu1zMaBQotUMioVXxI_qWM7KjOEK2w') (Line: 25)
    Drupal\realname\Controller\RealnameAutocompleteController->handleAutocomplete(Object, 'node,taxonomy_term', 'default', '3kwQYPisvvlkGtu1zMaBQotUMioVXxI_qWM7KjOEK2w')
    call_user_func_array(Array, Array) (Line: 123)
    Drupal\Core\EventSubscriber\EarlyRenderingControllerWrapperSubscriber->Drupal\Core\EventSubscriber\{closure}() (Line: 627)
    Drupal\Core\Render\Renderer->executeInRenderContext(Object, Object) (Line: 124)
    Drupal\Core\EventSubscriber\EarlyRenderingControllerWrapperSubscriber->wrapControllerExecutionInRenderContext(Array, Array) (Line: 97)
    Drupal\Core\EventSubscriber\EarlyRenderingControllerWrapperSubscriber->Drupal\Core\EventSubscriber\{closure}() (Line: 181)
    Symfony\Component\HttpKernel\HttpKernel->handleRaw(Object, 1) (Line: 76)
    Symfony\Component\HttpKernel\HttpKernel->handle(Object, 1, 1) (Line: 58)
    Drupal\Core\StackMiddleware\Session->handle(Object, 1, 1) (Line: 48)
    Drupal\Core\StackMiddleware\KernelPreHandle->handle(Object, 1, 1) (Line: 28)
    Drupal\Core\StackMiddleware\ContentLength->handle(Object, 1, 1) (Line: 32)
    Drupal\big_pipe\StackMiddleware\ContentLength->handle(Object, 1, 1) (Line: 106)
    Drupal\page_cache\StackMiddleware\PageCache->pass(Object, 1, 1) (Line: 85)
    Drupal\page_cache\StackMiddleware\PageCache->handle(Object, 1, 1) (Line: 53)
    Asm89\Stack\Cors->handle(Object, 1, 1) (Line: 48)
    Drupal\Core\StackMiddleware\ReverseProxyMiddleware->handle(Object, 1, 1) (Line: 51)
    Drupal\Core\StackMiddleware\NegotiationMiddleware->handle(Object, 1, 1) (Line: 36)
    Drupal\Core\StackMiddleware\AjaxPageState->handle(Object, 1, 1) (Line: 51)
    Drupal\Core\StackMiddleware\StackedHttpKernel->handle(Object, 1, 1) (Line: 704)
    Drupal\Core\DrupalKernel->handle(Object) (Line: 19)
    
    

    This is on Drupal 10.2.5 (PHP 8.1) with patch #60 applied.

  • 🇨🇭Switzerland tcrawford

    The above issue with the TypeError is coming as the second diff in patch #60 did not apply properly on one of our projects (and weirdly failed silently both locally and in the pipeline). Applying the second diff manually works. There appears to be an issue with patch #60 in that case and it would be good if someone else can verify. I have never seen a patch partially apply and fail silently and therefore suspect there is an issue on our side.

    diff --git a/core/modules/system/src/Controller/EntityAutocompleteController.php b/core/modules/system/src/Controller/EntityAutocompleteController.php
    index a49da37340..fd597a3947 100644
    --- a/core/modules/system/src/Controller/EntityAutocompleteController.php
    +++ b/core/modules/system/src/Controller/EntityAutocompleteController.php
    @@ -77,6 +77,7 @@ public static function create(ContainerInterface $container) {
        */
       public function handleAutocomplete(Request $request, $target_type, $selection_handler, $selection_settings_key) {
         $matches = [];
    +    $target_types = explode(',', $target_type);
    
         // Get the typed string from the URL, if it exists.
         if ($input = $request->query->get('q')) {
           $tag_list = Tags::explode($input);
    @@ -109,8 +110,12 @@ public function handleAutocomplete(Request $request, $target_type, $selection_ha
               }
             }
           }
    -
    -      $matches = $this->matcher->getMatches($target_type, $selection_handler, $selection_settings, $typed_string);
    +      foreach ($target_types as $t_type) {
    +        $target_matches = $this->matcher->getMatches($t_type, $selection_handler, $selection_settings, $typed_string);
    +        if ($target_matches) {
    +          $matches = array_merge($matches, $target_matches);
    +        }
    +      }
         }
    
         return new JsonResponse($matches);
    
  • 🇨🇭Switzerland tcrawford

    I have rerolled #60 as #66 (against 10.2.x) and this applies for me properly. I hope that helps if someone else was having an issue with #60. I am sorry that I have not yet been able to otherwise move this issue forward.
    →

  • 🇫🇷France alxgl

    Hello and thanks for the work on this issue !

    The latest patch #67 📌 Allow multiple target entity types in the 'entity_autocomplete' Form API element Needs work works well for me on a Drupal 10.4.6. The autocomplete is working for taxonomy terms and displays the proper label for the user once selected.

    However, if I come back to the edition form, the field displays the raw storage value entity:taxonomy_term/tid instead of the clean entity label.

    It's still functional and editable, but not very user friendly.

    If someone have an idea to fix it, it will be a good improvement.

  • First commit to issue fork.
  • 🇮🇹Italy robertom

    @alxgl I attach the patch that also shows the entity label when you return to edit the form

  • Pipeline finished with Failed
    2 days ago
    Total: 691s
    #490818
  • 🇨🇦Canada phjou Vancouver 🇨🇦 🇪🇺

    The patch doesn't seem to handle media. Only taxonomy terms and nodes.

  • 🇫🇷France alxgl

    @robertom Thanks a lot for improving this patch.

    It's now working perfectly for my needs and my request, which was taxonomy only. I didn't try another bundle indeed.

Production build 0.71.5 2024