I'm trying a functionality with a content type that has several fields as follows; full name being the node title, year born being a cck field (type=integer) and another is a computed field using a computed field that returns the age category of the person. the php code snippet follows;
$currentyear = date("Y");
$ageborn = $node->field_year_born[0]['value'];
$currentage = $currentyear - $ageborn;
switch ($currentage) {
case ($currentage >= 21 && $currentage <= 30):
$node_field[0]['value'] = "21-30";
break;
case ($currentage >= 31 && $currentage <= 40):
$node_field[0]['value'] = "31-40";
break;
case ($currentage >= 41 && $currentage <= 50):
$node_field[0]['value'] = "41-50";
break;
case ($currentage >= 51 && $currentage <= 60):
$node_field[0]['value'] = "51-60";
break;
case ($currentage > 60):
$node_field[0]['value'] = ">60";
break;
}
then i created a views that returns the node's title, year born and the age category. everything goes fine.
now here comes the problem. i created an exposed filter on the age category but the input type is in textfield. i have to know what are the age categories and key in manually in the textfield. i thought of creating a dropdown select list that shows the age categories. how do i do such a feature? views hook?
Comment | File | Size | Author |
---|---|---|---|
#16 | title-expose-1.JPG | 3.56 KB | saiya |
#16 | title-expose-2.JPG | 22.78 KB | saiya |
#16 | title-expose-3.JPG | 26.78 KB | saiya |
Comments
Comment #1
francoud CreditAttribution: francoud commentedI also need this... no suggestions?
Comment #2
amal850720 CreditAttribution: amal850720 commentedglad to find someone else doing and asking for the same thing. mostly suggest using views_hook. i'm digging that for now.
Comment #3
TechNikh CreditAttribution: TechNikh commenteddid you find a solution for this?
Comment #4
amal850720 CreditAttribution: amal850720 commentednope. negative.
Comment #5
Jegan2668 CreditAttribution: Jegan2668 commentedsubscribe
Comment #6
spencerbrooks CreditAttribution: spencerbrooks commentedI did this by creating a small module that implements hook_form_alter. For example, this is the code I used to turn a text field into a dropdown box of decades (obviously you'll need to replace with your content type):
Comment #7
alanpeart CreditAttribution: alanpeart commentedI can confirm that this approach works. In my case I needed to have a drop-down of node titles as a filter (a fairly common use case I think), so the above code combined with a bit of SQL to get the titles did the job.
Comment #8
LTech CreditAttribution: LTech commentedCould you please explain exactely where to put the code in #6. I'm new to drupal and I want to turn a computed field text area into a drop down views exposed filter.
Thanks so much. (I'm using drupal 7)
Comment #9
LTech CreditAttribution: LTech commentedI worked out how to create a new module. http://www.ubercart/comment/8749/Re-Removing-SKU-product-edit-page was a helpful guide.
I've used the code in #6. It is great except that I get the error:
Notice: Undefined index: #options in age_form_alter() (line 16 ' $form['field_age_value']['#default_value'] = $form['field_age_value']['#options']['All'];
An illegal choice has been detected. Please contact the site administrator.
If I put $form['field_age_value']['#options'] = array(...); before the $form['field_age_value']['#default_value'] line it removes the first part of the error but I am still left with:
An illegal choice has been detected. Please contact the site administrator.
I can't seem to get rid of this. Any ideas what I can do?
Thanks
Comment #10
LTech CreditAttribution: LTech commentedI finally found the solution.
Add the line:
$form['field_age_value']['#options']= t("");
to remove the error:
An illegal choice has been detected. Please contact the site administrator.
Comment #11
kay.beissert CreditAttribution: kay.beissert commentedThis solution sounds got to me but I have nodes with a date field (only the Year) and need an exposed filter with three dropdown date ranges (0-15 years, 15-30 years, older than 30 years). Im no php-guy, tried to snip around this code but had no luck. Can someone help me out with a shiny, little code snipped for my exposed filter?
Thanks a lot ;)
Comment #12
cknoebel CreditAttribution: cknoebel commentedThis thread was a huge help. I modified #6 to look up values from a Webform table, which I'm using to populate the exposed filter. The View groups results by "captain" and the code here populates the exposed filter with values for "captain" which lives both on the node in a CCK field and in the Webform table. I guess I could have grabbed it from all the nodes of this content type, but I have other shenanigans going on that led me down this path.
Comment #13
LTech CreditAttribution: LTech commentedI create a module for a dropdown exposed filter similar to #6 called age_range. It has been working great. I just added another computed field to my content type called age_range_test and even before I put any code in it, it has broken my module. I deleted the field age_range_test but the module for age_range still won't work. Does anyone know why another computed field should affect the module?
How can I get the module to work again? I'm really stuck, any ideas would be really appreciated.
Thanks
Comment #14
sumaiyajaved CreditAttribution: sumaiyajaved commentedIf you have done the above and still it is not working .. try clean cache (admin/settings/performance) or reset computer ... worked for me then.
thanks guys for the help
Comment #15
LTech CreditAttribution: LTech commentedit's working, thanks
Comment #16
saiya CreditAttribution: saiya commentedexcuse me sir,
referring to #6.
i wanted to change exposed filter from textfield to dropdown list. but im new to drupal. and i dont know how to implement the code.
this is my sample:
1. i created a filter with node title.
2. then i create an expose to that title. operator is "Start With"
3. the output is to make user just input start letter. so i want to make it as a drop down list. example like this:
0-9
A
B
C
... until Z
the problem is, where i can put the code given at #6?
Comment #17
dhills CreditAttribution: dhills commentedI am looking to do the same as most people above, an exposed filter with a dropdown of allowed values instead of a textfield. Unfortunately, I do not know what a hook is or what to do with it, or understand php, how to write it or where to put it. Is there no module to do this directly? If not, can someone point me to resources that a newbie can use to perform such a function?
Thanks!
Comment #18
Anonymous (not verified) CreditAttribution: Anonymous commentedI have this code (Drupal 7) but it keeps saying "An illegal choice has been detected. Please contact the site administrator." whenever I even touch the options value. Any idea how to fix it?
Comment #19
end user CreditAttribution: end user commentedHave a look here. This module turns a text field (my users enter their city name into it when they create a mode) and turns it into a drop down when exposed in views.
http://drupal.org/node/1549250#comment-5932296
Comment #20
edrupal CreditAttribution: edrupal commentedWith ref to Kline's issue in post #18 it may be that the $options array doesn't have a null value setting. Try adding something like
$options = array("" => "All");
Ed
Comment #21
Anonymous (not verified) CreditAttribution: Anonymous commentedThanks, I'll give that a shot.
Comment #22
ThirstySix CreditAttribution: ThirstySix commentedit's working, thanks..
Thanks for all..
Comment #23
rodpascoe CreditAttribution: rodpascoe commentedCan anyone help with a Drupal 6.x version of this?
I have tried to use
<?php
function thenps_form_alter(&$form, &$form_state, $form_id) {
if ($form_id == "views_exposed_form") {
$form['distance[search_distance]']['#type'] = "select";
$form['distance[search_distance]']['#size'] = null;
$form['distance[search_distance]']['#default_value'] = $form['distance[search_distance]']['#options']['All'];
$form['distance[search_distance]']['#options'] = array(
'All' => t(''),
'5' => t('5'),
'1' => t('1'),
'25' => t('25'),
'50' => t('50'),
);
}
}
But nothing I try is working. I wondered if it was because the fields were part of an array?
Would love some feedback!
Thanks
Comment #24
wing112707 CreditAttribution: wing112707 commentedThanks it helps..
Comment #25
rodpascoe CreditAttribution: rodpascoe commentedCan nobody help? :-(
Comment #26
stevenx CreditAttribution: stevenx commented#18 and #20
works fine for me.
thanks!
Comment #27
keyswebsites CreditAttribution: keyswebsites commentedYou can set an exposed filter to use pre-defined values presented in a dropdown by selecting "grouped" in the filter configuration page.
Comment #28
lias CreditAttribution: lias commentedThank you keyswebsites for sharing this simple way to do create a category dropdown from a computed age field! I have been struggling with this and you've provided the "ah-ha!" moment.
Comment #29
creatile CreditAttribution: creatile commentedI try to do the same thing with a date exposed filter,
And it doesn't work.
I get the following message:
Warning: Illegal offset type in isset or empty in _form_validate() (line 1326 of /Users/creatile/Sites/datetest/includes/form.inc).
An illegal choice has been detected. Please contact the site administrator.
Notice: Array to string conversion in devel_watchdog() (line 531 of /Users/creatile/Sites/datetest/sites/all/modules/devel/devel.module).
here is my code
Comment #30
gabrielZ CreditAttribution: gabrielZ commentedFound a simple solution Check here http://bryanbraun.com/2013/08/06/drupal-tutorials-exposed-filters-with-v...
Comment #31
ashwinshPoint #12 Works for me, Thank you knoebel.
Comment #32
kumkum29 CreditAttribution: kumkum29 commentedHello,
i have followed your differents posts to change a textfield of a view exposed filter to a list options.
It's ok for me, but I get a
<optgroup...>
in the beginning of my list.What I must change in my code for hide this
<optgroup>
?Thanks for your help.
////////////////////////////// UPDATE
I have found the solution. In the options, i have an array inside an array.
I have simplified the code and it's ok : $form['p1']['#options'] = $node_options;
Comment #33
Johann Wagner CreditAttribution: Johann Wagner commentedThanks gabrielZ #30, Better Exposed Filters works like a charm.
Comment #34
Anonymous (not verified) CreditAttribution: Anonymous commentedI too wan't something like this in #16 but I don't know any coding. Is there a module for this yet?
It's really useful and there should be.
Comment #35
eebanos CreditAttribution: eebanos as a volunteer commentedUsing the code provided in the comment #18
function MYMODULE_form_views_exposed_form_alter(&$form, &$form_state) {
if($form['#id'] == 'views-exposed-form-special-page-1')
{
$form['field_computed_value_1']['#type'] = 'select';
$form['field_computed_value_1']['#size'] = null;
$form['field_computed_value_1']['#default_value'] = No;
$form['field_computed_value_1']['#options'] = $options;
}
USE WITH CAUTION!!!
It seems that the error cames from the AJAX validation used in the form to avoid injection into the database:
Just add this line: $form['field_computed_value_1']['#validated'] = TRUE;
Source:
https://drupal.stackexchange.com/questions/15989/resolve-the-error-an-il...
http://drupal.stackexchange.com/questions/31846/illegal-choice-has-been-...
http://drupal.stackexchange.com/questions/96669/how-to-avoid-an-illegal-...
Comment #36
rajeshsaikrishna CreditAttribution: rajeshsaikrishna as a volunteer commentedComment #37
MrsWho CreditAttribution: MrsWho commentedI was able to turn a list of nodes into an exposed filter using the Views Reference Filter module.
Comment #38
Vali Hutchison CreditAttribution: Vali Hutchison commentedLike a few others I was getting the "An illegal choice has been detected. Please contact the site administrator." error message when converting a text filter to a select filter.
Managed to fix this by using a null value for the default select option rather than 'All'.
E.g.
$options = array('' => '- Any -');
Comment #39
harishpatel86 CreditAttribution: harishpatel86 commentedSome correction in #6 post. For remove "An illegal choice has been detected. Please contact the site administrator." error message.
function decadeselect_form_alter(&$form, &$form_state, $form_id) {
if ($form_id == "views_exposed_form" && $form['#id'] == "views-exposed-form-exposed-filter-search-page-1") {
$form['field_collection_item_decade_value']['#type'] = "select";
$form['field_collection_item_decade_value']['#size'] = null;
$form['field_collection_item_decade_value']['#default_value'] = $form['field_collection_item_decade_value']['#options']['All'];
$form['field_collection_item_decade_value']['#options'] = array(
'' => '- Any -',
'2000s' => t('2000s'),
'1990s' => t('1990s'),
'1980s' => t('1980s'),
'1970s' => t('1970s'),
'1960s' => t('1960s'),
'1950s' => t('1950s'),
'1940s' => t('1940s'),
'Prior to 1940' => t('Prior to 1940')
);
}
}
Comment #40
gaurishankar#39 working for me. But needs minor correction here : REMOVE line $form['field_collection_item_decade_value']['#default_value'] = $form['field_collection_item_decade_value']['#options']['All'];
Comment #41
Shivamverma CreditAttribution: Shivamverma commentedSimply try this one Helps you regarding Issue Illegal Choice has been detected ----->
$form['field_location_dependent_locality']['#options'] = array("" => "All",t('duhv'), t('sfjns'), t('sdknsk'));
Comment #42
zawlinnoo CreditAttribution: zawlinnoo commentedIn case, if you don't want to add default value and avoid "An illegal choice has been detected. Please contact the site administrator.", use this.
$form['field_name']['#validated'] = TRUE;
that will be updated with AJAX on select.
Comment #43
christinlepson CreditAttribution: christinlepson at Mindgrub Technologies commentedI made a blog post for this issue if anyone is stuck
Comment #44
colanThere's been enough interest in this over the years through many major Drupal versions. As such, I'm in favour of supporting this natively.
We could do what Views Autocomplete Filters is doing, but implement it as a select list instead. Drupal 7 Views drop-down exposed filter for a text field may be helpful here, even though it's for Drupal 7.
Oh, and if you're thinking of using Views Autocomplete Filters to get an autocomplete field instead, it doesn't yet support Computed Fields. If you're interested in that, create an issue in that queue, and link to it from here. Thanks!This actually works: You just need patches from #2986201: "Add some fields to view" option is always empty and #3083093: Correct handling for autocomplete with(out) dependent filters.Comment #45
colanComment #46
colan