Howdy,
IMO "Exposed filters" should expose only entities previously selected in "Filters".
I explain better my point of view:
if admin decides to set as "Filter" a condition like Node: Type Is One Of
, and restricts it to only 3 content-types (imagine he has 10 all in all), and then decides to expose this filter to his users, i can see no reason for which users should be able to see again all 10 content-types in the dropdown filter menu.
Same can be true for taxonomy terms, stickyness, or whatever else.. admin already made his decision from backend in "Filter" section, so why then allow users to bypass this decision and choose between all the possible choices of that kind from the site?
Thank you
Comments
Comment #1
merlinofchaos CreditAttribution: merlinofchaos commentedThe problem, unfortunately, is that the lists could also be used as defaults. Right now there isn't a way for views to explicitly remove those options from the list. The answer is to do it with theming.
<?php
function phptemplate_views_display_filters_VIEWNAME($view) {
$form = views_filters_form($view);
$options = $form['filter1']['#options'];
// some code to remove unneeded options here
$form['filter1']['#options'] = $options;
return drupal_get_form("views_filters_$view->name", $form, 'views_filters');
}
Comment #2
merlinofchaos CreditAttribution: merlinofchaos commented/me prods Drupal. Hey I properly quoted that. :/ Now i have to write it again. Gr.
Comment #3
marcoBauli CreditAttribution: marcoBauli commentedMerlin, this solution should anyway be easy enough for php n00bs anyway
Thank you for the precious hint!
Comment #4
alanburke CreditAttribution: alanburke commentedTop notch tip!
Just to add,
I was playing around with this, with a list of taxonomy terms, and you need to specify the term ids, rather than the term itself, when selecting which terms to show.
Thanks,
Alan
Comment #5
merlinofchaos CreditAttribution: merlinofchaos commentedGoing to call this fixed; I've set a post in the views snippets area to link to here. Ideally someone with some more time and/or energy could write this up a little more cleanly for that documentation page, but I figured a placeholder would be better than letting this get lost.
Comment #6
(not verified) CreditAttribution: commentedComment #7
seanberto CreditAttribution: seanberto commentedThere's an inconsistency between the two code blocks - one shows "phptemplate..." the latter "theme..."
In either event, with Drupal 5.1 and the latest version of views, I get a undefined function views_filters_form() error message when I add this code to template.tpl.php. Any suggestions?
This is a great tool. I'm really looking forward to getting it working.
cheers,
sean
Comment #8
merlinofchaos CreditAttribution: merlinofchaos commentedAhh, what you use for the function name is based upon your theme, it's probably phptemplate_ -- though you can probably also get away with theme_.
In Drupal 5.1, you have to do a little extra work. I *believe* what you need to do is *instead* of using views_filters_form, you need to do this:
Replace this section
And replace this section:
with:
In fact, this bit solves another problem elsewhere. I need to get a documenter to understand this and write this up sanely.
Comment #9
seanberto CreditAttribution: seanberto commentedHmmm...I tried this in the template.tpl.php for my theme, obviously substituting VIEWNAME for the particular view I'm theming, but it didn't have any effect. I'm no longer getting any errors, but "page" and "blog" are not removed from the exposed filter for node type.
Any ideas? I really do appreciate the help and will keep playing. I'll post if I figure any more out.
-s
Comment #10
seanberto CreditAttribution: seanberto commentedActually, this code did work. I am interested in figuring out how to add the "" option - but this is a good start. Then, I'm also working to figure out how to use this with the views_filter_block module.
Comment #11
dakalaAssuming you don't know if the view filter is filter0, filter1 or filter2 etc. or you don't want to enter the filter fields here again. Try this:
Comment #12
jlee CreditAttribution: jlee commenteddakala - Just wanted to confirm if the above code is working for you on Drupal 5? I get the following errors when pasting the code into template.php (and applying the appropriate VIEWNAME)
# warning: in_array() [function.in-array]: Wrong datatype for second argument in /home/mysite/public_html/themes/mytheme/template.php on line 32.
# warning: Invalid argument supplied for foreach() in /home/mysite/public_html/themes/mytheme/template.php on line 31.
Thanks!
Comment #13
tignux CreditAttribution: tignux commentedI have the same errors of jlee
Just to confirm
Comment #14
jlee CreditAttribution: jlee commentedtignuw - I ended up using Comment #9 from Campsoupster to get this to work...
Comment #15
Anonymous (not verified) CreditAttribution: Anonymous commentedHey - it seems you have developed a solution for my problem! I would love to test this, but I am not 100% sure where to put the php code, i.e. which file would it have to go to into?
Comment #16
Anonymous (not verified) CreditAttribution: Anonymous commentedI have now found the answer (never used template.php before) and can confirm that the code submitted by dakala works for me!
Comment #17
mrgoltra CreditAttribution: mrgoltra commentedsubscribing.
Comment #18
Xabi CreditAttribution: Xabi commentedI'm getting this error with dakala fix:
warning: Invalid argument supplied for foreach() in /var/www/themes/mytheme/template.php on line 67.
Please help! We need this one for 5.x!
Comment #19
dakalaThe code works for Drupal 5.1 and both Garland and Bluemarine themes. If your theme is phptemplate-based I believe it should work. Cheers.
Comment #20
rinnert CreditAttribution: rinnert commentedalright dakala I tried it with garland and I receive this only when I have changed the:
phptemplate_views_display_filters_VIEWNAME
in the right VIEWNAME
I receive this if I put in:
if(!in_array($option, $filters['value'])) {
a value that is in one of the options of the first exposed filter:
warning: in_array() [function.in-array]: Wrong datatype for second argument blabla and a hondred rows more
I don't know what I have to do more, but could you give me some instructions about what to change to get this work with garland? Thanks in advance
I tried to change:
if(!in_array($option, $filters['value'])) {
into:
if(!in_array($option, array('value'))) {
Than I only get two rows of warning: Invalid argument supplied
Comment #21
rinnert CreditAttribution: rinnert commentedI got it working now, I prefer to filter by term id but don't know how, if somebody knows how please let me know :)
This is what I used and added some comments in it for non php experts ;)
Comment #22
armand0 CreditAttribution: armand0 commentedThank you, I finally understand something of form_alter().
I see that it is necessary to locate it in template.php. And how to make that it affects certain View and to which Filter.
Now, my question is: How do I make me that an exposed filter only shows the parents of a taxonomy terms?
Conserving the depth of the search.
For example. If in my taxonomy the parents are States and the children are Cities. The filter only shows the States but conserving that when selecting a state and submit, the result it includes the nodes of all the Cities of that State.
Comment #23
jonfhancock CreditAttribution: jonfhancock commentedFor php n00bs like myself, it may not be completely obvious how to use this method to adjust multiple fields within the same form. Here is how:
Comment #24
Eugene Fidelin CreditAttribution: Eugene Fidelin commentedThis code work for me:
Replace !n! and !m! with real numbers
Comment #25
SocialNicheGuru CreditAttribution: SocialNicheGuru commentedsubscribing
Comment #26
ron_s CreditAttribution: ron_s commentedI tried using the code in #24 and the code in #11 AND the code in #9 and none of them worked for me in Drupal 5.7. This is what I got to finally work. Please note, I also have added in here a
$options[''] = '';
line since I want to have an option of leaving it blank. Just remove this and the asort line if you don't need them.Also note that you must change *X* and *Y* to be the right numbers. The easiest way to figure out which numbers you need is to add the following lines right beneath the
$filters =
line.This will print out to the screen the $options and $filters arrays so you can identify what numbers you need to use.
Comment #27
big_smile CreditAttribution: big_smile commentedAm I correct in thinking that the code in #26 / #24 doesn't require a string of text in template.php that lists the options you want to show (e.g. blog, story)
[where as the code in #2 does require such a string].
I can get the code in #2 to work perfectly in Drupal 5.7 (when using the updates in #8). However, when I try the code in #26, I get a Parse error: syntax error, unexpected '<', expecting ' message. Does anyone have any pointers for how I can overcome this.
^_^
Comment #28
sylv3st3r CreditAttribution: sylv3st3r commentedSimple, just remove <?php from #26 code (p^ ^)---O
Comment #29
upupax CreditAttribution: upupax commentedwhat about drupal 6.x and views 2.0? is it ok even for that?
Comment #30
sunCould someone please add this to the handbooks?
Comment #31
nicks CreditAttribution: nicks commentedHere's a slightly revised version of #26, which should be slightly easier to understand. Note that this solution (and #26) work by reusing the list of default options as the list of options that should appear to the user. #26 uses $view->filter[*n*]['view'] to obtain this list, whereas I use $form['filter*n*']['#default_value'], since I think this makes the behaviour more explicit.
If you want to set a default that is different from the set of visible options, then simply set the $enabled_options variable to be an array containing the options you want to shown to the user. I think this is probably the preferable way of doing things when using a dropdown box, since a dropdown can't correctly represent the situation where multiple items are selected.
Comment #32
doc2@drupalfr.org CreditAttribution: doc2@drupalfr.org commentedWorks great except the ALL option which isn't selected by default.
A feature request for PHP noobs: I have many views using the same filters/exposed filter in the same order. How do I use this code once for them all, please?
BTW, noob may know that the view's name is to take place instead of "browser" in the above-code snippet. And
must not be used in the snippet used in template.php.
Still, thank you very much for this great snipet.
Comment #33
eliza411 CreditAttribution: eliza411 commentedHere's another approach that worked for us. It's placed in the custom module we keep for things like this.
It was important for us that when a user chose All, All meant the choices in pick list, not all the items before they were limited. Also, the arguments for the function make it easy to add new views and filters. They're bolded below. The first one is the position of the filter in the Filters (not Exposed Filters) part of the view and the second one is the id of the filter in the HTML. Both start counting at zero. Unfortunately, the All option still isn't selected by default.
<?PHP
/**
* Implementation of hook_form_alter().
*/
if ($form_id == 'views_filters'){
if ($form['#view_name'] == 'NAME OF A VIEW') {
_<YOUR CUSTOM MODULE NAME>_limit_to_defaults($form, 0, 'filter0', array());
}
}
/**
* array - the form we are altering
* int - the number of the filter (not exposed) which holds the default options
* str - name of the filter to limit, ie. filter1
* array - any extra non default options that should be allowed
*/
function _<YOUR CUSTOM MODULE NAME>_limit_to_defaults(&$form, $default_filter_num = 0, $target_filter = 'filter0', $add_enabled_options = array()) {
//retrieve a copy of the array of options for this filter
$options = $form[$target_filter]['#options'];
// Set the enabled options to be the default options for this field, as set on the Views UI page.
$enabled_options = $form['view']['#value']->filter[$default_filter_num]['value'];
// Add in our extended options if any - '**ALL**' shouldn't probably be added since it has been redefined as the defaults below.
$enabled_options = array_merge($enabled_options, $add_enabled_options);
//Go through each option in turn, disabling it if is not one of the enabled options
foreach ($options as $option => $value) {
if(!in_array($option, $enabled_options)) {
unset($options[$option]);
}
}
$options = array_merge(array('' => 'All'), $options);
//Write the updated $options array back into the form
$form[$target_filter]['#options'] = $options;
}
?>
Comment #34
clown10 CreditAttribution: clown10 commentedI tried more or less all the above, but nothing works for me. There is no change and nothing gets displayed.
I use Drupal 6.4 and views2.
Does this code work for the above configuration?
Comment #35
sun@clown10: No, this issue is about Views 1.x on Drupal 5.
Comment #36
clown10 CreditAttribution: clown10 commentedAh, too bad, but thats what I thought. But is there a way for D6 and views 2?
Comment #37
upupax CreditAttribution: upupax commentedsubscribe this..
Comment #38
merlinofchaos CreditAttribution: merlinofchaos commentedBecause Views 1.x and 2.x are basically different pieces of software, we're not answering Views 2 questions in a Views 1 thread; it completely pollutes the thread.
Comment #39
lelizondo CreditAttribution: lelizondo commentedsubscribing
Comment #40
lee20 CreditAttribution: lee20 commentedSubscribing...
Comment #41
zlex CreditAttribution: zlex commentedsubscribing...
was there ever any more work done on this issue? I too have several views that would need this applied to them.
Comment #42
goose2000 CreditAttribution: goose2000 commentedVery helpful, thanks people. I am now thinking that I could alter the 'options' as well as limiting them as this thread shows. I'm faced with the problem the CCK user reference displays to the end user a list of usernames (when looking at an exposed filter), and not something friendlier like a last or full name.
I know this was tackled by the RealName module for D6 and Views 2 but not satisfactorily for D5. I think this will help...
Comment #43
esmerel CreditAttribution: esmerel commentedNo more serious work is going into views 1.