You are here

function fb_canvas_form_alter in Drupal for Facebook 5.2

Same name and namespace in other branches
  1. 5 fb_canvas.module \fb_canvas_form_alter()
  2. 6.3 fb_canvas.module \fb_canvas_form_alter()
  3. 6.2 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 135
This module provides some app-specific navigation to facebook apps.

Code

function fb_canvas_form_alter($form_id, &$form) {

  // Add our settings to the fb_app edit form.
  if (is_array($form['fb_app_data'])) {
    $node = $form['#node'];
    $fb_app_data = fb_app_get_data($node->fb_app);
    $fb_canvas_data = $fb_app_data['fb_canvas'];

    // defaults
    if (!$fb_canvas_data) {
      $fb_canvas_data = array();
    }
    $fb_canvas_data = array_merge(array(
      'theme_fbml' => 'fb_fbml',
      'theme_iframe' => 0,
    ), $fb_canvas_data);
    $form['fb_app_data']['fb_canvas'] = array(
      '#type' => 'fieldset',
      '#collapsible' => TRUE,
      '#collapsed' => TRUE,
      '#title' => t('Facebook canvas page settings'),
      '#description' => t('Allows application-specific front page and navigation links.'),
    );
    $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 users of this application'),
      '#description' => t('Leave blank to use the site-wide front page.'),
      '#default_value' => $fb_canvas_data['front_added'],
    );

    // Allow primary links to be different on facebook versus the rest of the
    // site.  Code from menu_configure() in menu.module.
    $root_menus = menu_get_root_menus();
    $primary_options = $root_menus;
    $primary_options[0] = t('<use sitewide setting>');
    $secondary_options = $root_menus;
    $secondary_options[0] = t('<use sitewide setting>');
    $form['fb_app_data']['fb_canvas']['primary_links'] = array(
      '#type' => 'select',
      '#title' => t('Menu containing primary links'),
      '#description' => t('Your application can have primary links different from those used elsewhere on your site.'),
      '#default_value' => $fb_canvas_data['primary_links'],
      '#options' => $primary_options,
    );
    $form['fb_app_data']['fb_canvas']['secondary_links'] = array(
      '#type' => 'select',
      '#title' => t('Menu containing secondary links'),
      '#default_value' => $fb_canvas_data['secondary_links'],
      '#options' => $secondary_options,
      '#description' => t('If you select the same menu as primary links then secondary links will display the appropriate second level of your navigation hierarchy.'),
    );

    // 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'],
    );
  }
  global $fb, $fb_app;

  // 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'])) {
      $form['fb_canvas_form_handler'] = array();

      // This variable tells us to handle the form on submit.
      // Can't use 'fb_handling_form' because facebook strips it.
      $form['fb_canvas_form_handler']['_fb_handling_form'] = array(
        '#value' => TRUE,
        '#type' => 'hidden',
      );

      // We need to make sure the action goes to our domain and not apps.facebook.com, so here we tweak the form action.
      $form['fb_canvas_form_handler']['#action_old'] = $form['action'];
      if ($form['#action'] == '') {
        $form['#action'] = $_GET['q'];
      }
      $form['#action'] = _fb_canvas_make_form_action_local($form['#action']);
      $form['fb_canvas_form_handler']['#action_new'] = $form['#action'];

      // We've stored #action_old and #action_new so custom modules have the option to change it back.
    }
  }
  else {
    if (fb_canvas_is_iframe()) {

      //dpm($form, 'fb_canvas_form_alter');
    }
  }
}