I am using profile.module in order to add fields to my user profiles. I want to put a drop-down box in my user profiles, from where the person creating a new account can select from a list of options.

This is all well and good, but I'd like to include php code in that list of options: and whenever I open php tags, Drupal just displays everything word-for-word within the drop-down box. I've checked that I'm using the full HTML input filter. I guess I'll have to fiddle around with the validation system for profile.module. Can anyone give me any pointers?

Comments

jim0203’s picture

I should add that I've now found the php.module which adds the PHP input filter (didn't know this had been done since D5!) and turned it on, so PHP snippets should be acceptable. However, I'm still having the same problem here. I'm able to add PHP to nodes, though, so it must be something specific to profile.module.

jim0203’s picture

Is what I want to do not possible without a serious amount of code? Anyone know? If it is tricky then I'll start pursuing other ways of getting Drupal to do what I want it to do.

WorldFallz’s picture

afaik, core profile module doesn't have the ability to use php code in the select list. you'ld have to use content_profile to turn profiles into full fledged nodes which can then use cck.

===
"Give a man a fish and you feed him for a day. Teach a man to fish and you feed him for a lifetime." - Lao Tzu
"God helps those who help themselves." - Ben Franklin
"Search is your best friend." - Worldfallz

jim0203’s picture

Is this a necessary feature of profile.module, or is there some way I can force it to let me use PHP? Anyone know?

My knowledge of PHP is pretty limited, but I'm guessing that it's some kind of validation that's causing the PHP not to work, so I need to know how to turn this validation off.

Woodside’s picture

I came across this same issue, I ended up creating a select box in the profile admin, then altering it using hook_user, $op = register used php to build an array for my options. Let me know if you need more info.

jim0203’s picture

Woodside, that's really interesting and would be really useful to me.

What I'm ultimately looking to do is to provide a select box in the profiles of users of certain types, which contains all users of a second type. I guess I could hard-code this into Drupal, but I think there would be more functionality if I could get it serve up a list of items based on a view. Any idea how I could go about doing this?

WorldFallz’s picture

I'm not sure what you mean by "hard code"-- woodside's solution is a 'drupal' way of doing it (without modifying core code). You might also be able to do it with a hook_form_alter on the profile form. The bottom line is, if you really want to stay with the core profile module you're going to have to write some code. To do it without code you can use the content_profile module.

===
"Give a man a fish and you feed him for a day. Teach a man to fish and you feed him for a lifetime." - Lao Tzu
"God helps those who help themselves." - Ben Franklin
"Search is your best friend." - Worldfallz

jim0203’s picture

I might be wrong, but it seems from what woodside was doing was adding a field to the profile and then providing options for that field by creating an array. What I want to do is to have the options available to that field provided by a view.

WorldFallz’s picture

Yes I get that-- and you can do that with php and some sql, but it is going to involve coding which your original post says you wish to avoid. Other than the content_profile module, i don't know of any other way that doesn't involve coding. That's the only point I was trying to make.

===
"Give a man a fish and you feed him for a day. Teach a man to fish and you feed him for a lifetime." - Lao Tzu
"God helps those who help themselves." - Ben Franklin
"Search is your best friend." - Worldfallz

jim0203’s picture

You're right, I originally wanted to avoid coding, but now it seems inevitable I guess I'll have to (plus I kind of want to start getting my hands dirtier with Drupal anyway).

So the code I'm now looking to write is something that will allow me to add a drop-down box (or one of those cool AJAX things that predicts what you want based on what you type), with the options available in that drop-down being provided by a specific view. But I don't even know where to get started.

WorldFallz’s picture

I'm not trying to be evasive-- I don't have a quick answer. if it were me I would probably start by examining the profile.module code that creates the existing select list. Then I would add 2 core select list fields to a profile (one with items, one without), examine the results, and try using hook_form_alter (which you'll need to create a module to use) to progammatically add some test select items to the empty one. Then, once that works, I'd work on grabbing the items from a view.

I'm no expert, so there may be better ways-- that's just what I would try. Hopefully woodside will respond with more details of his method.

===
"Give a man a fish and you feed him for a day. Teach a man to fish and you feed him for a lifetime." - Lao Tzu
"God helps those who help themselves." - Ben Franklin
"Search is your best friend." - Worldfallz

bartezz’s picture

Old thread... came here for something similar. I wanted a select list of available countries in my ubercart setup...

My solution was a custom module, create a profile_dynamic_select.info, profile_dynamic_select.install and profile_dynamic_select.module file. Here's the code for the .module file.

/**
 * Implementation of hook_form_alter().
 * Add the pattern field to the node edit form for comments.
 */
function profile_dynamic_select_form_alter(&$form, &$form_state, $form_id) {
	switch ($form_id) {
		case 'user_register':
			$result = db_query("SELECT * FROM {uc_countries} WHERE version > 0 ORDER BY country_name ASC");
			while ($country = db_fetch_object($result)) {
				$countries[$country->country_id] = $country->country_name;
			}
			if (is_array($countries)) {
				// I've created a field called profile_country in a fieldset Personal Information.
				// So if you do something different than this then make sure you use the right array path to the variable!
				$form['Personal information']['profile_country']['#options'] =  $countries;
			}
		break;
	}
}

Ofcourse this is a specific example but you can do this with any database call or some other cool php option generation snipplet ofcourse...

Cheers

________________
Live fast die young

texas-bronius’s picture

Works great, thanks for sharing here! I also added a default value on my select fields in admin/user/profile of "(values provided by module_name: something went wrong)" to help guide anyone debugging in the future.

--
http://drupaltees.com
80s themed Drupal T-Shirts

bartezz’s picture

Glad it helped... and no thanx. That's waht the community is about. Just make sure you always post back your solutions after looking for an answer :)

Cheers

________________
Live fast die young