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

Comments

dawehner’s picture

As first step we could just make the configuration on the widget settings level.

Dave Reid’s picture

I would also like to select a bundle as well, so I could filter down to only Article nodes, instead of all nodes. Often I've encountered projects that have nodes with the same titles, so this would help with those instances.

webchick’s picture

Agreed that filtering by bundle types is actually probably more important for a lot of use cases than different entity types.

jibran’s picture

jibran’s picture

How about adding a new DER form element to the core which will use ER form element and it'll show a select list with entity type ids? I am doing something similar in above issue with DER widget. We can use dynamic_entity_autocomplete form element instead of entity_autocomplete in link widget.
For #2 we can implement hierarchical select type widget for DER form element in a follow up issue to select bundles.

jibran’s picture

There is a @todo in EntityReferenceAutocompleteWidget::getAutocreateBundle()

        // @todo Expose a proper UI for choosing the bundle for autocreated
        // entities in https://www.drupal.org/node/2412569.

which is kind of similar to #2 but it is only for taxonomies #2412569: Allow setting the auto-create bundle on entity reference fields with multiple target bundles.

amateescu’s picture

Title: Allow an entity type to be selected in the generic 'entity_autocomplete' Form API element » Allow multiple target entity types in the 'entity_autocomplete' Form API element

Opened #2427803: Expose target type, selection handler and selection settings in LinkWidget for #1. I think that was the original intention for this issue but we need two of them anyway, so this can stay open and focused on the 'entity_autocomplete' form element.

Wim Leers’s picture

Version: 8.0.x-dev » 8.1.x-dev

Version: 8.1.x-dev » 8.2.x-dev

Drupal 8.1.0-beta1 was released on March 2, 2016, which means new developments and disruptive changes should now be targeted against the 8.2.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

jibran’s picture

amateescu’s picture

Why not in this issue and by adding support to the current form element?

Edit: oh, I see, that issue is in the DER project, not in core :)

Version: 8.2.x-dev » 8.3.x-dev

Drupal 8.2.0-beta1 was released on August 3, 2016, which means new developments and disruptive changes should now be targeted against the 8.3.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.3.x-dev » 8.4.x-dev

Drupal 8.3.0-alpha1 will be released the week of January 30, 2017, which means new developments and disruptive changes should now be targeted against the 8.4.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

spheresh’s picture

This still does not work as intended:

Steps to reproduce:
1. Create/update link with a "entity:group/{group_id}" value.
2. If you go after this on the edit form again. You can see that the value of the input field is now "Title of your group (277)"
3. After that I re-save the link. And I go to the editing form again.
4. The value of the field is again changed to "entity:node/{group_id}"

Since the entity type was changed from group to node The link for the end user was broken

This small patch will fix this problem while we are waiting for the solution to the issue with the whole

Berdir’s picture

@spheresh: There's an RTBC (as of today) issue that fixes that problem: #2804391: Resaving menu links that points to a non-node entity changes the type to node and breaks the link

Sam152’s picture

I would have expected adding features to entity_autocomplete would be one issue and updating the menu system to use those features to be another. While they are one issue, here is an approach to making the menu UI reference multiple entity types. It's blocked behind proper multi-entity autocomplete support, but in the meantime I'm shimming that support in with a sandbox: https://www.drupal.org/sandbox/sam/2881047.

Applying this patch and installing the sandbox gives you this feature, albeit not in a form that is ready for core.

Sam152’s picture

Needed the same patch for 8.3.x.

amateescu’s picture

@Sam152,

I would have expected adding features to entity_autocomplete would be one issue and updating the menu system to use those features to be another.

I don't know why @spheresh posted that patch here, but we do actually have separate issues. This one is for enhancing the core 'entity_autocomplete' element with multiple target type support and then there is #2427803: Expose target type, selection handler and selection settings in LinkWidget for improving the link field (used by the menu system).

Sam152’s picture

Exactly what I was looking for! Thanks @amateescu.

Sam152’s picture

Rerolled for latest 8.3.x.