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
| Comment | File | Size | Author |
|---|---|---|---|
| #76 | 2423093-allow-multiple-target-entity-types-76.patch | 6.44 KB | s3b0un3t |
| #74 | 2423093-74.patch | 6.23 KB | omar_emailat |
| #70 | 2423093-70-10.4.x.patch | 5.34 KB | robertom |
| #70 | 2423093-70.patch | 5.33 KB | robertom |
| #67 | 2423093-66-reroll-60-to-avoid-failure-applying.patch | 5.19 KB | tcrawford |
Issue fork drupal-2423093
Show commands
Start within a Git clone of the project using the version control instructions.
Or, if you do not have SSH keys set up on git.drupalcode.org:
Comments
Comment #1
dawehnerAs first step we could just make the configuration on the widget settings level.
Comment #2
dave reidI 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.
Comment #3
webchickAgreed that filtering by bundle types is actually probably more important for a lot of use cases than different entity types.
Comment #4
jibranThe related issue in DER iq #2411981: Fixes after generic 'entity_autocomplete' Form API element.
Comment #5
jibranHow 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.
Comment #6
jibranThere is a @todo in EntityReferenceAutocompleteWidget::getAutocreateBundle()
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.
Comment #7
amateescu commentedOpened #2427803: [PP-1] 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.
Comment #8
wim leersComment #10
jibranI'm going to try that in #2766213: Add DynamicEntityAutocomplete form element using DER.
Comment #11
amateescu commentedWhy 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 :)
Comment #14
spheresh commentedThis 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
Comment #15
berdir@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
Comment #16
sam152 commentedI 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.
Comment #17
sam152 commentedNeeded the same patch for 8.3.x.
Comment #18
amateescu commented@Sam152,
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: [PP-1] Expose target type, selection handler and selection settings in LinkWidget for improving the link field (used by the menu system).
Comment #19
sam152 commentedExactly what I was looking for! Thanks @amateescu.
Comment #20
sam152 commentedRerolled for latest 8.3.x.
Comment #22
aaronbaumanThis patch now proposes adding dynamic_entity_reference to core.
Am i reading this right?
Comment #24
ryan.ryan commentedRerolling the patch from 20 against latest 3.x.
Comment #25
ryan.ryan commentedThe same patch with cleaned up whitespace issues.
Comment #26
idflood commentedReroll of patch in #25 against 8.6.x.
Note that it may be useful for this issue https://www.drupal.org/project/linkit/issues/2712951 (use link field to reference files or other entities)
Comment #28
borisson_Setting to needs review to have the testbot take a look at the patches.
Comment #32
yogeshmpawarComment #33
yogeshmpawarRe-rolled the #26 patch against 8.6x branch.
Comment #36
k_a_l commentedWhile I'm interested in seeing this feature in core, I decided to just extend the core link field in order to add a bundle filter for node entities for the time being. If anyone is interested, you can check out the project here: https://www.drupal.org/project/filterable_link
Comment #38
lpeabody commentedRe-roll of 16 for latest 8.6.x.
Comment #39
lpeabody commentedRe-roll without the erroneous Drupal.php version constant change.
Comment #43
nadavoid commentedUntil this core issue is resolved, there is an alternative method available, using the linkit module. The patch from #2712951-191: Linkit for Link field enables using linkit on link fields, and linkit can be used to autocomplete any combination of entity types. I'm using it to list node and media entities in a single list.
Comment #44
techlead commented@nadavoid
Thanks for bringing this to our attention. This works for me.
Comment #47
norman.lolLinkit's #2712951: Linkit for Link field is truly a helper. By using it I found out that there's a least a pattern to be entered in the link field that works for spitting out the correct link to other entities in the end. Like querying a certain webform is
entity:webform/my_form_id. Querying a certain terms isentity:taxonomy_term/TID(although this one doesn't link to the alias).Comment #48
ravi.shankar commentedAdded reroll of patch #39 on Drupal 9.4.x.
Comment #51
kdborg@gmail.com commentedPatch #48 isn't working on Drupal 9.4.7. The URL of the Link field disappears.
The error in the logs:
Comment #52
dpiThe patch doesnt work as-is. See #16
Comment #53
rastepanyan commentedI have used a code from custom patch made by Valery Lourie (valthebald) and re-roll it to make it compatible with Drupal 9.3.x core. It seems that it fixed the issue reported in #51 - the missing CTA-link-uri field.
Comment #54
_utsavsharma commentedComment #55
_utsavsharma commentedFixed CCF for #53.
Comment #56
louis-cuny commented#55 doesn't work for me, if I disable reference validation neither :
Patches that are suggesting using dynamic_entity_reference does not feel right
I put it back to needs work because of the broken reference validation
EDIT
I could avoid the broken validation by using my own :
But it's mismatching entity types
My field (custom entity Base field), doesn't store the entity type. I guess this issue cannot be fixed so easily
Comment #57
brentgI 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.
Comment #59
brentgHad 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.
Comment #60
siegristRe-role for 10.2.3
Comment #63
sakthi_dev commentedCreated an MR against 11.x.
Comment #64
anybodyLooks like the suggestion from #47 by @leymannx have not been incorporated into the MR yet?
Furthermore, this will need tests.
Comment #65
tcrawford commentedI 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'.
This is on Drupal 10.2.5 (PHP 8.1) with patch #60 applied. This error occurs when editing the link on a menu item.
Comment #66
tcrawford commentedThe 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.
Comment #67
tcrawford commentedI 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.
Comment #68
alxgl commentedHello and thanks for the work on this issue !
The latest patch #67 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/tidinstead 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.
Comment #70
robertom commented@alxgl I attach the patch that also shows the entity label when you return to edit the form
Comment #71
phjouThe patch doesn't seem to handle media. Only taxonomy terms and nodes.
Comment #72
alxgl commented@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.
Comment #73
aherczeg commentedHello, the patch from #70 works for our use case with taxonomy terms on Drupal 10.4.7
Comment #74
omar_emailat commentedI've rerolled the patch to work with Drupal 11.3.2.
Comment #76
s3b0un3t@omar_emailat I tested your patch, Composer applies it, but when you look at the changes, not all of them are applied to Drupal 11.3.2.
I'm proposing a new patch that works for me on Drupal 11.3.2 and also enables media autocompletion.