You are here

function fb_stream_node_settings_form in Drupal for Facebook 7.3

Same name and namespace in other branches
  1. 6.3 fb_stream.module \fb_stream_node_settings_form()

Helper function for hook_form_alter() renders the settings per node-type.

1 call to fb_stream_node_settings_form()
fb_stream_form_alter in ./fb_stream.module
Implements hook_form_alter().

File

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

Code

function fb_stream_node_settings_form(&$form) {
  $node_type = $form['#node_type']->type;
  $token = variable_get(FB_STREAM_VAR_TOKEN, '');
  $to_options = array();
  $from_options = array();
  $from_tokens = array();

  // Include options in the form.
  $form['fb_stream'] = array(
    '#type' => 'fieldset',
    '#title' => t('Facebook Posts'),
    '#collapsible' => TRUE,
    '#collapsed' => TRUE,
    '#group' => 'additional_settings',
    '#attributes' => array(
      'class' => array(
        'fb-node-type-settings-form',
      ),
    ),
  );

  // Use $var_prefix because Drupal will append $node_type when saving variables.
  $var_prefix = 'fb_stream_enabled_';
  $var = $var_prefix . '_' . $node_type;
  $form['fb_stream'][$var_prefix] = array(
    // $var_prefix looks wrong, but is right.
    '#type' => 'checkbox',
    '#title' => t('Enable Post to Facebook for this content type.'),
    '#default_value' => variable_get($var, FALSE),
  );
  if (!$token) {
    $form['fb_stream']['fb'] = array(
      '#markup' => t('<a href=!url target=_blank>Configure settings</a>, then refresh this form to see all options.', array(
        '!url' => url(FB_PATH_ADMIN . '/fb_stream'),
      )),
      '#prefix' => '<p>',
      '#suffix' => '</p>',
    );
    return;
  }
  try {

    // Get the possible author/wall combinations from facebook.
    fb_stream_post_options($token, $to_options, $from_options, $from_tokens);

    // TODO: consolodate graph api, use batch. And cache.
    $me = fb_graph('me', array(
      'access_token' => $token,
    ));
    $via = fb_graph('app', array(
      'access_token' => $token,
    ));
    $t_args = array(
      '%from' => _fb_get_name($me),
      '%via' => _fb_get_name($via),
    );
    $var_prefix = 'fb_stream_to_';
    $var = $var_prefix . '_' . $node_type;
    $form['fb_stream'][$var_prefix] = array(
      '#type' => 'select',
      '#title' => t("Post to wall"),
      '#options' => $to_options,
      '#description' => t('Post to which Facebook Wall?  If you don\'t see the options you expect, you may need a new <a href=!url target=_blank>access token</a>.', array(
        '!url' => url(FB_PATH_ADMIN . '/fb_stream'),
      )),
      '#default_value' => variable_get($var, NULL),
    );

    // 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_settings_form_validate';
    $form['fb_stream']['fb_stream_from_token_'] = array(
      // placeholder.  See fb_stream_node_settings_form_validate().
      '#type' => 'value',
      '#value' => NULL,
    );
  } catch (Exception $e) {

    // TODO: link to token admin page.
    fb_log_exception($e, t('Failed to access facebook using the fb_stream token (node settings form).'));
  }
}