You are here

function fb_user_form_user_profile_form_alter in Drupal for Facebook 7.3

Implements hook_form_user_profile_form_alter.

File

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

Code

function fb_user_form_user_profile_form_alter(&$form, &$form_state, $form_id) {
  global $user, $_fb_app;
  if (empty($_fb_app)) {

    // No application active.
    // @todo: should this alter loop through all known apps?
    return;
  }
  if (!user_access('administer users') && !(user_access('delete own fb_user authmap') && $user->uid == $form['#user']->uid)) {
    return;
  }

  // hide from this user
  if (empty($form['#user_category']) || $form['#user_category'] != 'account') {
    return;

    // Alter only the primary user account form.
  }
  $fb_user_data = _fb_user_get_config($_fb_app);
  $account = $form['#user'];
  $fbu = _fb_user_get_fbu($account->uid);
  if ($fbu) {

    // The drupal user is a facebook user.
    $form['fb_user'] = array(
      '#type' => 'fieldset',
      '#title' => t('Facebook Application ' . $_fb_app->title),
      '#collapsed' => false,
      '#collapsible' => false,
    );
    $form['fb_user']['fb_user_map'] = array(
      '#type' => 'checkbox',
      '#title' => t('Connect to facebook.com'),
      '#default_value' => $fbu,
      '#return_value' => $fbu,
      '#description' => '',
    );

    // Now, learn more from facebook.
    try {
      $data = fb_api($fbu, array(
        'access_token' => fb_get_token(),
      ));
      if (count($data)) {
        if (empty($data['link']) && !empty($data['id'])) {
          $data['link'] = 'https://www.facebook.com/profile.php?id=' . $data['id'];
        }
        $form['fb_user']['fb_user_map']['#description'] .= t('Local account !username corresponds to !profile_page on Facebook.com.', array(
          '!username' => l($account->name, 'user/' . $account->uid),
          '!profile_page' => l($data['name'], $data['link']),
        ));
      }
    } catch (Exception $e) {
      fb_log_exception($e, t('Failed to get user data from facebook.'));
    }
    if (fb_facebook_user() == $fbu) {

      // The user is currently connected to facebook.  Depending on
      // config, they may not be able to break the connection.
      $form['fb_user']['fb_user_map']['#disabled'] = TRUE;
      $form['fb_user']['fb_user_map']['#description'] .= '<br/>' . t('(Checkbox disabled because you are currently connected to facebook.)');
    }
    else {
      $form['fb_user']['fb_user_map']['#description'] .= '<br/>' . t('Uncheck then click save to delete this connection.');
    }
  }
  if (!$fbu) {

    // this tells us that a mapping hasn't been created
    if ($user->uid == $account->uid) {

      // Could not obtain the $fbu from an existing map.
      $fbu = fb_facebook_user();
      if ($fbu) {

        // they are connected to facebook; give option to map
        $form['fb_user'] = array(
          '#type' => 'fieldset',
          '#title' => t('Facebook Application ' . $_fb_app->title),
          '#collapsed' => false,
          '#collapsible' => false,
        );
        $form['fb_user']['fb_user_map'] = array(
          '#type' => 'checkbox',
          '#title' => t('Connect account to facebook.com'),
          '#default_value' => 0,
          '#return_value' => $fbu,
          '#description' => '',
        );
        $form['fb_user']['message'] = array(
          '#markup' => t('If checked, link local account (!username) to facebook.com account (!fb_name).', array(
            '!username' => theme('username', array(
              'account' => $form['#user'],
            )),
            '!fb_name' => "<fb:name uid={$fbu} useyou=false></fb:name>",
          )),
          '#prefix' => "\n<p>",
          '#suffix' => "</p>\n",
        );
      }
      elseif (!$fbu && $_fb_app) {

        // they are not connected to facebook; give option to connect here
        $form['fb_user'] = array(
          '#type' => 'fieldset',
          '#title' => t('Facebook Application ' . $_fb_app->title),
          '#collapsed' => false,
          '#collapsible' => false,
        );
        $fb_button = theme('fb_login_button', array(
          'text' => t('Connect with Facebook'),
        ));
        $form['fb_user']['button'] = array(
          '#markup' => $fb_button,
          '#weight' => -1,
          '#prefix' => "\n<p>",
          '#suffix' => "</p>\n",
        );
      }
    }
    else {
      $form['fb_user'] = array(
        '#type' => 'fieldset',
        '#title' => t('Facebook Application ' . $_fb_app->title),
        '#collapsed' => false,
        '#collapsible' => false,
      );
      $form['fb_user']['message'] = array(
        '#markup' => t('Local account !username is not connected to facebook.com.', array(
          '!username' => theme('username', array(
            'account' => $form['#user'],
          )),
        )),
        '#prefix' => "\n<p>",
        '#suffix' => "</p>\n",
      );
    }
  }

  // On user/edit, hide proxied email
  if (isset($form['account']) && isset($form['account']['mail'])) {
    $account = $form['#user'];

    // Proxied email obsolete.  Deprecated.
    if (isset($account->fb_user_proxied_mail) && $form['account']['mail']['#default_value'] == $account->fb_user_proxied_mail) {
      unset($form['account']['mail']['#default_value']);
    }
  }
  return $form;
}