Great work on the version 2 re-write, plugin system works great.
Problem/Motivation
To provide more flexibility to the content Editor to control the output of the view it would be good to expose any filters added to the view back to the content editor when they are adding and editing the views reference field within the content type. In particular this would be useful when wanting to further refine the view by taxonomy term.
In our scenario we currently have multiple views displaying resource listings setup across the site that use different filters depending on resource type. It would be preferable to only have one view that can then be filtered by a resource type vocabulary depending on the context of its location. This would remove the dependancy of a developer having to create a fresh view and config for each listing purpose.
Contextual filters could be used, but its not particularly user friendly as you have to know the term id.
Proposed resolution
Add an additional plugin called filter that reads the view object when selecting a view and displays the relevant filters back the the content editor for selection.
Remaining tasks
None, patch to be uploaded.
I've had to make some wider changes to trait and plugins in order to accommodate this feature.
Comments
Comment #2
roborew commentedComment #3
roborew commentedHere's a patch to try..
Comment #4
roborew commentedComment #5
roborew commentedComment #6
roborew commentedScreenshot of working feature:

Comment #7
alaa abbad commentedFix patch to viewing exposed filters not all filters.
Comment #8
alaa abbad commentedComment #9
roborew commentedCheers @alaa abbad, looks good, but actually I did try this solution originally, but I didn't want to tie the filters to only the exposed filters.. sometimes you will only want the content creator todo the filtering and not expose the filter to the end user.
To tidy up this feature to optionally show content filters for a view probably need to extend the view filter plugin and add an additional configuration check box for allowing view filter for the content creator. Will have a look when I get time.
Comment #10
attiks commentedPatch only showing exposed filters based on #7
Comment #11
jonraedeke commentedThis would be a really nice feature to have. I tested the patch in #10 and my view is not getting filtered.
The id of the taxonomy term is getting captured in the data for the field in the DB, but the views query is not effected by the filters. Am I missing any configuration?
A few nice-to-haves:
FWIW, I think it makes the most sense to only show the exposed filters to the author. Exposed filters do not get rendered on the front end anyway.
Comment #12
luisnicg commentedThis feature is very useful, thanks for the patch but I got the same result as #11 the view is not getting filtered. I made some adjustments to make it work. I also updated the validation to only show the exposed filters.
Hope this helps, please update the issue if someone find another issue.
Comment #13
gambryHi all. This is indeed a nice-to-have functionality, as using the current
Argumentplugin is pretty impossible for standard content editors (not skilled with Drupal or Views conventions).However I believe if we change the approach a bit, current implementation and future improvements will be much easier. Specifically:
a) Use handlers' form element, instead of forcing this to be a dropdown.
b) (better!) We use arguments rather than filters, and we provide ViewReferenceSettings plugins for basic field types? For example this issue can provide the plugins for
entity_reference,list_stringandlist_integerwhich I'm sure will cover a good 75% of filtering cases?ViewsReferenceSettingInterface::alterFormField()can't access the current view and the form_state. I think if we pass both to the plugins we'll reduce complexity as delegating plugins to have their own logic.Thoughts?
Comment #14
gambryWorking on this. Using arguments is too complex, because Views doesn't handle them as fields/widgets but simply as tables relationship. Using exposed filters provides at least a UI to the (views) editor to select the form element and some more details.
I've got a solution which is really simple and hopefully covers most of scenarios. I'll clean up and post it in here soon.
Comment #15
gambryHere we go. On this patch I'm providing the editor with View's (display's) exposed filters. The form element are handled by each filter plugin, so it should work with pretty much any exposed filter.
I've also tested it with some weird libraries providing filters widgets, in order to cover most scenarios possible.
There is still space for improvement, but I'm technically changing 10-15 lines of code (half being due a change in the interface) + one new plugin, so should have really few if no disruption.
Please test and review.
UPDATE: there is no interdiff as I started pretty much from scratch.
Comment #16
scottsawyerI have the patch in #15 installed, but it doesn't seem to filter the view. It's a basic taxonomy term filter, works as expected in the Views preview, but when the field it rendered I see all results.
When I save the node with the Views Reference, I see an error in the log:
If more information from the log would be helpful in debugging, please let me know.
Comment #17
gambry@scottsawyer can you provide the view config yaml file?
I'm using this either with single or multiple filters, all of them being taxonomy term filters, and I didn't get your error.
Comment #18
scottsawyer@gambry, thank you for looking. I posted on pastebin: https://pastebin.com/2prGAUXS, I removed all of the displays except Master and the one that throws the error.
Comment #19
gambry@scottsawyer I don't see any problem, other than in the default display (and the extended one) the filter for the taxonomy field is not exposed.
Also the warning you posted is on assigning the value to the filter, saying the order of the filters in the exposed form in the content edit page is not the same order of the filters when the view is executed. Can you investigate a bit more?
I can't replicate it locally. And as I said locally I have 3 exposed filters, so likely to mess-up with ordering.
Comment #20
scottsawyer@gambry, I forgot that I removed the exposed filter when creating the new displays.



Here I expose the filter:
Then I set the filter on my node:
When I save, the filter is not applied ( I get all results, not just "Hats" ), and I get 2 PHP warnings in the log, both the same message:
In a different display with no exposed filter, it filters properly.
Comment #21
tobiberlinHey... this is a feature that we would really appreciate to have so I would like to support you in implementing it.
I installed the module and applied the patch and got the same errors as described above.
I solved it by changing alterView() to
See the
$view->setExposedInput($filters)which makes the magicComment #22
tobiberlinBut what I realized: when I first choose a view the options field is not filled with the exposed filters available. I need to save the entity with the views reference field first (after choosing a view), when I edit the entity after that again I see the exposed filters from the referenced view. Additionally the values I choose for the exposed filters are not saved - they disappear when I save the entity. This only appears to be an issue with the autosuggest field widget.
Comment #23
tobiberlinAnother issue with the patch: when I add an autosuggest term reference field with multiple values allowed ViewsReferenceExposedFilters->alterFormField() in line 73 $current_values[$info['value']] will be an array. This leads to the following error:
Additionally in the filter field there is just the word "Array" instead of the terms.
Comment #24
gambryOk, let's wrap-up current issues and make some progress:
I like this. Let's have this is if it fixes issues on #20
. Can you describe this more? Any error in the console or watchdog? Do you think this is only an AJAX handler issue, or a conflict between what we currently do and "autosuggest"?
Not sure what to do in here, we may have to re-think the way we are doing the form_field... but if this issue only manifests with a single form field type it's tricky to judge the refactoring workload with the values it may bring?
This issue needs work because at least 1. should be applied to current work, and 2. should get some investigation. Thanks @tobiberlin !
Comment #25
seanbGreat work being done here, thanks! Here is a quick first review on the overall approach.
I needed this for a project recently as well, makes a lot of sense!
Maybe it would be better to pass the
$field_valueinstead of a loaded view?Could we add this in a separate submodule to make it optional? I can see how this helps people, but personally I prefer to separate the exposed filters and the views reference settings. So it would be great to make this optional.
Comment #26
seanbAfter reviewing a different issue I realised this breaks the API. Also see my comment in #3081979-2: Add $element and $form_state to interface method alterFormField. I do think we should solve this but we have to make sure it doesn't break existing sites. We should probably fix this specific thing in the other issue and base this patch on top of that one.
Comment #27
seanbIgnore my previous comment. I just found #2957177: Pass view name and display id to alterFormField() method in ViewsReferenceSetting plugin which has a very creative solution to work around the BC issues.
Comment #28
gambryYep, it does break the API, but this way is consistent with the other method
alterView()where we pass the view object. With #2957177: Pass view name and display id to alterFormField() method in ViewsReferenceSetting plugin not only we will have each plugin handling the - same - view in the two methods in two different ways, but there is no way of documenting the view is actually loadable from the config, unless you dig down ViewsReferenceTrait code until line 163.Each plugin requiring the view in its
alterFormField()will be forced to load the view in their own ways, which is a waste of memory as the same view will be loaded in multiple objects.Yeah, solution in #15 breaks the API, but at:
The config solution is nice too, but only if there was a base method - or a trait - doing something like:
Which - again - won't be immediate to developers trying to use the plugin type by just reading the interface, but at least we reuse the same $view object, and there is a helper which avoid the far-west of everyone loading the view on own ways. Thoughts?
(I'll cross-post this last bit in there)
Comment #29
gambryOne option could be to improve #2957177: Pass view name and display id to alterFormField() method in ViewsReferenceSetting plugin with a trait, and doing the API change in a follow up for next major version.
Comment #30
tobiberlinResponse to comment #24
I did not go deeper into the Ajax problem with autosuggest field widget for choosing a view yet but hopefully will find some time for that later.
Regarding the problem that I described about multiple entity autocomplete fields with already two or more entity defined I found out that the EntityAutocomplete widget would do the work of turning the array of entities into a string with titles in EntityAutocomplete ->valueCallback(). But there are two problems with the current $form_field:
#process_default_value = true. It seems that$handler->buildExposedForm($form_field, $form_state);does not properly build the field widget array with all needed information for autocomplete handling into $form_field. Usually these values are set EntityAutocomplete->getInfo() but this method does not get called when you build the $form_fieldI wonder if there would be a more secure way to render the exposed filter form fields to assure that any kind of field widget may work as expected. Maybe we could simply render the exposed form directly into the options instead of building every filter widget on its own?! I will test some other filters and field widgets for exposed filters later - maybe I will find some other issues. If not it may be easier to simply work around the autocomplete issue.
Comment #31
abdelrahman amer commentedComment #32
abdelrahman amer commentedComment #33
abdelrahman amer commentedComment #34
abdelrahman amer commentedComment #35
kekkisFirst of all I have to say I like the idea here.
Secondly, I tend to agree with #30 - for safety - but also to note that perhaps by rendering the complete exposed form we could make use of modules altering it, like BEF.
However I haven't (yet anyway) had time to look into how we could approach this suggestion. That would require that I take a deeper look in the code not only in this patch but also the module in general and perhaps some other modules leveraging the exposed form (if there are any).
Comment #36
gambry@Abdelrahman_Amer can you describe what values your patches are bringing? Otherwise people won't follow, nor review.
Also is a good habit to add an interdiff file to better explain what has been changed. I'm hiding those files while waiting for a description, at least for the latest one.
Unless anything is provided, current patch is #15 with work needed as per comments on #24, #25, understanding and maybe including (waiting!) changes from #2957177: Pass view name and display id to alterFormField() method in ViewsReferenceSetting plugin and probably feedback from #30.
Comment #37
gambryThis patch only adds
$view->setExposedInput($filters)change as per #24.1 . Nothing more.Also #24.2 can be ignored until more info about the bug (see #30) and #24.3 change request are covered by #30.
So currently this issue Needs Work as per
About latest point RE feedback from #30 and specifically:
I did a bit of research and we could potentially replace ALL code in
ViewsReferenceExposedFilters::alterFormField()with simplyWe only need to clean up $form - i.e. from 'actions' item - , and restore the bit of logic we are currently doing re-enabling form items titles, but technically this should use Views' way of building exposed form opening to all sort of altering. I haven't tried, but should work.
Comment #38
gambryThis line of code causes an issue when the site-builder changes filters identifiers to anything other than the default one. I believe skipping this logic entirely and just using
$view->setExposedInput($values)with a cleanup similar to \Drupal\views_ui::renderPreview() on line 551 should work.Comment #39
psf_ commentedThe last patch, #37, works for me. But we require that the exposed filter remain visible, I think that we should add a "show filter" checkbox in each exposed filter in ViewsReferenceExposedFilters::alterFormField, but I don't know where storage that settings.
Comment #40
gambryI'm not sure what do you mean? To have the exposed filters showing up in the frontend as well, rather than only available to editors?
This issue is specifically to improve editor experience, if you want exposed filters I believe you can set them up in the view and by just exposing them they should show in the FE? So without passing from this plugin?
Comment #41
psf_ commentedI mean that the editor should decide if he set the settings to a default value in exposed filters or if he wants to fix the filter to the values that he selects (how works now with the patch). This way a final user sees the exposed filter if the editor allows it.
I think that this add a bit more flexibility.
Comment #42
psf_ commentedI created a patch with the functionality that I explain before.
I'm sorry, but I can't build an interdiff.txt, but I'll explain my changes.
When the editor is editing the views reference field, in the options area, get a "Show exposed filters" checkbox with FALSE by default:
This configuration will be saved with the configurations of the filter.
In #38:
I get this problem too. I see your referenced code, but I think that it's no applicable directly. I created an alias list configuration to get the right filter ID later:
Then, while we preload the filters I use that alias list to get the right ID:
The complete method code:
How you can see, I remove my custom configurations before all, to not modify your logic.
I hope this patch help.
Comment #43
marcoka commentedDo i understand this correctly? This exposed filters will only apear in nodes and not if you place a block(that uses viewsreference field) using the layout manager?
Comment #44
psf_ commentedWe are using this how a block in a panopoly layout :_ D
Works like any other field type. You can see this functionality if you enable "Exposed Filters - editor view" in the field edit form.
Comment #45
marcoka commentedAh yes, you are right. Thank you.
If i now edit the block in the block ui i can set the values.
If i edit the block using the Layout-Manager (pen symbol, then edit) the fields do NOT show up. Sholdnt they?
Btw if i "add a new custom" block through the layoutmanagers right sidebar(blocklist) then the fields will show up but only half of them.
I exposed the content type and published status here.
Create block from layout manager: http://www.root.artwaves.de/sharex/05-03-20--15-36-16.jpg
Create Block from block admin ui(/block/6?destination=/admin/structure/block/block-content): http://www.root.artwaves.de/sharex/05-03-20--15-37-09.jpg
Btw. This functionality is extremely useful! Especially if you like add a lot of blocks that like show last 5 pages, articles, gallerys etc using views. Otherwise you have to replicate a gazillion of blocks in views.
testing patch #43
Comment #46
psf_ commentedThe patch is to the option in this image:
Comment #47
marcoka commentedYes i understand, that is in the field options for "exposing" it as a form inside the block edit form. I can see that exposed filter then if i edit a block in the block ui. Do you see that option in the block if you add a block using the layout manager?
Comment #48
psf_ commentedlayout manager is layout builder? I used this only with panopoly, and I see the options when I add a new block to the layout: I select a view, then a display, and in the options fieldset I see the exposed filters.
Comment #49
marcoka commentedI know Panopoly, i like it. As Panopoly uses panels i tried that with a panel page. Here it shows the exposed filters! Meaning clicking the "pencil icon" will show that exposed filter.
Yes i meant the layout-builder. Here when you click the edit pencil there is none of the filers avaliable.
Layout-Builder looks like this: http://www.root.artwaves.de/sharex/05-03-20--18-36-59.jpg
Panels looks like this: http://www.root.artwaves.de/sharex/05-03-20--18-39-45.jpg
BUT in panels the selection(in my case the content type i select) will not be saved and i also get an error "An illegal choice has been detected. Please contact the site administrator."
The error log says
Comment #50
psf_ commentedI found that when the filters are visible, they don't allow change the selected option.
I updated ViewsReferenceExposedFilters::alterView to set the preference to user selection over editor defaults.
Comment #51
marcoka commentedOk i applied it and took a look at the code with a debugger. The conditions are never met. I triggered this by editing the block on the panel page using the pencil icon.
Debugger: http://www.root.artwaves.de/sharex/06-03-20--19-43-48.jpg
As you can see the $values is empty so the conition is never met. Used the same field as in the screenshots i used in this issue. Exposed the contenttype.
I found another issue. When i edit the fieldsettinsg inside the block like here: /admin/structure/block/block-content/manage/customblocka/fields/block_content.customblocka.field_view_ref
And select something like this: http://www.root.artwaves.de/sharex/06-03-20--19-51-09.jpg
and hit save. Nothing will be saved at all. Edit again, nothing saved.
Some more Info.
It seems $enabled_settings never contains the settings selected. In my case it always only has one value 'exposed_filters'. Doesnt matter if i select multiple exposed filters. Because of this values are never saved.
Debugger: http://www.root.artwaves.de/sharex/07-03-20--20-48-32.jpg
Also completely uninstalled the module and reinstalled. Settinsg not beeing saved. All above leads to the settings not beeing saved by the code. But i am not firm enough with the api here to patch this. I hope the analysis helps.
Comment #52
marcoka commentedConfirming this again. I selected all checkboxes avaliable.
Pagination and results, Items per page are saved. The custom exposed filters are not beeing saved and i can not find the code that is supposed to do that.
I dont get how that works with panopoly. Did yoz patch the 2.x version of viewsreference?
Also tested #15 same. Exposed filter setting not beeing saved.
I use Drupal 8.8.3 and newest panels 8.x-4.4
Comment #53
gambryYeah, I got what you mean. What I'm trying to say is this patch is already complex per-se, and it's specifically targeting Editors. No users.
By extending the functionality we increase the complexity and open to new problems, without actually fixing existing one.
I suggest we start back from #37 and we address the current issues (thanks for tackling already some).
@psf_ I believe you can achieve exactly the same by using this patch locally and creating your own plugin extending
ViewsReferenceExposedFilters?Again: these are just my thoughts. This is a nice-to-have functionality, but if we add complexity at this point we will never see it committed.
Let's get the core of this feature stable, committing and then we can talk about extending it.
Comment #54
marcoka commentedSame for patch #37
Add a block to a panels page and the content type(exposed views filter) is never saved at all.
http://www.root.artwaves.de/sharex/18-03-20--20-47-40.jpg
I can debug this if i get any hints. I have phpstorm and xdebug running but could not figure it out by now.
Comment #55
seanbI would have to agree with gambry in #53. The reason we created the plugin based approach in the first place was to prevent adding a lot of complexity and maintenance burden to the base module. Exposed filters were never added to the module because exposed filters are pretty complex compared to the other settings. Some of the things seen in this thread alone:
I'm slowly starting to think someone who is willing to maintain this should create a separate module for the plugin. In the viewsreference module we should make sure we add all the changes the plugin needs. Other complex features would also benefit from improving the API for the plugins. Is there anyone that would be willing to maintain the plugin?
Comment #56
roborew commentedHey, Took the liberty of creating module to tackle this issue.
Views Reference Field Filter
I've used the patch from #37 for the plugin and created a service to get the view.
@gambry I did attempt the approach you outlined, using:
I got it rendering correctly and tidied up, but could not get the values to submit correctly due to the render changes that the exposed_form introduces. But we can tackle this amend and other feature requests in isolation now on the other issue queue.
Only thing is this module does have a requirement on #2957177: Pass view name and display id to alterFormField() method in ViewsReferenceSetting plugin currently, will bump this to see if we can get it merged.
Comment #57
roborew commentedClosing this issue now as this feature is forked to separate module.