3

How can I enable autocompletion for media entities in Link fields? The Link field seems to "know" only nodes, but no media entities.

Use case:

I've got a paragraph for a "card style" link consisting of 4 fields for an image, a title, a short description and a link. The paragraph must work for internal and external links, so I'm using a Link field and not an entity reference.

A common task is that editors upload a PDF to the media library, and provide a nice download link to the PDF on multiple nodes, using the paragraph mentioned above.

Tried this, doesn't work good enough:

I know that it technically would be possible to manually enter a path like /media/<media_entity_id>. But this so bad UX that I need to find a better solution.

I also have LinkIt installed, but sadly I can't use LinkIt for Link/URL fields.

(I'm using Drupal v8.4 core media)

2
  • PS: I've found github.com/minnur/Alter-Entity-Autocomplete - could this be changed to include media entities instead of altering existing suggestions?
    – Hudri
    Nov 14, 2017 at 17:29
  • Question is outdated, the Linkit module now also supports Link fields. Use the Linkit module.
    – Hudri
    Sep 19 at 6:27

1 Answer 1

7
+50

The short answer: Currently not supported, unfortunately:

  // @todo Support entity types other than 'node'. Will be fixed in
  //    https://www.drupal.org/node/2423093.

Until https://www.drupal.org/node/2423093 is done, the link field widget can only support a single entity type; so you can have a widget for linking to nodes or to media entities, but not both.

Worse, until https://www.drupal.org/node/2427803 is done, the entity type is hardcoded as node and not configurable.

The good news: It's still possible, if you're prepared to write a bit of code.

Other than three lines in LinkWidget that contain a hardcoded reference to node, nothing stops the code from working with a different entity type in principle. So as a workaround, you could duplicate the core LinkWidget in a custom module, change the relevant lines to media (I made a patch for this at https://gist.github.com/cburschka/b214a52bdb1fdf7349ef3b0afe56b950), and thus create an alternate widget for media links.

This still means you have to choose between either a Node or Media link field, unfortunately. If you don't want to create two different fields, you could use the Paragraphs module in order to create a single Paragraph field that can be filled with one of two paragraph types (one of which exposes the link field with the original widget, and the other with your custom Media widget). It's a bit more complex, but should still provide a decent user experience.

Edit: Alternatively, instead of using the Link module you could use a Dynamic Entity Reference field: https://www.drupal.org/project/dynamic_entity_reference

The downside is that this field could then only reference entities; external links and non-entity links wouldn't be possible anymore.

1
  • The short response: that really su**s, but thank you anyways :-) From reading the linked issues I don't have any hopes at all for a viable solution going the core way, I think I'll try my luck with this LinkIt patch
    – Hudri
    Nov 20, 2017 at 11:01

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.