Categories

Facebook + Drupal — sending invitations to friends

05.11.2014
Facebook + Drupal
Author:

We have already spoken about publications on Facebook public pages. Now we want to tell you how to send invitations to friends using Facebook SDK for JavaScript.

There is an out-of-box solution in Facebook js sdk for displaying popup with your friends on Facebook. It is a Requests Dialog. It can be used to send the request directly from one person to another or to display a form which allows the sender to select multiple recipients of the invitations.

To make such screen appear on your website you need to create your own application in Facebook. To do this you should specify the page from which the request will be sent and the popup for inviting friends will be shown in the Canvas URL field. In the Secure Canvas URL field it is necessary to indicate the page which the invited users can see (it definitely should support https protocol).

Let’s start with the description of two pages (namely the configuration page and the page with the very popup).

/**
 *  Implements hook_menu().
 */
function idevels_facebook_menu() {
  $items = array();

  $items['admin/config/system/facebook'] = array(
    'title'            => 'App configuration',
    'page callback'    => 'drupal_get_form',
    'page arguments'   => array('idevels_facebook_config'),
    'access arguments' => array('facebook configuration'),
  );
  $items['request'] = array(
    'title'            => 'Facebook request page',
    'access callback'  => TRUE,
    'page callback'    => 'drupal_get_form',
    'page arguments'   => array('idevels_facebook_request_page'),
  );
  return $items;
}

Then add permissions to the configuration page:

/**
 * Implements hook_permission().
 */
function idevels_facebook_permission() {
  return array(
    'facebook configuration' => array(
      'title'       => t('Access facebook configuration page'),
      'description' => t('View and edit facebook intergration cofiguration'),
    ),
  );
} 

Describe the configuration form in which App ID and App Secret will be saved:

/**
 * Facebook request configuration page.
 */
function idevels_facebook_config($form, &$form_state) {
  $form['idevels_facebook_app'] = array(
    '#type'  => 'fieldset',
    '#title' => t('Facebook app settings'),
  );
  $form['idevels_facebook_app']['idevels_facebook_app_key'] = array(
    '#type'          => 'textfield',
    '#title'         => t('App ID'),
    '#default_value' => variable_get('idevels_facebook_app_key', ''),
    '#required'      => TRUE,
  );
  $form['idevels_facebook_app']['idevels_facebook_app_secret'] = array(
    '#type'          => 'textfield',
    '#title'         => t('App Secret'),
    '#default_value' => variable_get('idevels_facebook_app_secret', ''),
    '#required'      => TRUE,
  );
  return system_settings_form($form);
}

We also need to write the code for the page containing the popup for inviting friends:

/**
 * Facebook request page.
 */
function idevels_facebook_request_page($form, &$form_state) {
  $id = variable_get('idevels_facebook_app_key', FALSE);
  if ($id) {
    drupal_add_js('http://connect.facebook.net/en_US/all.js', 'external');
    drupal_add_js(drupal_get_path('module', 'idevels_facebook') . '/js/idevels_facebook.js');
    $form['root'] = array(
      '#markup' => '<div id="fb-root"></div>',
    );
    $form['button'] = array(
      '#markup' => l(t('Request'), 'request', array(
        'attributes' => array(
          'class' => array('button'),
          'id' => 'request-facebook',
          'data-appid' => $id
        ),
        'fragment' => ' ')
      ),
    );
  }
  return $form;
}

Be sure to add div from fb-root. It is connected with the Facebook Library.

$form['root'] = array(
  '#markup' => '<div id="fb-root"></div>',
);

Now let’s add script of the popup. To do this it is enough to initialize the application and to display a popup:

Drupal.behaviors.internetDevelsFacebook = {
        attach: function (context, settings) {
          $('#request-facebook').click(function() {
              var id = $(this).attr('data-appid');
              FB.init({
                  appId  : id,
                  status : true,
                  xfbml  : true,
              });
              FB.ui({
                method: 'apprequests',
                message: 'My Great Request'
              }, function(response) {});
          });
        }
    };

You can send an invitation not only to friends but also to other Facebook users, specifying their id codes:

FB.ui({method: 'apprequests',
  message: 'My Great Request',
  to: {user-ids}
  }, requestCallback);
);

That's all, the invitation functionality is ready.

To learn more about Facebook SDK for JavaScript click here.

4 votes, Rating: 5

Read also

1

The migrate module makes it possible to import website’s content into Drupal from other sources. Read in our blog how to install and set up this framework for the correct work.

2

Developing our store package CommerceBox, we have faced an issue of keeping specific functionality for apps in features. Feature Injection technique solved this problem.

3

Being a member of Drupal Community is very responsible. That is why internetDevels company organized regular 3-months contribution code sprint. The results are promising.

4

Node.js is a powerful platform for building all kinds of applications which can be integrated with Drupal 7. From our blog you will learn how to install and set up...

5

Often one can see such charges against Drupal: “It is impossible to build a high-load website using this framework”. We will refute this myth on...

Subscribe to our blog updates