Advertising sustains the DA. Ads are hidden for members. Join today

Computed Field

Calculate Subscribe/Unsubscribe links for forum topics (requires Notifications module)

Last updated on
30 April 2025

The objective was to provide a CCK field that contains a link to either subscribe or unsubscribe for the node, depending on the current subscription status.
A CCK Computed Field stores the node id:

// ensure the node has an id by saving it if it is new. 
if (!$node->nid) { node_save($node); } 
// store the nid in our computed field 
$node_field[0]['value'] = $node->nid;

Because the subscription status is user-dependent, it has to be obtained at display time and cannot be stored in the computed field (this why I only store the nid). The calculation can be done in the Display Format code at display time. The following is the Display Format for a single forum at forum/idea-exchange. I am redirecting to the topic list page from the subscription form. If you have more than one forums, you will need to determine the URL for the topic list page instead of hardcoding it:

global $user;
$nid = $node_field_item['value'];
$topic = node_load($nid, NULL, TRUE);
$subs = notifications_user_get_subscriptions($user->uid,'node',$nid, $topic, FALSE);

// compose link
$destination = "?destination=forum/idea-exchange";
if ($subs) {
  foreach ($subs as $key => $sub)
  {
     $link = notifications_get_link('unsubscribe', array('sid' => $sub->sid, 'confirm' => FALSE));
     $display = '<a class="forumsubscription unsubscribe" href="/'.$link['href'].$destination.'">'.t('Stop tracking this topic').'</a>';
     break;
   }
}
else {
  $link = notifications_get_link('subscribe', array('uid' => $user->uid, 'type' => 'thread', 'fields' => array('nid' => $nid), 'confirm' => FALSE));
  $display = '<a class="forumsubscription subscribe" href="/'.$link['href'].$destination.'">'.t('Track this topic').'</a>';
}

Help improve this page

Page status: Not set

You can: