Drupal Notifications module - how to obtain a subscription id (SID)?
-
29-09-2019 - |
Question
I want to show a subscribe/unsubscribe link on a forum topic list page, along with each of the topics in the list. I have all the info for the subscribe link in the $topic variable in mytheme_preprocess_forum_topic_list():
foreach ($variables['topics'] as $id => $topic) {
Assuming that I want to call notifications_get_link() to get the unsubscribe link, how can I obtain the subscription id (SID) for any existing subscription for the topic node?
I suppose I ought to call notifications_user_get_subscriptions(), but the documentation is a bit thin. An example would be great.
Solution
My solution finds exactly one subscription for the current node if one exists and composes either a susbcribe or unsubscribe link that is made available to the template:
// find subscription
$subs = notifications_user_get_subscriptions(
$user->uid,
'node',
$topic->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));
$variables['topics'][$id]->subscribe_link =
'<a class="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' => $topic->nid),
'confirm' => FALSE));
$variables['topics'][$id]->subscribe_link =
'<a class="subscribe" href="/'.
$link['href'].$destination.'">'.t('Track this topic').'</a>';
}
I ended up using a CCK Computed Field for this so that I can include it in a View. See this for more context.