1

Using Drupal 8 I want to create a field that references certain Views that the user should be able to choose from when editing a node. Creating this reference is possible using the reference field type and choosing "View" as the type to reference.

The problem is I also want to filter which views should be selectable. This does not seem to be possible.

When referencing for example nodes, there's an option to select which bundles should be referenceable. Is there any way to do something similar for views, or for config entities in general? Or is there any contrib module available for this? Maybe there'a a hook / plugin I could implement to filter the referenceable views programmatically?

2
  • If you could create a view that lists the views you want, that could drive the entity reference widget. However, such a view can't be created from the UI as far as I can see, and I don't know if it can be created from code.
    – Jonathan
    Dec 22, 2015 at 12:40
  • @Jonathan I agree, it would be nice if it was possible to reference config entities (not just views) in a view. Maybe it is, just not through the UI? If not I think it would be a quite powerful addition to the views functionality.
    – reekris
    Dec 22, 2015 at 13:14

1 Answer 1

2

Yes, the options you can select from there are plugins.

As you found out, you can't use views with config entities, but you can implement one yourself.

There are two ways to do that, one is to define your own plugin that you can select there, like ViewsSelection does it. Or, it is possible to provide a version of the default plugin (DefaultSelection) that is automatically used for a specific entity type, see for example TermSelection.

I would however only use the second pattern if it is your own entity type, and otherwise define a separate plugin (you can still extend from DefaultSelection and re-use the code there).

4
  • Thanks for leading me in the right direction! When you say I should only use the second method if it is my own entity type, do you mean that the entity type that has the reference field should be my own or the entity I'm referencing? Since I want to reference regular views that would mean I should go with your suggested option 1? Looking a bit into the two classes you mentioned, I'm leaning towards implementing my own Plugin since DefaultSelection seems to contain a lot of code regarding bundles, which Views don't have. Does that sound right?
    – reekris
    Dec 22, 2015 at 14:10
  • The code regarding bundles is optional, it won't do anything if they don't exist. It does work, otherwise you won't be able to select and see the the views by default. Yes, the type you're referencing, which type you are referencing from is not relevant.
    – Berdir
    Dec 22, 2015 at 14:11
  • Oh, of course you're right, it contains checks if the entity type has a bundle. Then I'll have a go at creating a DefaultSelection subclass and get back here with progress. Thanks for the help!
    – reekris
    Dec 22, 2015 at 14:18
  • Here is a gist of a working solution: gist.github.com/reekris/73d1696694bb3e869efb The class uses view tags to filter which views that should be selectable. Not sure if that's a bad idea, but it feels right when there's many views to choose from. Thanks again @Berdir
    – reekris
    Dec 22, 2015 at 15:15

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.