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)


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.

| improve this answer | |
  • 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 '17 at 11:01

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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