Contributions API

Calling all Drupal developers!

Help us get this on the first page of Digg. DIGG NOW!

Modules in 6

avatar_selection_form_user_register_alter

Definition

avatar_selection_form_user_register_alter(&$form, $form_state, $form_id="user_register")
contributions/avatar_selection/avatar_selection.module, line 142

Description

Implementation of hook_form_alter().

Create the form structure for adding an avatar in the user registration page.

Parameters

&$form General reference used in drupal, defining the structure & the fields of a form.

$form_state General variable, used to control the processing of the form.

$form_id The default is "user_register"; hold the page where the function is being used.

Return value

Return the structure of the form.

Code

<?php
function avatar_selection_form_user_register_alter(&$form, $form_state, $form_id="user_register") {

  // If user pictures aren't enabled, nothing to do here.
  if (!variable_get('user_pictures', 0)) {
    return;
  }

  $anon_user = drupal_anonymous_user();

  // See if user has access to avatars.
  $disable_upload = variable_get('avatar_selection_disable_user_upload', 0);
  if (!user_access('access avatars')) {
    // If uploads also disabled, remove the field altogether.
    if ($disable_upload) {
      unset($form['picture']);
    }
    return;
  }

  $force_choose = variable_get('avatar_selection_force_user_avatar_reg', 0);
  $selects = _avatar_selection_image_list($anon_user);
  if (count($selects['avatars'])) {
    drupal_add_css(drupal_get_path('module', 'avatar_selection') .'/avatar_selection.css');
    $form['picture'] = array(
      '#type' => 'fieldset',
      '#title' => t('Picture'),
      '#weight' => 1,
    );
    $form['picture']['select_avatar'] = array(
      '#type' => 'radios',
      '#title' => t('Select an avatar'),
      '#options' => $selects['avatars'],
      '#required' => $force_choose ? TRUE : FALSE,
      '#attributes' => array('class' => 'user_avatar_select'),
    );
    $form['#validate'][] = 'avatar_selection_validate_user_avatar';
  }
  else {
    $form['#validate'][] = 'avatar_selection_validate_user_avatar';
  }
  $images_per_page = variable_get('avatar_selection_avatar_per_page', 30);
  $js_settings = array(
    'num_images_per_page' => ($images_per_page ? $images_per_page : 1),
    'num_images' => $selects['total'],
    'image_url' => file_create_url(file_create_path('avatar_selection')),
    'images' => $selects['avatars_indexed'],
  );
  drupal_add_js(array('avatar_selection' => $js_settings), 'setting');
  drupal_add_js(drupal_get_path('module', 'avatar_selection') .'/js/avatar_selection.js', 'module', 'header');

  return $form;
}
?>