You are here

function fb_stream_form_alter in Drupal for Facebook 6.3

Same name and namespace in other branches
  1. 7.3 fb_stream.module \fb_stream_form_alter()

Implements hook_form_alter().

File

./fb_stream.module, line 107
Support for Facebook's Stream API.

Code

function fb_stream_form_alter(&$form, $form_state, $form_id) {
  if (isset($form['#node_type']) && 'node_type_form' == $form_id) {
    fb_stream_node_settings_form($form);
  }
  elseif ($form['#id'] == 'node-form') {
    $type = $form['type']['#value'];
    if (variable_get('fb_stream_enabled__' . $type, FALSE) && ($token = variable_get(FB_STREAM_VAR_TOKEN, NULL)) && user_access(FB_STREAM_PERM_POST)) {

      // Defaults configured per node type.
      $from_token = variable_get('fb_stream_from_token__' . $type, $token);
      $from_id = variable_get('fb_stream_from__' . $type, NULL);
      $to_id = variable_get('fb_stream_to__' . $type, NULL);
      try {

        // TODO: consolodate graph api, use batch/cache.
        $to = fb_graph($to_id, array(
          'access_token' => $token,
        ));
        $via = fb_graph('app', array(
          'access_token' => $token,
        ));
        $me = fb_graph('me', array(
          'access_token' => $token,
        ));
        $form['fb_stream'] = array(
          '#type' => 'fieldset',
          '#title' => t('Post to Facebook'),
          '#collapsible' => TRUE,
          '#collapsed' => TRUE,
        );

        // These args will be passed to t() more than once in the code that follows.
        $t_args = array(
          '%to' => _fb_get_name($to),
          '%via' => _fb_get_name($via),
          '%me' => _fb_get_name($me),
        );
        $form['fb_stream']['fb_stream_do_post'] = array(
          '#type' => 'checkbox',
          '#title' => t('Post to Facebook'),
          '#description' => $to['id'] == $me['id'] ? t('Post this content to %to on Facebook via %via application.', $t_args) : t('Post this content to %to on Facebook via the %via application and %me\'s account.', $t_args),
        );

        // Default details configured per node type.
        $form['fb_stream']['override']['fb_stream_to'] = array(
          '#type' => 'value',
          '#value' => $to_id,
        );
        if (user_access(FB_STREAM_PERM_OVERRIDE)) {

          // Allow override of author/wall.
          $to_options = array();

          // IDs of user/page walls.
          $from_options = array();

          // deprecated.  No longer used. Clean this up! XXX
          $from_tokens = array();

          // Access tokens for posting as Page (not user).
          fb_stream_post_options($token, $to_options, $from_options, $from_tokens);
          $form['fb_stream']['override'] = array(
            '#type' => 'fieldset',
            '#title' => t('Override defaults (advanced)'),
            '#collapsible' => TRUE,
            '#collapsed' => TRUE,
          );
          $form['fb_stream']['override']['fb_stream_to'] = array(
            '#type' => 'select',
            '#title' => t("Post to Wall"),
            '#options' => $to_options,
            '#description' => t('Post to which Facebook Wall?'),
            '#default_value' => $to_id,
          );

          // We'll need the token that corresponds to our "from" option.  We don't want to include tokens directly in the form, for security.
          $form['#fb_stream_from_tokens'] = $from_tokens;
          $form['#validate'][] = 'fb_stream_node_form_validate';
        }
        $form['fb_stream']['fb_stream_from_token'] = array(
          // placeholder.  See fb_stream_node_settings_form_validate().
          '#type' => 'value',
          '#value' => $from_token,
        );
        $form['fb_stream']['fb_stream_message'] = array(
          '#type' => 'textfield',
          '#title' => 'Message',
          '#default_value' => '',
          '#description' => 'Optionally, a brief message to precede the link.',
        );
      } catch (Exception $e) {
        drupal_set_message(t('Post to facebook options not available.  Possibly a temporary failure to reach facebook.com, or an expired access token.'), 'warning');
        fb_log_exception($e, t('Failed to access facebook altering node-form.'));
      }
    }
  }
}