You are here

function avatar_selection_form_alter in Avatar Selection 5

Same name and namespace in other branches
  1. 5.2 avatar_selection.module \avatar_selection_form_alter()

Implementation of hook_form_alter().

File

./avatar_selection.module, line 97

Code

function avatar_selection_form_alter($form_id, &$form) {

  // we need the pager global variables
  global $user;

  // If user pictures aren't enabled, nothing to do here.
  if (!variable_get('user_pictures', 0)) {
    return;
  }
  if ('user_edit' == $form_id && is_array($form['picture'])) {
    drupal_add_css(drupal_get_path('module', 'avatar_selection') . '/avatar_selection.css');
    $access = user_access('access avatars');
    $disable_upload = variable_get('avatar_selection_disable_user_upload', 0);
    $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']);
    }

    // If user has access to choose an avatar, show selection options
    $selects = _avatar_selection_image_list($user);
    if ($access && count($selects['avatars'])) {
      $user_form = drupal_retrieve_form($form_id);
      $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' => $user_form['_account']['#value']->picture,
        '#required' => $force_choose ? TRUE : FALSE,
        '#attributes' => array(
          'class' => 'user_avatar_select',
        ),
      );
    }

    // If user can't do either, remove the fielset altogether
    if (!$access && $disable_upload) {
      unset($form['picture']);
    }

    // Don't allow user to delete a selected avatar
    $path = '/avatar_selection/';
    if ($form['picture']['current_picture']['#value'] && preg_match($path, $form['picture']['current_picture']['#value'])) {
      unset($form['picture']['picture_delete']);
    }
    $num_images_per_page = variable_get('avatar_selection_avatar_per_page', 30);
    $js_settings = array(
      'num_images_per_page' => $num_images_per_page ? $num_images_per_page : 1,
      'num_images' => $selects['total'],
    );
    drupal_add_js(array(
      'avatar_selection' => $js_settings,
    ), 'setting');
    drupal_add_js(drupal_get_path('module', 'avatar_selection') . '/js/avatar_selection.js', 'module', 'header');
  }
  else {
    if ('user_register' == $form_id) {
      $anon_user = drupal_anonymous_user();
      $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',
          ),
        );
      }
      $num_images_per_page = variable_get('avatar_selection_avatar_per_page', 30);
      $js_settings = array(
        'num_images_per_page' => $num_images_per_page ? $num_images_per_page : 1,
        'num_images' => $selects['total'],
      );
      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;
}