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 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.

Rails options_for_select with include blank and selected option

If you want to use a select_tag and include a blank option it can be achieved using the following snippet:

<%= select_tag “district”, options_for_select(Customer.all.collect { |k| ["#{k.district} #{k.store}", k.id] }.insert(0, ["", 0]), params[:number].to_i) %>

- it also handles setting the selected option if one is passed – in this case in the query string. I have been blogging about this previously but noticed that I had forgotten to show how to pass the chosen option if one exists.

mysql change collation to utf8

If your encoding is off and you didn’t get it right from the beginning changing it is not a difficult task to do – but doing it in production could be risky.

To check you database collation in MySQL you can use the following SQL statement:

SHOW TABLE STATUS;

This will show you a lot of useful info about your database including collation. If you want to check it for a specific table use the following SQL statement:

SHOW TABLE STATUS WHERE NAME = 'The Name of Your Table';

This will show you the same info as above but only for a specific table. And if you want to change you collation to utf8 use the following SQL statement:

ALTER TABLE The Name of Your Table CONVERT TO CHARACTER SET utf8;

drupal 7 theme tutorial

I’m helping a friend of mine whip a Drupal 7 site together, and part of the work is to create a responsive HTML5 based design for the site.

Building a Drupal 7 theme from the grounds up isn’t difficult, all you have to do is follow these simple steps and then your ready to rock n roll.

1) create a folder for your theme in sites/all/themes/ in my case the theme will be called “badning”, so Ill create the folder sites/all/themes/badning

2) place a name-of-your-theme.info file in sites/all/themes/name-of-your-theme, in my case it will be badning.info

Add the following to your name-of-your-theme.info file:

  1 name = Badning theme
  2 description = Badning.dk responsive theme
  3 core = 7.x
  4 engine = phptemplate
  5 regions[header] = Header
  6 regions[content] = Content
  7 regions[footer] = Footer
  8 stylesheets[all][] = badning.css
  9 scripts[] = badning.js

I have only added the bare minimum of regions that I will use.  Now your ready to building your actual theme.

PHP pass by reference to call_user_func_array

You need to make a small change in the call_user_func_array if you want to pass the user_func an argument that is a reference. I’m using this in my own small PHP micro framework when I call the “view”. Here is a small snippet:

call_user_func_array('before_all', array($func, &$params));

as you can see, the $params is now a reference within an array and putting it into an array makes the whole difference. I found this solution over at stackoverflow.com - MAN I LOVE THAT SITE!

The user_func which in this case is called “before_all” looks like this:

function before_all($func, &$args)
{
}

In my PHP micro framework it takes the current function and the arguments passed to that function as it’s arguments, and the second argument is passed as a reference.

Follow

Get every new post delivered to your Inbox.