By johnhanley on
Hello,
I would like to populate a profile list selection field from a query result versus a double quote comma delimited list. I am prepared to make this patch myself, but am wondering if someone has already created it. I checked the site without success.
The idea would be to include an additional input on the "Add new list selection" page to specify a query. For example:
$query = "SELECT users.* FROM users";
$result = db_query($query);
$users = array();
$users['0'] = '-- choose --';
while($row = db_fetch_array($result)) {
$users[$row['uid']] = $row['name'];
}
form_select(t(Users'), 'user', $_POST['user'], $users, null, 0, false, true);
All comments welcome.
Comments
Me Too!
I also require this functionality. CCK already has it working - why not in the profile? Please let me know if you have found a solution.
Me three!
I realize this post is a little old, but don't see a solution anywhere. Has anyone found a way to do this?
whoa, old issue is right
This was back in my 4.6 days. (Sidebar: when they talk about the good ol' days, they're not talking about drupal 4.6.)
Interestingly the profile.module for 5.10 still doesn't support populating a list selection from a query nor does it support key/value pairs.
I recently broke from "best practices" and modified the profile.module ever so slightly to support key/value pairs.
Replace line 684, which reads
with this
Now, in the selection options input, you can specify different key & values pairs like
USD|U.S. Dollar (USD)
AUD|Australian Dollar (AUD)
You could devise a similar solution to do the same from a query.
HTH,
John
"The sting in any rebuke is the truth." - Benjamin Franklin
one more thing
If you decide to go with the above modification to profile.module, you'll also want to add some additional text to the list selection textarea description.
-------------------------------------------------------
"The sting in any rebuke is the truth." - Benjamin Franklin
Drupal 6.9 patch for labeled options in selection field
Bacteria Man,
I have posted a patch for Drupal 6.9 based on your code at http://drupal.org/node/375307.
Best,
rob.
-- Robert
way overdue
Thanks, Rob.
This feature is way overdue for inclusion in profile.module.
That said my previous example illustrates of how to achieve the same thing with hook_form_alter(). This technique has the added benefit of defining key|value pairs from a query or system variable array.
Cheers,
John (aka. Bacteria Man)
-------------------------------------------------------
"If you don't read the newspaper you are uninformed;
if you do read the newspaper you are misinformed."
-- Mark Twain
how to achieve without hacking profile.module
I recently revisited the idea of populating a profile list selection via a query and discovered it's quite easy without hacking profile.module (as previously illustrated.)
The solution is to use hook_form_alter(). For example:
The above example assumes:
1) category == 'Personal'
2) title == 'Favorite Color'
3) database table 'colors' exist w/structure: color_id (int), color_name (varchar)
4) form name == 'profile_favorite_color'
5) selection options == [blank]
6) user_form_alter does not already exist
-------------------------------------------------------
"The sting in any rebuke is the truth." - Benjamin Franklin
Excellent
Thank you! That was an excellent tip and snippet.
+1
John, that solution looks
John, that solution looks promising. Into which file should that code go?
-- Robert
custom module
Robert,
You'll want to add the function to a new or existing custom module.
Cheers,
John
Thanks John! I'm new to
Thanks John! I'm new to Drupal module development, but planning to dig deep into that stuff once the time allows it. Would you like to start creating a small and simple module with your key/value selection field functionality in it?
-- Robert
getting started
At the very least you'll need .module and .info files.
The info file only needs to contain a few entries in order for the system to recognize it on the admin modules page. The module file could be as simple as containing only the single hook_form_alter() function.
I'd suggest looking at an existing third party module for an example and read the module development documentation.
hook_form_alter hook patch for user.module
Thanks John! For now, I have added the function to the core user.module for implementing the hook_form_alter hook. I have posted the patch for Drupal 6 at http://drupal.org/node/400904#comment-1357568
-- Robert
patch
Robert, nicely done!
This is definitely an overdue feature of profile.module.
Totally agree
We need to get this into D7 before we can backport it to D6: #375307: Allow key|value pairs in selection fields of profile.module
If we can incorporate this feature to it, we stand more chance of getting this in quickly: #41846: No option should be selected by default for required select fields
great
I was looking for something like this. I'm kinda new in drupal, and I'm developing in d7. Do you know how would this change for 7?
thanks
THE VERY LITTLE AGENCY