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_profile_form_alter

Definition

avatar_selection_form_user_profile_form_alter(&$form, $form_state, $form_id="user_profile_form")
contributions/avatar_selection/avatar_selection.module, line 211

Description

Implementation of hook_form_alter().

Create the form structure for adding an avatar in the user profile 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_profile_form"; holds the form name.

Return value

Return the structure of the form.

Code

<?php
function avatar_selection_form_user_profile_form_alter(&$form, $form_state, $form_id="user_profile_form") {

  // If user pictures aren't enabled, nothing to do here.
  if (!variable_get('user_pictures', 0)) {
    return;
  }
  $user = user_load(array("uid" => $form['#uid']));

  // 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;
  }

  if (is_array($form['picture'])) {
    drupal_add_css(drupal_get_path('module', 'avatar_selection') .'/avatar_selection.css');
    $force_choose = variable_get('avatar_selection_force_user_avatar', 0);

    // If upload support has been disabled, remove the ability to upload and
    // delete pictures.
    if ($disable_upload) {
      unset($form['picture']['picture_delete']);
      unset($form['picture']['picture_upload']);
    }
    else {
      $force_choose = 0;
    }

    // Show selection options.
    if (!isset($user->og_groups)) $user->og_groups = '';
    $selects = _avatar_selection_image_list($user);

    if (count($selects['avatars'])) {
      $current_avatar = basename($user->picture);
      $form['picture']['select_avatar'] = array(
        '#type' => 'radios',
        '#title' => $disable_upload ? t('Select an avatar') : t('Or simply select an icon'),
        '#options' => $selects['avatars'],
        '#default_value' => array_key_exists($current_avatar, $selects['avatars']) ? $current_avatar : '',
        '#required' => $force_choose ? TRUE : FALSE,
        '#attributes' => array('class' => 'user_avatar_select'),
      );
      if (array_key_exists($current_avatar, $selects['avatars'])) {
        $form['_account']['#value']->picture = null;
      }
      $form['#validate'][] = 'avatar_selection_validate_user_avatar';
    }
    else {
      $form['#validate'][] = 'avatar_selection_validate_user_avatar';
    }

    // Don't allow user to delete a selected avatar.
    $path = '/avatar_selection/';
    if (!empty($form['picture']['current_picture']['#value'])
      && preg_match($path, $form['picture']['current_picture']['#value'])) {
      unset($form['picture']['picture_delete']);
    }

    $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;
}
?>