The attached patch refactors the handling of exposed filters so that invokers of views_build_view can pass in exposed filter values, instead of relying on views to fetch them from the $_GET global. This patch bubbles down the changes to query building level as well, so that views_query.inc is no longer coupled to the exposed filter form implementation.
If no exposed filter values are passed to views_build_view, they are fetched from the $_GET global just as usual.
Some notes:
1. The $filters array passed into views_build_view is of the form
array(
0 => array('op' => 'foo', 'filter' => 'bar),
1 => ... etc
);
This reflects the old style of views exposed filter handling. Is this still necessary? Instead of stripping out all the names, etc, can we just return to normal Drupal form handling? I think it might be a little cleaner. Also, drupal no longer prepends 'edit' on everything, so it will a lot like the current implementation. (This will also help viewfield with exposed filter handling).
2. I've tried to be backward compatible. I doubt many other modules are calling _views_build_filters directly, but if they do, they're going to get a rude surprise. I'm guessing these changes are pretty safe.
Comments
Comment #1
moshe weitzman CreditAttribution: moshe weitzman commentedsubscribe ... a quick read through didn't sound any alarms. if it works, i think it is commit worthy. will try to test it, but am busy busy.
Comment #2
yched CreditAttribution: yched commented+1 on the feature - but this is not (yet) an actual review of the patch either
Comment #3
mfredrickson CreditAttribution: mfredrickson commentedThanks for the +1s.
Comment #4
mfredrickson CreditAttribution: mfredrickson commentedI've rerolled this patch against 1.x-dev
In addition to the earlier comments, I've also made the following changes:
1. Removed the explicit "#name" setting the views_filters() function. Drupal no longer prepends edit[] to everything, so we don't need to explicity set the name anymore. This will help with my second change...
2. Refactored views_filters() into two functions: _views_build_filters_form() and views_filters(). The new function (_views_build_filters_form()) simply constructs a form array of all the ops and filter values and passes it back to views_filters(), which wraps it with the #action, #method, etc that views needs for standalone view pages.
These changes will faciliate other modules using views filters outside the context of views callback pages. (e.g. viewfield and my views node selector form element work).
The api remains unchanged for views_filters(), so you don't need to worry about changing anything.
I've tested this patch with required and optional filter, textbox values and multiselects, choosing one options, or many options, and with the pager. Everything looks good, though I would still appreciate a review.
Cheers,
-Mark
Comment #5
KarenS CreditAttribution: KarenS commentedDid you mean to attach a patch when you said you re-rolled it??
Comment #6
mfredrickson CreditAttribution: mfredrickson commentedI have to stop doing that.
Comment #7
KarenS CreditAttribution: KarenS commentedI'm having trouble getting the patch to apply, plus there is a lot of unnecessary stuff in it. It picked up diffs for all the version name in all the info files. Do you want to try to re-roll?
Comment #8
mfredrickson CreditAttribution: mfredrickson commentedSure. Here is a patch that only applies to views.module, views_query.inc, and views_cache.inc.
Comment #9
Mojah CreditAttribution: Mojah commentedWould this patch have to be applied in order to set an exposed filter according to the node being viewed? I have a view embedded into my page.tpl.php as ...
Am I on the wrong train here? Thanks.
Comment #10
KarenS CreditAttribution: KarenS commentedI think adding this capability is a nice addition and makes sense. I've tried it on a couple test sites, one a fresh install and one an existing site, and it seems to work fine.
I never could get the patch to apply, it was choking on the first and last chunk in views.module, neither of which actually make any changes, and I finally got it working by manually removing both of them from the patch.
So this is a +1 from me. If we can get a cleaner patch, and Earl agrees that the feature is acceptable, I would mark it ready to commit.
@Mojah, yes, you should be able to do that with this patch, but slightly differently than your example. It looks like it would be something like:
Comment #11
mfredrickson CreditAttribution: mfredrickson commentedOk. After a little manual massaging I have a patch that applies cleanly with all the appropriate changes.
Also, I'm changing the version to 1.x-dev, as this patch applies to the dev branch.
I agree with Karen, if we can get Earl's approval, this patch is RTBC.
Thanks to all the testers!
Comment #12
moshe weitzman CreditAttribution: moshe weitzman commentedwell, lets get earl's review then.
Comment #13
merlinofchaos CreditAttribution: merlinofchaos commentedThis one is, indeed, pretty clean and looks good.
Applied to 5.x branch. Would it be possible to backport this to 4.7.x as well? It's probably not necessary, so if you don't no big deal, either.
Comment #14
mfredrickson CreditAttribution: mfredrickson commentedI can back port it. No worries.
Thanks for reviewing and applying. :-)
Comment #15
mfredrickson CreditAttribution: mfredrickson commentedAttached is a backport to 4.7.
This patch is a little smaller than the 5.x version as I didn't split the form building code into two functions. Since in 4.7 you need to set the #name property on everything in the filter form the need for that particular re-factoring went away.
Comment #16
merlinofchaos CreditAttribution: merlinofchaos commentedApplied. I'm trusting you a bit, trying to get through as many issues as possible in fairly limited time.
Comment #17
mfredrickson CreditAttribution: mfredrickson commentedThanks Earl. :-)
Comment #18
(not verified) CreditAttribution: commentedComment #19
.-_-. CreditAttribution: .-_-. commenteddid this patch get commited?