My New Friend: hook_form_alter

Events happening in the community are now at Drupal community events on www.drupal.org.
Posted by Summitt Dweller on February 25, 2009 at 3:10pm

Michael put me up to this... and rightfully so. He said that hook_form_alter would be my friend and it is. But it's not my best friend, yet.

Long story short... I have an assistant who's ready to bless my new site with buckets of content. Specifically, user profiles. Problem: I didn't feel comfortable turning her loose on the data using the default user profile form because it carried a lot of stuff that could only make trouble for us (like menu settings, body input format selector, revision settings, etc.).

So I took the advice handed out at Monday's user group meeting and had a look at hook_form_alter. It works! I did struggle a little with finding the ID of my target forms and wasn't sure exactly what to name my function either, but before long I got both figured out. If you are interested in doing the same check out http://www.lullabot.com/articles/modifying-forms-5-and-6 for guidance.

In the end I modified my user.module adding a function that reads like this:

function user_form_alter(&$form, $form_state, $form_id) {
// Normally a switch is used because you may want to alter more than
// one form and it is easy to add a new case for each form.
switch ($form_id)
{
// This is our form ID.
case 'user_profile_form':
// Print out the $form array to see all the elements we are working with.
// $output .= dump($form);
// Once I know which part of the array I'm after we can change it.

// Change the Username field title to Login ID.
$form['account']['name']['#title'] = t('Login ID');

// Hide some things...
unset( $form['account']['pass'] ); // password fields
unset( $form['account']['status'] ); // status
unset( $form['account']['roles'] ); // roles
unset( $form['theme_select'] ); // themes
unset( $form['block'] ); // blocks
break;

Now that I have this working there's just one more form element that I'd like to hide. I've enabled the Print module (delivers printer, e-mail and PDF friendly output of your content) and now all my content forms show this collapsed input selection:

Printer, e-mail and PDF versions

Can't seem to get rid of this thing, or even figure out how to change its weight so I can move it out of the middle of my profile object data. There's a dump of my $form array attached to this post as a .txt file.

If anybody can figure out how to hide this form element using hook_form_alter I will be eternally grateful and declare you to be my new best friend...and hook_form_alter will be my new second best friend. 8^)

AttachmentSize
User Profile Form Array.txt 153.34 KB

Comments

Don't modify user.module!!!

Posted by Michael Hofmockel on February 25, 2009 at 4:20pm
  • Don't modify user.module!!!

If you take that same function and drop it in your own module then you would not have to fork Drupal core. Look at that Lullabot article again. Where they have "modulename_" you should substitute your module name. Create a new module called "mymodule". Add a new function called "mymodule_form_alter". Add an if statement to catch just the $formid you are interested in.

With this method you will maintain your upgrade pathway for Drupal core and you will be able to keep all your custom code in one place. You need to learn this method or you will eventually make a big mess out of your Drupal core modules.

  • "(like menu settings, body input format selector, revision settings, etc.)." You could hide all of this with proper permissions and avoid form_alter all together. Although I am happy you found form_alter.

  • You can turn off the print feature on specific pages by exclusion. See /admin/settings/print You looking for link visibility.

** In the future try to ask one question per post. Three questions on three posts is easier to handle then a brain dump. On the upside keep posting! Keep asking questions!

Regards,
Michael Hofmockel
iMed Studios

Open Source || Open Access || Open Mind

Regards,
Michael Hofmockel

Open Source || Open Access || Open Mind

Great Advice... and NOT as Painful as Imagined

Posted by Summitt Dweller on February 25, 2009 at 5:56pm

Thanks Michael.

I've been making small mods to core with my own revision control in place (all the mods are stored in a project with rev tracking enforced). Intent was to make it work, then go back and "make it right". I honestly didn't understand how modules played together but now that I've read chapter 2 in the book (thanks for that too!) it makes sense.

Per your suggestion, I created stc.module (for my South Tama County project) to hold the few custom mods that I have created thus far. Took all of 15 minutes to make it happen and it didn't even hurt.

Sorry for prompting a brain dump, but honestly I'll take all of those I can get given my extensive (5 weeks, real time=5 days?) Drupal experience. 8^)

Mark

Mark

Central Iowa Drupal Users Group

Group categories

Category

Group notifications

This group offers an RSS feed. Or subscribe to these personalized, sitewide feeds:

AltStyle によって変換されたページ (->オリジナル) /