You are here

function fb_user_form_alter in Drupal for Facebook 7.3

Same name and namespace in other branches
  1. 5.2 fb_user.module \fb_user_form_alter()
  2. 5 fb_user.module \fb_user_form_alter()
  3. 6.3 fb_user.module \fb_user_form_alter()
  4. 6.2 fb_user.module \fb_user_form_alter()

Implements hook_form_alter().

File

./fb_user.module, line 567
This module manages relations between local Drupal user accounts and their accounts on facebook.com.

Code

function fb_user_form_alter(&$form, &$form_state, $form_id) {
  if (isset($form['fb_app_data'])) {

    // Add our settings to the fb_app edit form.
    module_load_include('inc', 'fb_user', 'fb_user.admin');
    fb_user_admin_form_alter($form, $form_state, $form_id);
  }
  elseif ($form_id == 'user_edit' && ($app = $form['#fb_app'])) {

    // Disable buttons on user/edit/app pages, nothing to submit
    unset($form['submit']);
    unset($form['delete']);
  }

  // Add name and email to some forms.
  if (isset($GLOBALS['_fb'])) {
    $fb = $GLOBALS['_fb'];
    if (!$GLOBALS['user']->uid && ($form_id == 'user_register_form' && variable_get(FB_USER_VAR_ALTER_REGISTER, TRUE) || $form_id == 'user_login' && variable_get(FB_USER_VAR_ALTER_LOGIN, TRUE) || $form_id == 'user_login_block' && variable_get(FB_USER_VAR_ALTER_LOGIN_BLOCK, TRUE))) {

      // Add a facebook connect button or profile pic to the form.
      $fb_button = theme('fb_login_button', array(
        'text' => t(_fb_user_button_text($form_id)),
      ), array(
        'form_id' => $form_id,
      ));
      $form['fb_user'] = array(
        '#fb_user' => NULL,
        'connected' => array(
          '#prefix' => '<div class="fb_connected">',
          '#suffix' => '</div>',
          'picture' => array(
            '#markup' => '<fb:profile-pic uid="loggedinuser" linked="false" facebook-logo="true"></fb:profile-pic>',
            '#type' => 'markup',
            '#prefix' => '<div class="fb_user_picture">',
            '#suffix' => '</div>',
          ),
          'text' => array(
            '#type' => 'markup',
            '#markup' => t('!name, after <a href="!login_url">login</a> or <a href="!register_url">registration</a> your account will be connected.', array(
              '!name' => '<fb:name uid="loggedinuser" useyou="false" linked="false"></fb:name>',
              '!login_url' => url('user/login'),
              '!register_url' => url('user/register'),
            )),
          ),
        ),
        'loginbutton' => array(
          '#markup' => $fb_button,
          '#type' => 'markup',
          '#prefix' => '<div class="fb_user-login-button-wrapper fb_not_connected">',
          '#suffix' => '</div>',
        ),
        '#weight' => -1,
      );

      // @TODO confirm fb session before sharing sensitive data!
      if ($fbu = fb_facebook_user($fb)) {

        // Include $fb_user in form, so other modules can custom form_alter.
        $data = _fb_user_facebook_data($fb);
        $form['fb_user']['#fb_user'] = $data;
        if ($form_id == 'user_register_form') {
          if ($data) {

            // Provide defaults for name and email.
            if (isset($form['name']) && !$form['name']['#default_value']) {

              // @TODO - ensure name is unique to Drupal.
              $form['name']['#default_value'] = $data['name'];
            }
            elseif (isset($form['account']) && isset($form['account']['name']) && !$form['account']['name']['#default_value']) {

              // @TODO - ensure name is unique to Drupal.
              $form['account']['name']['#default_value'] = $data['name'];
            }
            if (isset($form['mail']) && !$form['mail']['#default_value']) {
              $form['mail']['#default_value'] = $data['email'];
            }
            elseif (isset($form['account']['mail']) && isset($form['account']['mail']) && !$form['account']['mail']['#default_value']) {
              $form['account']['mail']['#default_value'] = $data['email'];
            }
          }
        }
      }
    }
    elseif ($form_id == 'contact_site_form' && variable_get(FB_USER_VAR_ALTER_CONTACT, TRUE)) {
      if ($data = _fb_user_facebook_data($fb)) {
        if (!$form['name']['#default_value'] || strpos($form['name']['#default_value'], '@facebook')) {
          $form['name']['#default_value'] = $data['name'];
        }
        if (!$form['mail']['#default_value']) {
          $form['mail']['#default_value'] = $data['email'];
        }
      }
    }
  }
}