views:

77

answers:

2

I am using the profile module and have several categories for different fields. I want to add a small bit of text to the top of one of the categories saying what the category is for. The information would be displayed when a new user registered. Basically I want to tell users to only fill out a category on certain conditions. Can anyone tell me how I could do this? I'm guessing I could use hook_form_alter(), but I don't know where to start.

A: 

You want to create your own module and implement hook_form_alter like you mentioned.

In a nutshell:

  • Use print_r($form) in hook_form_alter to look through what you'll need to edit
  • A category will have a #type => 'fieldset' and #title => 'What you named your category'
  • Remove print_r and add $form['categoryname']['#description'] = 'My description here!';

You may have to update your module's "weight" as I described here (replacing CCK with Profile).

Chris Ridenour
Hi Chris, Many thanks, that worked!
Ben
A: 

As Chris Ridenour alluded to, you can do this with hook_form_alter() in a custom module:

function mymodule_form_alter(&$form, &$form_state, $form_id) {
  if ($form_id === 'user_profile_form') {
    // Change personal to the name of the category.
    $form['personal']['#description'] = t('This is a description of your personal information.');
  }
}

In this example, it adds a description to the personal category on the user profile form.

You can read more about what types of things you can modify in the Forms API reference. If you have the Devel module installed, dsm($form) within your hook_form_alter() will pretty-print the form structure to give you an idea of what's available to alter.

Mark Trapp
Hi Mark, many thanks for your explanation. It was very helpful and I have solved my problem.
Ben