You are here

function fb_registration_form_alter in Drupal for Facebook 7.3

Same name and namespace in other branches
  1. 6.3 contrib/fb_registration.module \fb_registration_form_alter()

Implements hook_form_alter().

File

contrib/fb_registration.module, line 37
http://developers.facebook.com/docs/user_registration

Code

function fb_registration_form_alter(&$form, &$form_state, $form_id) {
  if (!isset($GLOBALS['_fb'])) {

    // Facebook app not configured.  Nothing to do.
    return;
  }
  if ($GLOBALS['user']->uid) {

    // No need to alter user add form.
    return;
  }
  if ($form_id == 'user_register_form') {

    // Replace regular form with fb_registration.
    $form['#fb_registration'] = TRUE;

    // Some fields of the registration form require special treatment.
    _fb_registration_form_alter_fields($form);
  }
  if (isset($form['#fb_registration'])) {

    // Registration has been enabled for this form.
    $sr = $GLOBALS['_fb']
      ->getSignedRequest();
    if ($sr && !empty($sr['registration'])) {
      $registration = $sr['registration'];

      // The form has been submitted.  We don't need to alter it.  Instead we
      // must submit the original form.
      // Captcha is a special case, we can't require it during drupal_execute.
      $form['captcha'] = NULL;
      if (!isset($form_state['fb_registration_avoid_recursion'])) {
        $state = array(
          'fb_registration_avoid_recursion' => TRUE,
          'values' => $registration,
          'fb_regisration_values' => $registration,
        );

        // Drupal expects strings for some values, not the arrays sent by facebook.
        foreach ($registration as $key => $value) {
          if (is_array($value) && $value['name']) {
            $state['values'][$key] = $value['name'];

            // Simply for drupal form api.
          }
          elseif ($key == 'email' && is_string($value)) {

            // Drupal expects 'mail', not 'email'.
            if (!isset($state['values']['mail'])) {
              $state['values']['mail'] = $value;
            }
          }
        }
        drupal_execute($form_id, $state);

        // If successful, user will be redirected to another page.
      }
      return;
    }
    $fb_fields = array();
    _fb_registration_extract_fb_fields($fb_fields, $form);
    $redirect_url = url(request_path(), array(
      'absolute' => TRUE,
    ));
    $fields = json_encode($fb_fields);
    if (FALSE) {

      // Use <fb:registration> XFBML.
      // In testing, I could never get this to work._
      $attrs = "fields='" . json_encode($fb_fields) . "' redirect-uri='" . $redirect_url . "'";
      $form['fb_registration'] = array(
        '#type' => 'markup',
        '#value' => '<fb:registration ' . $attrs . '></fb:registration>',
      );
    }
    else {

      // Use the iframe markup.
      $id = $GLOBALS['_fb_app']->id;
      $url = urlencode($redirect_url);
      $fields = urlencode($fields);
      $markup = "<iframe src=http://www.facebook.com/plugins/registration.php?client_id={$id}&redirect_uri={$url}&fields={$fields}\n        scrolling=\"auto\"\n        frameborder=\"no\"\n        style=\"border:none;\"\n        allowTransparency=\"true\"\n        width=\"100%\"\n        height=\"360px\">\n</iframe>";
      $form['fb_registration'] = array(
        '#type' => 'markup',
        '#value' => $markup,
      );
    }
  }
}