Drupal 7 adding user profile picture at registration

It took me a while to get this working and since I’m sure others will be struggling with the same task I decided to share my code.

Here’s my approach, all stored in a module called “customers”

  1. Alter “user-register-form” found at http://www.your-domain.com/user/register
  2. Add custom submit handler to “user-register-form” form
  3. Upload user image in custom submit handler

it really is that simple :)

Using hook_form_FORM_ID_alter() to alter “user-register-form” making sure I only hook into that form.

Inside this function which in my case is called “customers_form_user_register_form_alter” I add a few attributes to the form so that it can handle file uploads:

$form['#attributes'] = array(
  'enctype' => 'multipart/form-data',
  'autocomplete' => 'off'
);

then add custom submit handler like so:

$form['#submit'][] = 'customers_register_handler';

and then the field that holds the image file:

$form['picture_upload'] = array(
  '#type' => 'file',
  '#title' => t('Company Logo')
);

in my case I use the user profile picture to store a company logo, but it’s just the title of the field. And now to where the magic happens; the custom submit handler:

$validators = array(
  'file_validate_is_image' => array(),
);
$file = file_save_upload('picture_upload', $validators);

if ($file == false) {
  form_set_error('picture_upload', t('Failed to upload picture'));
  $picture = null;
} elseif ($file != null) {
  $picture = $file;
  // Set the user id
  $picture->uid = $user->uid;
}

user_save($user, array(
  'picture_upload' => $picture,
));

One of the things that took me a bit of time to grasp was the fact that files are not passed inside the $form_state array, instead you call file_save_upload() where the 1. parameter is the name of the file field. In my case the file field is called picture_upload, and as you can see I’m currently not passing any file validation. The next “if statement” just takes care that a file was uploaded and sets the owner to the newly created user.

The only thing left is to call user_save() and pass in the file that was uploaded.

I know several modules exist that handle this for you, but adding a module just for that is an overkill in my opinion, especially since all my other logic related to “customers” is stored inside my customers.module.

Drupal 7 form altering

After having written 2 blog posts about how to alter drupal forms I decided to turn it into a miniseries where I will do my absolutely best in order to keep my posts short and to the point.

Drupal 7 form altering

Drupal’s hooks are great for altering existing forms, you can hack in and change the way a form works and behaves without ever touching the original form code.

hook_form_form_id_alter()

This hook will give you the ability to alter any form you see rendered on your drupal site, all you need is a standard drupal module and then simply follow these simple steps for accessing the desired form.

Lets say the name of your module is “topfunky” then in order to extend or modify the desired form, you would use the following syntax when naming your function: topfunky_form + “name of desired form” + _form(&$form, &$form_state, $form_id). If the form you want to extend is called “block-add-block-form” then name name of the function would be “topfunky_form_block_add_block_form_alter”.

Naming convention

The naming convention for this hook is “module name” + _form_ + “form id” + _alter(). And inside you can whisk up what ever you like and add your requirements to this form.

Back at hacking Drupal

this time it’s drupal 7!

I haven’t written a single line of drupal code in a year or so – imagine that – but I thought it could be funny hacking a bit of drupal again. Continue reading

Drupal 6 and hook_form_FORM_ID_alter

Using hook_form_FORM_ID_alter makes it possible to hook into any drupal 6 form before it renders, that way you can add fields or manipulate existing fields before the form is presented to the end user. Continue reading

Follow

Get every new post delivered to your Inbox.