You are here

function realname_form_alter in Real Name 5

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

Implementation of hook_form_alter(). Intercepts the contact forms to show the realname.

File

./realname.module, line 178
The RealName module allows the admin to choose fields from the user profile that will be used to add a "realname" element (method) to a user object. Hook_user is used to automatically add this to any user object that is loaded.

Code

function realname_form_alter($form_id, &$form) {
  global $user;
  if (!user_access('use realname')) {
    return;
  }

  //  drupal_set_message($form_id);
  if (substr($form_id, -10) == '_node_form') {

    //    drupal_set_message(print_r($form, TRUE));
    foreach ($form as $name => $elements) {
      if (substr($name, 0, 6) == 'field_') {
        if (isset($form[$name]['uids'])) {
          $fields = content_fields();
          $field = $fields[$name];
          $options = _userreference_potential_references($field);
          foreach ($options as $uid => $optname) {
            $account = user_load(array(
              'uid' => $uid,
            ));
            $options[$uid] = realname_make_name($account);
          }
          asort($options);
          if (!$field['required']) {
            $options = array(
              'none' => t('<none>'),
            ) + $options;
          }
          $form[$name]['uids']['#options'] = $options;
        }
        else {
          foreach ($elements as $key => $value) {
            if (is_numeric($key)) {
              foreach ($value as $key1 => $value1) {
                if (isset($form[$name][$key]['user_name']['#autocomplete_path'])) {
                  $path = 'realname/userref/autocomplete/' . $name;
                  $form[$name][$key]['user_name']['#autocomplete_path'] = $path;
                }
              }
            }
          }
        }
      }
    }
  }
  $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']) {
      $field_name = '$form[\'' . implode("']['", $bypass['fields']) . "']['#default_value']";
      if (!isset($user->realname)) {
        $user = user_load(array(
          'uid' => $user->uid,
        ));
      }
      $value = $user->uid ? $user->realname_save : variable_get('anonymous', 'Anonymous');
      eval($field_name . ' = $value;');
    }
  }

  // We will go ahead and fall through in case there is any other special processing.
  switch ($form_id) {

    //    case 'comment_form':
    //      if (!isset($user->realname)) {
    //        $user = user_load(array('uid' => $user->uid));
    //      }
    //      $form['author']['#default_value'] = $user->realname_save;
    //      break;
    case 'privatemsg_new_form':

      //      drupal_set_message(print_r($form['header']['recipient']['#autocomplete_path'], true));
      $form['header']['recipient']['#autocomplete_path'] = 'realname/privatemsg/autocomplete';
      break;
    case 'user_edit':
    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 = user_load(array(
          'uid' => $user->uid,
        ));
      }
      $form['from']['#value'] = check_plain($user->realname) . ' &lt;' . check_plain($user->mail) . '&gt;';
      break;
    case 'contact_mail_page':
      if (!isset($user->realname)) {
        $user = user_load(array(
          'uid' => $user->uid,
        ));
      }
      $form['name']['#default_value'] = $user->uid ? $user->realname : '';
      break;
  }
}