You are here

function fb_form_friend_selector_process in Drupal for Facebook 7.3

Same name and namespace in other branches
  1. 5.2 fb_form.module \fb_form_friend_selector_process()
  2. 6.3 fb_form.module \fb_form_friend_selector_process()
  3. 6.2 fb_form.module \fb_form_friend_selector_process()

A selector allowing the user to choose from their friends. This must behave differently depending on whether the form is displayed on an FBML canvas page, iframe canvas page, or regular HTML page.

File

./fb_form.module, line 255
This module defines facebook-specific form elements for use with Drupal's form API.

Code

function fb_form_friend_selector_process($orig) {

  // TODO: use fb:friend-selector on FBML pages.
  // TODO: support fb_app specified in element.  Perhaps using Facebook Connect.
  $fb = $GLOBALS['_fb'];

  // Global is set on canvas pages.
  if (!$fb) {

    // TODO: Generate an error.
    return;
  }
  static $options = NULL;
  if (!$options) {
    $query = "SELECT name, uid, pic_square FROM user WHERE uid IN (SELECT uid2 FROM friend WHERE uid1=" . fb_facebook_user() . ")";

    // FQL not SQL, no {curly_brackets}
    $result = fb_fql_query($fb, $query);

    // Store list of friends in SESSION, so our autocomplete function will not
    // have to query it every time.
    $_SESSION['fb_form_friend_selector_result'] = $result;
  }
  $element = array(
    '#validate' => array(
      'fb_form_friend_selector_validate' => array(
        $orig,
      ),
    ),
  );
  foreach (array(
    '#title',
    '#parents',
    '#description',
    '#default_value',
    '#weight',
    '#multiple',
    '#required',
    '#name',
    '#value',
    '#id',
    '#size',
    '#rows',
    '#validate',
  ) as $key) {
    if (isset($orig[$key])) {
      $element[$key] = $orig[$key];
    }
  }

  // Allow use of textarea instead of textfield, autocomplete will not work.
  if (isset($orig['#rows']) && $orig['#rows'] > 0) {
    $element['#type'] = 'textarea';
  }
  else {
    $element['#type'] = 'textfield';
  }
  $element['#autocomplete_path'] = url('fb_form_friend_selector_autocomplete', array(
    'absolute' => TRUE,
  ));
  return $element;
}