Problem using form_alter...

cameronp - May 3, 2007 - 11:49

Hi There, Im just trying to use the form_alter hook, but not having much luck.
What Im trying to do is add a new option in the user settings page, to allow a user agreement to be specified...

At this stage, I have this code, but nothing will come up within the user settings admin page...

<?php
/**
* Add the legal agreement in the user settings pages, for admin purposes.
*/
function gcs_form_alter($form_id, &$form)
{
  if (
$form_id == 'user_admin_settings')
  {
   
$form['registration']['user_agreement'] = array(
     
'#type' => 'textarea',
     
'#title' => t('Legal Agreement'),
     
'#default_value' => t('testing'),
     
'#description' => t("This gives the user the option of agreening to this statement.")
    );
  }
}
?>

Hmm...that code looks like

Island Usurper - May 3, 2007 - 12:26

Hmm...that code looks like it should work. Make sure that the module that's contained in has been enabled. Next on the list is to check the form_id, make sure it's correct.

Other than that, it might be that the theme function for that form isn't rendering any parts of the form it didn't define. This would be a bug or require a very good explanation of why that is.

-----
Übercart -- One cart to rule them all.

I agree that it looks good -

aggaire - May 3, 2007 - 14:58

I agree that it looks good - if this is most of what you're doing with this module, you may want to take a look at http://drupal.org/project/legal.

______________________
Grand Junction Design, LLC
Web and Print Design For
Nonprofits and Foundations

Don't forget to return $form at the end

usonian - July 18, 2007 - 17:14

I was having this same exact problem with user_admin_settings and form_alter(), and was tearing my hair out until I realized that I hadn't added

return $form;

to the end of my form_alter() hook. As soon as I did that it worked as expected.

I suppose that's worth

Island Usurper - August 1, 2007 - 13:53

I suppose that's worth trying, but you really shouldn't have to. The $form array is passed by reference, so any changes made to it by this function are passed to the rest of FAPI.

-----
Übercart -- One cart to rule them all.

You are, of course, correct

usonian - August 29, 2007 - 20:25

Thank you for pointing that out - I swear I tried adding the return statement after seeing it in another code snippet, and adding it did seem to work, but it doesn't seem right and perhaps I unknowingly fixed my code somewhere else.

-A

form_id

jbomb - January 3, 2008 - 16:16

So... "user_admin_settings" is the proper form_id for the form at :

http://www.example.com/?q=user/[uid]/edit

answered my own question...

jbomb - January 3, 2008 - 16:47

You can modify the form at:

http://www.example.com/?q=user/[uid]/edit

using the following code:

<?php
function notify_user_form_alter($form_id, &$form) {
    if (
$form_id == 'user_edit') {
       
// do something....
       
}
}
?>

Going nuts with this one...

peashooter - April 15, 2008 - 07:40

Hi all,

Not sure if any of you can help, but I'm having a similar problem with form_alter. This doesn't seem to add the variable to the $form array, and I have no idea why. Any ideas much, much, appreciated:

<?php
function procon_form_alter($form_id, $form) {
  switch (
$form_id) {
    case
'comment_form':

     
$form['procon'] = array(
        
'#type' => 'radios',
        
'#weight' => '10',
        
'#title' => t('ProConAnti'),
        
'#options' => array(
            
t('Pro Argument'),
            
t('Con Argument'),
            
t('Neutral Comment')
          ),
       );
      return
$form;
      break;
  }
}
?>

It should be <?php 

Island Usurper - April 18, 2008 - 15:40

It should be

<?php
 
function procon_form_alter($form_id, &$form) {
?>

The & makes the $form pass by reference, so you don't have to return it. Whatever changes are done are passed back to the calling function.

And in general, you don't need "return" and "break" in the same place.

-----
Übercart -- One cart to rule them all.

Thanks so much mate, that

peashooter - April 19, 2008 - 07:39

Thanks so much mate, that really helped me out. J

 
 

Drupal is a registered trademark of Dries Buytaert.