function avatar_selection_form_alter in Avatar Selection 5.2
Same name and namespace in other branches
- 5 avatar_selection.module \avatar_selection_form_alter()
Implementation of hook_form_alter().
Create the form structure for adding an avatar on the user registration and user profile forms.
Parameters
$form_id: The id of the form to modify.
&$form: General reference used in drupal, defining the structure & the fields of a form.
File
- ./
avatar_selection.module, line 125 - 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_alter($form_id, &$form) {
global $user, $_GET;
// If user pictures aren't enabled, nothing to do here.
if (!variable_get('user_pictures', 0)) {
return;
}
// 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']);
}
return;
}
// We find out the current page number.
$page = 0;
if (isset($_GET['page']) && is_numeric($_GET['page'])) {
$page = $_GET['page'];
}
$avatars_per_page = variable_get('avatar_selection_avatar_per_page', 30);
$js_file = drupal_get_path('module', 'avatar_selection') . '/js/avatar_selection.js';
if ('user_edit' == $form_id && 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.
$selects = _avatar_selection_image_list($user, "", 0, $page * $avatars_per_page, $avatars_per_page);
if (count($selects['avatars'])) {
$user_form = drupal_retrieve_form($form_id);
$current_avatar = basename($user_form['_account']['#value']->picture);
drupal_add_js(drupal_get_path('module', 'avatar_selection') . '/js/avatar_selection_pager.js', 'module', 'header');
$form['picture']['select_avatar'] = array(
'#type' => 'radios',
'#title' => $disable_upload ? t('Select an avatar') : t('Or simply select an icon'),
'#description' => $disable_upload ? t('Your virtual face or picture.') : '',
'#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',
),
'#prefix' => '<div id="avatar-selection-loading"></div>',
'#suffix' => theme('avatar_selection_pager', 'form#user-edit', 'div.user-avatar-select', $selects['total'], $avatars_per_page, '/' . $js_file),
);
}
// 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']);
}
drupal_add_js($js_file, 'module', 'header');
}
elseif ('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, "", 0, $page * $avatars_per_page, $avatars_per_page);
if (count($selects['avatars'])) {
drupal_add_css(drupal_get_path('module', 'avatar_selection') . '/avatar_selection.css');
drupal_add_js(drupal_get_path('module', 'avatar_selection') . '/js/avatar_selection_pager.js', 'module', 'header');
$upload = 1;
if (!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,
);
}
$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, '/' . $js_file),
);
}
drupal_add_js($js_file, 'module', 'header');
}
return $form;
}