You are here

function fb_graph_node_view in Drupal for Facebook 7.3

Implements hook_node_view() Add reasonable opengraph tags to node pages.

File

./fb_graph.module, line 331
Open Graph Helpers

Code

function fb_graph_node_view($node, $view_mode, $langcode) {
  if ($view_mode == 'full') {

    //dpm(func_get_args(), __FUNCTION__);

    // TODO: support taxonomy fields, as supported in D6.
    // Some tags might come from content type.
    if ($fb_graph_type = variable_get(FB_GRAPH_VAR_OBJECT_TYPE . '_' . $node->type, '')) {
      if ($app_label = variable_get(FB_GRAPH_VAR_OBJECT_TYPE_APP . '_' . $node->type, 0)) {
        if (!empty($GLOBALS['_fb_app']) && $GLOBALS['_fb_app']->label == $app_label) {

          // Custom type applies only when app is currently active.
          fb_graph_set_tag('og:type', $GLOBALS['_fb_app']->canvas . ':' . $fb_graph_type, FALSE);
        }
      }
      else {
        fb_graph_set_tag('og:type', $fb_graph_type, FALSE);
      }
    }

    // Add additional "smart" tags.
    $tags = fb_graph_get_tags();
    fb_graph_set_tag('og:url', url('node/' . $node->nid, array(
      'absolute' => TRUE,
    )), FALSE);
    fb_graph_set_tag('og:title', t($node->title), FALSE);
    if (variable_get(FB_GRAPH_VAR_SMART_TAGS, TRUE)) {
      $body = field_get_items('node', $node, 'body');
      fb_graph_set_tag('og:description', text_summary($body[0]['value']), FALSE);
    }
    if (isset($tags['og:type']) && variable_get(FB_GRAPH_VAR_SMART_TAGS, TRUE)) {
      if ($tags['og:type'] == 'article') {
        fb_graph_set_tag('article:published_time', format_date($node->created, 'custom', 'Y-m-dTH:iZ'), FALSE);
        fb_graph_set_tag('article:modified_time', format_date($node->changed, 'custom', 'Y-m-dTH:iZ'), FALSE);

        // Only include URL to author if facebook's server will be able to view it.
        if (user_access('access user profiles', drupal_anonymous_user())) {

          // @TODO also test author profile is active.
          fb_graph_set_tag('article:author', url('user/' . $node->uid, array(
            'absolute' => TRUE,
          )), FALSE);
        }
      }
    }
  }
}