I created some new comment fields ('Institutional Affiliation', for example) and would like to pre-populate the values for these based on user/account fields. I started down a path to use PHP to define the default value (via CCK module), but found that PHP code for default values is not available for text fields (although it is available for lists). I'm now trying to use hook_form_alter in template.php to pre-populate these fields, but am a PHP newbie and haven't been able to find a solution documented that I can use successfully.

I followed a very helpful guide to start using hook_form_alter (http://www.digett.com/blog/05/26/2011/how-theme-comment-form-drupal-7), but don't know how to correctly assign default values / pre-populate the values for my custom comment fields.

In case it is unclear, the desired end user experience is: the comment form contains the typical text area for the comment body as well as some additional fields, like Institutional Affiliation. The Institutional Affiliation field is pre-populated with a value from the user's account, 'Harvard University', for instance. The user can submit the comment with the pre-populated value, or can edit it before submitting (changing 'Harvard University' to 'UCLA', for instance).

For now, I'd be happy if I can just pre-populate the comment field with the correct value from the user's account. (In the future, I'd also like to update the user's account field if they edit the value in the comment form.)

Thanks kindly for any advice or direction.

Comments

WorldFallz’s picture

can you post the code you're working on?

Ank.g9’s picture

hii!!

1. Click on Structure tab and click on Content types.

2. Click edit on desired content type row in which you want to add new comment field.

3. Now click on COMMENT FIELDS.

4. Add your desired filed.

5. Create a module and by using hook_form_alter() add default value in your particular element if form id equal to your desired form id.
for example :
$form['your_element_field_name']['und'][0]['value']['#default_value']=$GLOBALS['user']->name;

6. Add a new submit function.
for example:
$form['#submit'][] = "change_user_data";

7. in submit function check your element value by using $form and $form_state array.

8. run db_update() query.

labs’s picture

Ankit -- thanks very much. I'm going to start down the path to make the module as you've outlined, but already I've achieved my first goal: simply pre-populating text fields with a value from the user account.

For anyone else trying to do this, Ankit's code can also be used the template.php file. I followed these instructions to modify a form in Drupal 7 (http://www.digett.com/blog/05/26/2011/how-theme-comment-form-drupal-7), then simply inserted Ankit's code to set the default value via hook_form_alter. My code is specifically targeting the comments form in a particular content type. Thus (for newbies like myself), I've added the following to my template.php file:

function YOURTHEME_form_comment_node_YOURCONTENTTYPE_form_alter(&$form, &$form_state) {
$form['your_element_field_name']['und'][0]['value']['#default_value']=$GLOBALS['user']->name;
  }

Hope to return or pay forward your good help.

labs

labs’s picture

...and to get access to custom user fields I called the user global and then loaded them with another line. So working code for this is now:

function YOURTHEME_form_comment_node_YOURCONTENTTYPE_form_alter(&$form, &$form_state) {
global $user;
$user_fields = user_load($user->uid); //load the current users custom fields
$form['your_element_field_name']['und'][0]['value']['#default_value']=$user_fields->your_user_custom_field_name['und']['0']['value'];
 }
Ank.g9’s picture

write this hook (hook_form_alter) in .module file not in template.php file.

Here are some confusion :
['your_element_field_name'] is just an example not a machine name of new field element.
i write just an example code don't copy & past that in your .module file.

just follow the logic.

Thank You