2

I have a content type that has an entity reference field allowing you to link to other pages and add a custom image, for nice big CTAs.

However, it's come to light that some of the links may need to be to Views that show content as a list or map.

The ER field has autocomplete enabled and it can't find the view by its URL e.g /my-view

Is there a plugin that would allow the field to find Views? Or do I have to change the content type and add a Link field instead?

2
  • As it's an entity reference field, and views pages aren't entities (views are, but its the page display which you want to link to, not the underlying view), you cannot use this field for this purpose. A link field will be a better choice in this case. There's a plugin you can use to include views pages in the autocomplete, just hunting for it, brb. Jan 29, 2018 at 14:13
  • Turns out I was thinking of linkit_views, which is not relevant to this case, sorry. Jan 29, 2018 at 14:26

3 Answers 3

0

Views are config entities so you can target them to be referenced (see screenshots) and check this post.

Add new field Selecting ER field target type

The caveat is you can only point to a given entity type when using ER fields so you cannot have an ER field targeting content and config alike.

On the other hand almost any market solution referencing views aim to render it not just displaying a link pointing the view.

As you suggest using a link field can work around with your use case but the link form element only autocompletes content entities, you cannot type your views' display title and wait for it to get the proper URL for you. For that I'm afraid you need to provide your own widget/form element (and controller). Check this and this about how to build your own autocomplete widget/form element for a text field and this post from Drupal.org docs on how to build your own field formatter.

Also worth noticing that Views URL's are used on limited display types (Page, Feeds, possibly 2 more but I'm not quite sure) and is considered a route, not a path or link like when building a menu link item.

Hope this points you in the right direction.

2
  • There is a Catch 22 with this situation too: When editing your page and searching for your View, all enabled Views will be exposed. You would normally change the Field to use 'Entity Reference View' to only expose the subset you want, but ER Views can't expose Views themselves as they are config, not content.
    – Timmah
    Jan 4, 2023 at 21:49
  • Yes, is an disadvantage but can be "solved" either extending ER field widget to exclude certain views or just include the ones required for the feature, or by implementing hook_form_alter or hook_field_widget_single_element_WIDGET_TYPE_form_alter if you want a nip tuck solution.
    – d70rr3s
    Jan 9, 2023 at 8:47
0

As others have mentioned, the Views Reference Field module does this nicely (not to be confused with View Reference).

It defined a new field type for you to reference in Entity Reference Fields, allowing Editors to link to pre-build Views. It also helpfully allows defining exactly which views and display modes you want to expose via the UI:

Views Reference configuration example

And here is how it can look in the Node form: Views Reference Field in action

-1

One way to handle this is to make the view page into a block, then create a node with no content that contains this block (block placement in main content with limited view to /node/. Then use this in the entity reference field.

An alternative might be to have a content type that uses one of the views fields like view reference. Then just create a view node with the desired view and use it.

1
  • The views_reference module allows to reference a view to render it, is not what the OP wants to achieve since the problem to solve is to generate a link element pointing to the selected view display. Thus the later can be built on top of referenced module it may not be what the OP is looking for.
    – d70rr3s
    Dec 11, 2019 at 16:45

Your Answer

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

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