You are here

function fb_opengraph_node_view in Drupal for Facebook 7.4

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

File

./fb_opengraph.module, line 334
Open Graph Helpers

Code

function fb_opengraph_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_opengraph_type = variable_get(FB_OPENGRAPH_VAR_OBJECT_TYPE . '_' . $node->type, '')) {
      if ($app_label = variable_get(FB_OPENGRAPH_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_opengraph_set_tag('og:type', $GLOBALS['_fb_app']->canvas . ':' . $fb_opengraph_type, FALSE);
        }
      }
      else {
        fb_opengraph_set_tag('og:type', $fb_opengraph_type, FALSE);
      }
    }

    // Add additional "smart" tags.
    $tags = fb_opengraph_get_tags();
    fb_opengraph_set_tag('og:url', url('node/' . $node->nid, array(
      'absolute' => TRUE,
    )), FALSE);
    fb_opengraph_set_tag('og:title', t($node->title), FALSE);
    if (variable_get(FB_OPENGRAPH_VAR_SMART_TAGS, TRUE)) {
      $body = field_get_items('node', $node, 'body');
      fb_opengraph_set_tag('og:description', text_summary($body[0]['value']), FALSE);
    }
    if (isset($tags['og:type']) && variable_get(FB_OPENGRAPH_VAR_SMART_TAGS, TRUE)) {
      if ($tags['og:type'] == 'article') {
        fb_opengraph_set_tag('article:published_time', format_date($node->created, 'custom', 'Y-m-dTH:iZ'), FALSE);
        fb_opengraph_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_opengraph_set_tag('article:author', url('user/' . $node->uid, array(
            'absolute' => TRUE,
          )), FALSE);
        }
      }
    }
  }
}