You are here

function avatar_selection_form_user_register_alter in Avatar Selection 6

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.

File

./avatar_selection.module, line 148
The Avatar Selection module allows the user to pick an avatar image from a list already loaded by an administrative user, and to the administrator to disable uploading other avatar files by the user.

Code

function avatar_selection_form_user_register_alter(&$form, $form_state, $form_id = 'user_register') {

  // We need the pager global variables.
  global $_GET;

  // 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) && !user_access('upload avatar in profile');
  if (!user_access('access avatars')) {

    // If uploads also disabled, remove the field altogether.
    if ($disable_upload) {
      unset($form['picture']);
    }

    // To allow random avatars to be assigned on registration.
    $form['#validate'][] = 'avatar_selection_validate_user_avatar';
    return $form;
  }

  // We find out the current page number.
  $page = 0;
  if (isset($_GET['page']) && is_numeric($_GET['page'])) {
    $page = $_GET['page'];
  }
  $force_choose = variable_get('avatar_selection_force_user_avatar_reg', 0);
  $avatars_per_page = variable_get('avatar_selection_avatar_per_page', 30);
  $selects = _avatar_selection_image_list($anon_user, '', 0, $page * $avatars_per_page, $avatars_per_page);
  if (count($selects['avatars'])) {
    drupal_add_css(drupal_get_path('module', 'avatar_selection') . '/avatar_selection.css');
    $upload = 1;
    if (empty($form['picture']) || !is_array($form['picture'])) {
      $upload = 0;

      // I.e. Not provided by 'register with picture' contributed module.
      $form['picture'] = array(
        '#type' => 'fieldset',
        '#title' => t('Picture'),
        '#weight' => 1,
      );
    }
    drupal_add_js(drupal_get_path('module', 'avatar_selection') . '/js/avatar_selection_pager.js', 'module', 'header');
    $form['picture']['select_avatar'] = array(
      '#type' => 'radios',
      '#title' => $upload == 0 ? t('Select an avatar') : t('Or simply select an icon'),
      '#description' => $upload ? '' : t('Your virtual face or picture.'),
      '#options' => $selects['avatars'],
      '#required' => $force_choose ? TRUE : FALSE,
      '#attributes' => array(
        'class' => 'user-avatar-select',
      ),
      '#prefix' => '<div id="avatar-selection-loading"></div>',
      '#suffix' => theme('avatar_selection_pager', 'form#user-register', 'div.user-avatar-select', $selects['total'], $avatars_per_page),
    );
    $form['#validate'][] = 'avatar_selection_validate_user_avatar';
  }
  else {
    $form['#validate'][] = 'avatar_selection_validate_user_avatar';
  }
  $js_file = drupal_get_path('module', 'avatar_selection') . '/js/avatar_selection.js';
  drupal_add_js($js_file, 'module', 'header');
  return $form;
}