You are here

function fb_canvas_form_alter in Drupal for Facebook 6.2

Same name and namespace in other branches
  1. 5.2 fb_canvas.module \fb_canvas_form_alter()
  2. 5 fb_canvas.module \fb_canvas_form_alter()
  3. 6.3 fb_canvas.module \fb_canvas_form_alter()
  4. 7.3 fb_canvas.module \fb_canvas_form_alter()

Implementation of hook_form_alter.

File

./fb_canvas.module, line 303
This module provides support for Canvas page applications. Use Drupal to power traditional Facebook Apps.

Code

function fb_canvas_form_alter(&$form, &$form_state, $form_id) {

  // Add our settings to the fb_app edit form.
  if (isset($form['fb_app_data']) && is_array($form['fb_app_data'])) {
    $fb_app = $form['#fb_app'];
    $fb_canvas_data = _fb_canvas_get_config($fb_app);
    $form['fb_app_data']['fb_canvas'] = array(
      '#type' => 'fieldset',
      '#collapsible' => TRUE,
      '#collapsed' => TRUE,
      '#title' => t('Facebook canvas pages'),
      '#description' => t('Settings which apply to <a href=!url target=_blank>canvas pages</a>.', array(
        '!url' => 'http://developers.facebook.com/get_started.php?tab=anatomy#canvas',
      )),
    );
    $form['fb_app_data']['fb_canvas']['require_login'] = array(
      '#type' => 'radios',
      '#title' => t('Require authorization'),
      '#description' => t('Require authorization if you want Drupal for Facebook to call require_login() on <strong>every</strong> canvas page.'),
      '#options' => array(
        FB_CANVAS_OPTION_ALLOW_ANON => t('Allow anonymous visitors'),
        FB_CANVAS_OPTION_REQUIRE_LOGIN => t('Require all users to authorize the application'),
      ),
      '#default_value' => $fb_canvas_data['require_login'],
      '#required' => TRUE,
    );
    $form['fb_app_data']['fb_canvas']['front_anonymous'] = array(
      '#type' => 'textfield',
      '#title' => t('Front page when user is not logged in to facebook'),
      '#description' => t('Leave blank to use the site-wide front page.  See <a href="!link" target=_blank>Public Canvas Pages</a>.', array(
        '!link' => 'http://wiki.developers.facebook.com/index.php/Public_Canvas_Pages',
      )),
      '#default_value' => $fb_canvas_data['front_anonymous'],
    );
    $form['fb_app_data']['fb_canvas']['front_loggedin'] = array(
      '#type' => 'textfield',
      '#title' => t('Front page when user is logged in to facebook, but is not a user of the app'),
      '#description' => t('Leave blank to use the site-wide front page.'),
      '#default_value' => $fb_canvas_data['front_loggedin'],
    );
    $form['fb_app_data']['fb_canvas']['front_added'] = array(
      '#type' => 'textfield',
      '#title' => t('Front page for authorized users of this application'),
      '#description' => t('Leave blank to use the site-wide front page.'),
      '#default_value' => $fb_canvas_data['front_added'],
    );

    // Override themes
    $themes = system_theme_data();
    ksort($themes);
    $theme_options[0] = t('System default');
    foreach ($themes as $theme) {
      $theme_options[$theme->name] = $theme->name;
    }
    $form['fb_app_data']['fb_canvas']['theme_fbml'] = array(
      '#type' => 'select',
      '#title' => t('Theme for FBML pages'),
      '#description' => t('Choose only a theme that is FBML-aware.'),
      '#options' => $theme_options,
      '#required' => TRUE,
      '#default_value' => $fb_canvas_data['theme_fbml'],
    );
    $form['fb_app_data']['fb_canvas']['theme_iframe'] = array(
      '#type' => 'select',
      '#title' => t('Theme for iframe pages'),
      '#description' => t('Choose only a facebook-aware theme'),
      '#options' => $theme_options,
      '#required' => TRUE,
      '#default_value' => $fb_canvas_data['theme_iframe'],
    );
  }

  // We will send all form submission directly to us, not via
  // apps.facebook.com/whatever.
  if (fb_canvas_is_fbml()) {

    //dpm($form, "fb_canvas_form_alter($form_id)"); // debug

    // We're in a facebook callback
    if (!isset($form[FB_CANVAS_FORM_HANDLER_WRAP])) {

      // Use the form data structure to keep track of whether data is
      // posted directly to us, via absolute URL, or instead via
      // relative URL on apps.facebook.com/...
      $form[FB_CANVAS_FORM_HANDLER_WRAP] = array(
        '#action_fb' => $form['#action'],
        '#action_local' => _fb_canvas_make_form_action_local($form['#action']),
      );

      // This helper function allows third-party modules to control
      // where the post goes.  By default use local server.
      fb_canvas_form_action_via_facebook($form, FALSE);
    }

    // Drupal includes wacky markup for javascript junk in node forms.  It
    // makes things look terrible in FBML.  It only works when javascript is
    // enabled and it should have been implemented to degrade gracefully, but
    // it wasn't.
    if (isset($form['body_field'])) {
      unset($form['body_field']['teaser_js']);
      unset($form['body_field']['teaser_include']);
    }
  }
  elseif (fb_canvas_is_iframe()) {

    // Include the fb_sig so that when post is received, we know we are still in a canvas.
    foreach ($_REQUEST as $key => $value) {
      if (strpos($key, 'fb_sig') === 0) {
        $form['fb_canvas_iframe'][$key] = array(
          '#type' => 'hidden',
          '#value' => $value,
        );
      }
    }
  }
}