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

Chris Gillis’s picture

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.

michellezeedru’s picture

I realize this post is a little old, but don't see a solution anywhere. Has anyone found a way to do this?

johnhanley’s picture

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

            $options[$line] = $line;

with this

            $parts = explode('|', $line);
            if ($parts[1]) {
              // different key & value pairs, separated with vertical bar (|)
              $options[trim($parts[0])] = trim($parts[1]);
            } else {
              // use default line of code; use string for both key & value
              $options[$line] = $line;
            }

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

johnhanley’s picture

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.

  if ($type == 'selection') {
    $form['fields']['options'] = array('#type' => 'textarea',
      '#title' => t('Selection options'),
      '#default_value' => $edit['options'],
      '#description' => t('A list of all options. Put each option on a separate line. Example options are "red", "blue", "green", etc. You can optionally enter a label using the format key|label. The key will be used as the label if no label is specified.'),
    );
  }

-------------------------------------------------------

"The sting in any rebuke is the truth." - Benjamin Franklin

roball’s picture

Bacteria Man,

I have posted a patch for Drupal 6.9 based on your code at http://drupal.org/node/375307.

Best,
rob.

-- Robert

johnhanley’s picture

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

johnhanley’s picture

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:

/**
 * Implementation of hook_form_alter().
 *
 */
function user_form_alter($form_id, &$form) {
  // check to see if form element exists
  if (isset($form['Personal']['profile_favorite_color'])) {
    $colors = array();
    // get key/value pairs for dropdown
    $result = db_query("SELECT color_id, color_name FROM {colors}");
    while ($row = db_fetch_object($result)) {
      $colors[$row->color_id] = $row->color_name;
    }
    // update options attribute for form element
    $form['Personal']['profile_favorite_color']['#options'] = $colors;
  }
}

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

Guru’s picture

Thank you! That was an excellent tip and snippet.

+1

roball’s picture

John, that solution looks promising. Into which file should that code go?

-- Robert

johnhanley’s picture

Robert,

You'll want to add the function to a new or existing custom module.

Cheers,
John

roball’s picture

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

johnhanley’s picture

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.

roball’s picture

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

johnhanley’s picture

Robert, nicely done!

This is definitely an overdue feature of profile.module.

BioALIEN’s picture

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

nicodv’s picture

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