Drupal 7 – Add the Facebook Connect login button on user registration and login page

In the past 2 days we have talked about the use of Facebook Connect module.
Drupal 7 – Allow Facebook login using Facebook Connect
Drupal 7 – Override the Facebook Connect login button theme

In this post, i will show you how to render the Facebook Login button programmatically in the Drupal user registration and login page.

The following code will render the Facebook Login button programmatically.

$user_profile = fbconnect_user_profile();
$attr = array();
if (variable_get('fbconnect_fast_reg', 0) && variable_get('fbconnect_reg_options', 0)) {
  $attr = array('perms' => 'email');
}
$ss__facebook_connect = fbconnect_render_button($attr);


 

The $ss__facebook_connect is the HTML markup of the Facebook Login button. You can set it to the $vars in the preprocess function and then print it in corresponding .tpl.php.

If you want to add the button in the registration and login page, create a custom module and implement the following hook_form_alter().

function <module>_form_alter(&$form, &$form_state, $form_id) {
  $user_profile = fbconnect_user_profile();
  $op = $user_profile ? 'login' : 'register';
  switch ($form_id) {
    case 'user_register_form':
    case 'user_login':
      $attr = array();
      if (variable_get('fbconnect_fast_reg', 0) && variable_get('fbconnect_reg_options', 0)) {
        $attr = array('perms' => 'email');
      }

      $ss__facebook_connect = fbconnect_render_button($attr);

      $form['fbconnect_button'] = array(
        '#type' => 'item',
        '#title' => t('Facebook login'),
        '#markup' => $ss__facebook_connect,
      );
      break;
  }
}

 

Done =)

Reference:

Advertisements

3 thoughts on “Drupal 7 – Add the Facebook Connect login button on user registration and login page”

  1. sorry, I don’t get it. To make a custom login page I have a custom block block–user–login.tpl.php and template.php defines the hook as

    function MYTHEME_form_alter(&$form, &$form_state, $form_id) {
    	/*drupal_set_message($form_id);*/
      if ($form['#id'] == 'user-login-form') {
        //dpm($form);
        $form['name']['#title'] = t("Custom title");
        $form['actions']['submit']['#value'] = t("Save");
      }
    }
    

    where does the $ss__facebook_connect = fbconnect_render_button($attr); go?

    Like

    1. Try this

      function MYTHEME_form_alter(&$form, &$form_state, $form_id) {
        /*drupal_set_message($form_id);*/
        if ($form['#id'] == 'user-login-form') {
          //dpm($form);
          $form['name']['#title'] = t("Custom title");
          $form['actions']['submit']['#value'] = t("Save");
      
          $attr = array();
          if (variable_get('fbconnect_fast_reg', 0) &amp;&amp; variable_get('fbconnect_reg_options', 0)) {
            $attr = array('perms' => 'email');
          }
      
          $ss__facebook_connect = fbconnect_render_button($attr);
      
          $form['fbconnect_button'] = array(
            '#type' => 'item',
            '#title' => t('Facebook login'),
            '#markup' => $ss__facebook_connect,
          );
        }
      }
      

      Like

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s