I know this question has been asked in the forums before, but I cannot seem to find an answer to the problem. It seems like a rather simple thing at first glance, so maybe I'm just missing it.
I have a field called birthdate on my user profile content type. I have a view that displays all nodes of this type (user profiles). I have an exposed filter for birthdate that allows users to search within a range of birth years. This works, but it's not really what I want.
I'd like to create an exposed filter that uses the birthdate field to acquire an age and then allow users to filter based on an age range. Now, I could create a computed field to store the age. In fact, I'm using a computed field to display the age on the user's profile page, but I'm not storing the value of the computed field in the database because it will never be updated with the correct age for each user unless they edit their user profile node and re-save it. Right now, the computed field is getting the age on the fly when someone views a user's profile page. This is fine, of course, and exactly what I want, but it doesn't help me with the exposed filter.
I'm lost and hoping someone has done this and can help me. It seems like something that a lot of people would need, especially if they are allowing users to search the profiles of other users. I'd like to create a solution that others can use, since I've seen this question asked a lot and haven't seen an answer that works yet. (Again, if there's a discussion about this with a solution, please let me know. I've searched and haven't found one, however.)
Thanks for any help that can be provided. I really appreciate it! The site I'm working on will go live soonish, and I know that this is one of the first things that the users will notice if it's missing.
Comments
Comment #1
dawehnerConfigure the exposed filters by clicking is currently not possible. There is a feature request #731662: Hybrid Exposed Filters, but its far from being usable.
Sadly you didn't said, what birthdate is a kind of date. I hope that its a date field.
In general, you need some custom coding, but there are two ways todo it:
Comment #2
dmetzcher commentedYes, it is a date field.
Sadly, I have no idea how to do either of these two things. Is there a guide I can use? Or what would be the best way to educate myself?
I'm still a little shocked that it's this difficult... this seems like something that would be very commonly needed. I was hoping there was a module that someone had already written.
Comment #3
drupdrips commentedWhat dereine said is actually the best way and I would say the more drupal friendly way to do it.
You may benefit from storing the information in the database as it will help avoid doing too much computation when trying to do a search based on filters/views for all of the nodes. This computation really should happen only once a year for a given user. If you have a lot of nodes, it could cut down on a lot of extra repetitive processing for the computed field during views search or even profile views. So basically in the computed field php code you could determine if the value already exists in the database, and if it is, then serve it up or else compute. A daily cron task via hook_cron can also check which users have the same day and month for their birthdate as the current day's, and if any user matches then recompute the value and save it into the db.
In regards to the filter age range, in my case, I used the "Views Filter Block" to take my filter and place it on another page like this for example :
http://www.notforplayers.com/find-a-date
Look at the line for the age range display. For this, I did some hacking on the code output of views filter block to get the desired drop down values, although they do not belong to any of the actual CCK fields that I could expose a filter for -> which is where the "hybrid exposed filter" idea that dereine links to would be a good solution if/when works.
Comment #4
dawehnerIf you know php it might be good to understand how drupal module development works.
If you know this you can implement the hooks.
So all in all you need quite some custom stuff.
Comment #5
championsgate7 commentedWe are trying to create same search functionality by age like in http://www.notforplayers.com/find-a-date. So far no success. Could you be a little bit more specific of how you’ve tweaked the views or what code exactly you used?
Thank You
Comment #6
dmetzcher commentedI ended up creating a Computed Field and using it to store the user's age in the database.
I'm then updating the ages of all users who have a birthday each morning using the Rules module. If the user edits their profile after they have a birthday the computed field will update the database with the new age, but the Rules module lets me run some code that will update the birthdays of users automatically each morning for users who have a birthday on that morning.
It's not ideal, but it works.
Comment #7
dagmarSo, it is fixed.
Comment #9
punges24 commenteddmetzcher, I am having a similar issue as to what you were trying to do. Can you tell me how you use Rules to update the birthdays every morning?
As for the filters, how did you get this to work? I also use Computed Fields in order to get an age from the birthday but when I use exposed filters between to try to search it has a problem with the max field because if someone is 25.5 and you are looking for anyone up until 25 it wont pull up anyone over 25, meaning if the computer calculates it as 25.1 it wont come up anymore. Do you have this problem also?
Thanks, I hope you can be of help.
Comment #10
fehin commented@dmetzcher, please share your computed field code and the rule setting you are using. Thank you.
Comment #11
mustanggb commented