You are here

function fb_post_publish_node in Drupal for Facebook 7.4

3 calls to fb_post_publish_node()
fb_post_entity_publish_form_submit in ./fb_post.module
fb_post_node_insert in ./fb_post.module
Implements hook_node_insert().
fb_post_node_update in ./fb_post.module
Implements hook_node_update().

File

./fb_post.module, line 133

Code

function fb_post_publish_node($options, $node) {
  try {
    $node_url = url("node/{$node->nid}", array(
      'absolute' => TRUE,
    ));
    if (!empty($options['account_id'])) {
      $token = $options['page_tokens'][$options['account_id']];
    }
    else {
      $token = $options['page_tokens'][$options['feed_id']];
    }

    //dpm(__FUNCTION__ . "using $token.  User token is " . fb_user_token());

    //dpm($options, __FUNCTION__);
    $params = array(
      'access_token' => $token,
      'message' => $options['message'],
      'link' => $node_url,
      'name' => $node->title,
      'description' => $node->title,
      'actions' => json_encode(array(
        'name' => 'View More',
        'link' => $node_url,
      )),
    );

    // @TODO invoke drupal_alter()
    if (!$params['access_token']) {
      drupal_set_message(t('Post to Facebook failed.  No authorization.'), 'error');
      return;
    }

    // A common problem is "#100 link URL is not properly formatted"
    // @TODO Not sure what the test should be...
    if (strpos($params['link'], "http://local") === 0) {
      unset($params['link']);
    }
    $result = fb_graph_post($options['feed_id'] . '/feed', $params);
    if ($id = $result['id']) {

      // Note that the id returned by facebook is not a normal one that can be found at graph.facebook.com/$id.
      db_insert('fb_post_graph')
        ->fields(array(
        'entity_id' => $node->nid,
        'entity_type' => 'node',
        'graph_id' => $id,
      ))
        ->execute();
      $msg = "Posted <a href=!node_url>%title</a> to <a href=!feed_url>facebook</a>.";
      $args = array(
        '!node_url' => $node_url,
        '%title' => $node->title,
        '!feed_url' => url("http://facebook.com/profile.php", array(
          'external' => TRUE,
          'query' => array(
            'id' => $options['feed_id'],
            'sk' => 'wall',
          ),
        )),
        // This may work for page posts, does not work for profile posts.  :(
        '!post_url' => url("//www.facebook.com/permalink.php", array(
          'external' => TRUE,
          'query' => array(
            'story_fbid' => $id,
            'id' => $options['feed_id'],
          ),
        )),
      );
      watchdog('fb', $msg, $args);
      drupal_set_message(t($msg, $args));
    }
    return $result;
  } catch (Exception $e) {
    $msg1 = "Could not post <a href=!node_url>%title</a> to <a href=!feed_url>facebook</a>.  You might avoid this error in the future by <a href=!auth_url>granting additional permissions</a>.";
    $msg2 = "Post <a href=!node_url>%title</a> to <a href=!feed_url>facebook</a> failed with this error:  %error";
    $args = array(
      '%error' => $e
        ->getMessage(),
      '!node_url' => $node_url,
      '%title' => $node->title,
      '!auth_url' => fb_client_auth_url(array(
        'scope' => 'publish_actions,manage_pages',
      )),
      // How to properly generate facebook url???
      '!feed_url' => url("http://facebook.com/profile.php", array(
        'external' => TRUE,
        'query' => array(
          'id' => $options['feed_id'],
          'sk' => 'wall',
        ),
      )),
    );
    watchdog('fb', $msg2, $args, WATCHDOG_ERROR);
    drupal_set_message(t($msg1, $args), 'error');
    drupal_set_message(t($msg2, $args), 'error');
  }
}