You are here

function realname_form_alter in Real Name 6

Same name and namespace in other branches
  1. 5 realname.module \realname_form_alter()

Implements hook_form_alter(). Intercepts the contact forms to show the realname.

File

./realname.module, line 527

Code

function realname_form_alter(&$form, $form_state, $form_id) {
  global $user;
  if (!user_access('use realname')) {
    return;
  }
  $bypass_forms = variable_get('realname_bypass_forms', array(
    array(
      'name' => 'comment_form',
      'fields' => array(
        'name',
      ),
    ),
  ));
  foreach ($bypass_forms as $bypass) {

    // Is it a form the admin wants bypassed?
    if ($form_id == $bypass['name']) {
      if (!isset($user->realname)) {
        $user->realname = realname_make_name($user);
      }
      $value = $user->uid ? $user->realname_save : variable_get('anonymous', 'Anonymous');
      realname_array_set_nested_value($form, array_merge($bypass['fields'], array(
        '#default_value',
      )), $value);
    }
  }
  switch ($form_id) {
    case 'user_profile_form':
      if (variable_get('realname_theme', TRUE)) {
        $form['account']['name']['#default_value'] = $form['_account']['#value']->realname_save;
      }
      break;
    case 'contact_mail_user':
      if (!isset($user->realname)) {
        $user->realname = realname_make_name($user);
      }
      $form['from']['#value'] = check_plain($user->realname) . ' <' . check_plain($user->mail) . '>';
      if (!isset($form['recipient']['#value']->realname)) {
        $form['recipient']['#value']->realname = realname_make_name($form['recipient']['#value']);
      }
      $form['to']['#value'] = check_plain($form['recipient']['#value']->realname);
      break;
    case 'contact_mail_page':
      if (!isset($user->realname)) {
        $user->realname = realname_make_name($user);
      }
      $form['name']['#default_value'] = $user->uid ? $user->realname : '';
      break;
    case 'user_admin_role':
      $rid = $form['rid']['#value'];
      $pic = variable_get('realname_user_level_' . $rid, NULL);
      $form['level'] = array(
        '#type' => 'textfield',
        '#default_value' => $pic,
        '#title' => t('User level image'),
        '#description' => t('This image will be shown when the user name is themed.
          It can be used to show special attributes or user participation level.
          The path is relative to the files directory "%files".', array(
          '%files' => file_directory_path(),
        )),
        '#field_suffix' => empty($pic) ? '' : '&nbsp;&nbsp;<img src="' . file_create_url($pic) . '" />',
      );
      $form['submit']['#weight'] = 98;
      $form['delete']['#weight'] = 99;
      $form['#submit'][] = '_realname_role_submit';
      break;
    case 'privatemsg_new':
      $form['privatemsg']['recipient']['#autocomplete_path'] = 'realname/privatemsg/autocomplete';
      break;
  }
}