Support for Drupal 7 is ending on 5 January 2025—it’s time to migrate to Drupal 10! Learn about the many benefits of Drupal 10 and find migration tools in our resource center.
Hi All,
Has anyone had any success exposing a filter for a computed field, and turning this in to a selection box? For example - computed field outputs Monday, Tuesday, Wednesday, Thursday or Friday as an output. I would then like a user to be able to filter a view based on these values, but from a select box. Currently, it seems like support is only available for a text box entry.
Thanks
Comment | File | Size | Author |
---|---|---|---|
#8 | my_module.zip | 908 bytes | werushka |
Comments
Comment #1
flying_q CreditAttribution: flying_q commentedComment #2
ds1964 CreditAttribution: ds1964 commentedI've been trying to figure out the same thing for awhile and recently came up with something that seems to work.
1) I implemented the "mymodule_tools_form_views_exposed_form_alter" hook in a mini-module that I've created for various helper functions.
2) I placed the following code w/in the hook function:
3) In the View's administration page, I set the "Value" field for the exposed computed field filter to "All".
The last step was necessary in order to clear an “An illegal choice has been detected. Please contact the site administrator.” error message that popped up on the View's display page otherwise. I should note that neither the default value property nor the "All" selection in the View definition affected the status of the HTML select field widget -- this was always set to the first value in the select list (in this case "All") on page load. However, after that everything seems to work normally.
This is actually the first time that I've implemented a Drupal hook, so if I'm doing anything wrong (or if there is a better way to approach the problem) I'd appreciate hearing about it.
I hope this helps!
Comment #3
flying_q CreditAttribution: flying_q commentedESC - thanks so much for the response....instead of using a computed field I was actually fortunate enough to use the tokens available from the Date module. I stripped the 'day' field from the date a user selected (via the really slick JS Calendar popup). At time of Node-save I just created a simple rule set that said save Day from Date into Field XYZ. I am then able to filter based on those values.
Regards.
Comment #4
ds1964 CreditAttribution: ds1964 commentedflying_q ,
Thanks for the info. I had also been thinking about something along the same lines as your approach, but still have a couple questions.
1) What type of field is "Field XYZ" in your example and does it have an allowed values list? I've only been able to get select lists for allowed values fields (as opposed to a select list of actual current field values).
2) Sounds like you are using the Rules module to implement the save-time action. Is that right?
Appreciate any more info on these questions.
Best
Comment #5
Mac Clemmens CreditAttribution: Mac Clemmens commentedsubscribing
If there is a way to do this, that would be very helpful!
Comment #6
werushka CreditAttribution: werushka commentedI am trying to do the same thing...
I am using template.php way. is it possible if you can provide the complete function that you would put to template.php if you were doing my way...I would appreciate some feedback
Comment #7
TomMynd CreditAttribution: TomMynd commentedHi,
I currently banging my head against this issue. I have a computed field called "age" that is filled up by a computed value from a birthday field. I want to have an exposed filter, where the user can select an age to filter only nodes which have the appropiate age.
Currently I'm stucked.
Comment #8
werushka CreditAttribution: werushka commentedi am attaching the module i created dont forget to change the field_name in the module then enable the module if you are on drupal6 create exposed filter with the value Is Equal to "All"
Comment #9
TomMynd CreditAttribution: TomMynd commentedHi,
thank you for this little snippet. I installed the module and after that the specified fields from "options" are shown and I can select those without problems. But when I hit the page, there is an error message "An illegal choice has been detected.".
Also when I select the "All"-Value inside the selectlist, no entry is shown.
I peeked inside the SQL-Queries for that page with the devel-module and the SQL for retrieving the data is:
I have set the default value for the filter of the Age-Field to "All".
And yes... it's Drupal6 with the newest available CCK and Views.
Best regards, Tom
Comment #10
TomMynd CreditAttribution: TomMynd commentedShame on me... I looked deeper and came to a simple solution. The computed field is an integer and not varchar (or somethings other text based), so I switched the default value for that field to a nuimber and not to "All" and it went smooth.
Best regards, Tom
Comment #11
werushka CreditAttribution: werushka commentedwould you happen to have idea about how we would find age range?
Comment #12
TomMynd CreditAttribution: TomMynd commentedI found this over at gdo: http://groups.drupal.org/node/25941
I currently filter the age with "is less than" and not in a range with "in between". With that you will have 2 fields for the exposed filter. These both fields can be combined and after submitting they will be splitted for use inside the view.
Comment #13
kris_mcl CreditAttribution: kris_mcl commentedThanks everyone, this is great info & has gotten me close to what I want to do. Using werushka's module technique from #8, I've got my exposed filter showing up as a select.
The only thing that's left for me is to populate that select with possible values on the fly instead of hard-coding a list of options in my module_form_alter function. I have a CCK computed field that I'd like to use as the possibilities list. Any ideas on how to do this? I've examined the entire $form variable, but haven't been able to get my computed fields included in there so that I can loop through them & add them to the select list.
I've got the node reference defined as a relationship, and added the computed field to the Fields list in my view, but that hasn't done the trick.
Thanks in advance for any advice!
Update: Got it!
I'm not sure if this is the best way to do it, but it did the trick for me. I just manually queried the DB for the fields I needed & built an array with the results, then used that array for my $form['field-name']['#options'] value. Here's my new mymodule_form_alter function:
This works well. Thanks everyone!
Comment #14
OFF CreditAttribution: OFF commentedMy solution:
Comment #15
amaisano CreditAttribution: amaisano commentedWorks great for me as well! It would be even better though if this could be tweaked to allow multiple values, and use checkboxes instead. Is that possible?
I've tried myself, but my syntax and form settings are probably getting me in trouble. I've made sure the Views filter for this computed field is set to "Contains any word."
I need the URL to look like this:
...field_game_dow_value=Monday+Tuesday+Wednesday
But instead it looks like this:
...field_game_dow_value%5BMonday%5D=Monday&field_game_dow_value%5BTuesday%5D=Tuesday&field_game_dow_value%5BWednesday%5D=Wednesday
Comment #16
chinita7 CreditAttribution: chinita7 commentedI modified the code from #8 for myself but form doesn't show up as a select list.
I'm not a coder and don't really understand php so I just changed the fom id and the field name.
'field_nedan' is a computed field I want to display as select list in exposed filter and has value like '1000-2000' '1000-2000'
Am I still missing something?
Comment #17
dqdDue to the Drupal core life cycle policy and security support advisery, Drupal 6 is no longer supported. So issues for Drupal 6 cannot be longer maintained. The project maintainer has asked for closing all D6 issues to clean up the issue queue.
Comment #18
colan