I want to add a custom buttom to a page so that users can subscribe to the node. The button should only be shown in case the user is not yet subscribed to the node. How do I check that in PHP? I think the function "notifications_get_subscriptions" might do the trick, but I don't know how to use it. Can anyone post a small code snippet?

Comments

advseb’s picture

Status: Active » Closed (fixed)

I figured it out by myself. The following function does the trick:

<?php
		// get subscriptions for current user and node
		$options = notifications_ui_subscribe_options($user, 'node', $node);

		// scan all subscriptions and create corresponding links
		foreach ($options as $index => $option) {
			// we don't allow subscribing to content type or author here
			if ($option['type'] != 'thread') {
				continue;
			}

			// either show subscribe or unsubscribe button
			if (!empty($option['subscription'])) {
		      // Unsubscribe link
				$sub_link = notifications_get_link('unsubscribe', array('sid' => $option['subscription']->sid, 'confirm' => FALSE, 'html' => 'TRUE', 'destination' => $_GET['q']));
				break;
			} else {
				// Subscribe link
				$sub_link = notifications_get_link('subscribe', array('uid' => $user->uid, 'type' => $option['type'], 'confirm' => FALSE, 'html' => 'TRUE', 'fields' => $option['fields'], 'destination' => $_GET['q']));
				break;
			}
		}
?>
BenK’s picture

Subscribing....