I had to add a Subscribe link on the taxonomy pages on my site. Currently with Notifications I was only able to find the subscription links for the My Account areas and the node pages, so I somewhat recreated the links that are appended to the end of nodes on full node pages to the top of my taxonomy pages, which are views.

If you add the below function to a custom module or this module, and then add the link code to any tpl.php file you have that starts with "views-view" then you should have a dynamic link for users to subscribe or unsubscribe (the correct link with show for users) from that taxonomy.

/**
 * Override or insert variables into the views-view templates.
 *
 * @param $vars
 *   An array of variables to pass to the theme template.
 * @param $hook
 *   The name of the template being rendered ("views-view" in this case.)
 */
function catalog_misc_preprocess_views_view(&$vars, $hook) {

  if (arg(0) == 'taxonomy' && arg(1) == 'term'){
      	  // Below we see if the user is subscribed to the taxonomy of the page.
  	  // If they are, then show "Unsubscribe" link.
  	  // If they are now, then show "Subscribe" link.
  	  $query = "
     		SELECT f.value, f.sid
            FROM {notifications_fields} f
            INNER JOIN {notifications} n ON n.sid = f.sid
            WHERE n.uid = '%d'
            ";
  	 global $user;
     $result = db_query($query, $user->uid);
      // Convert the queried rows into objects.      
     while ($action = db_fetch_object($result)) {
     $subscribe[$action->value] = $action->sid;
     }
     $vars['subscribe'] = $subscribe;
     
}
}

Code to stick into the views-view...tpl.php file:

 	  // Below we see if the user is subscribed to the taxonomy of the page.
  	  // If they are, then show "Unsubscribe" link.
  	  // If they are not, then show "Subscribe" link.
  	  $term_id = $view->args[0];
     if (array_key_exists($view->args[0], $subscribe)) {
          print '<a href="/notifications/unsubscribe/sid/' . $subscribe[$term_id];
    	  print '?destination=taxonomy/term/' . arg(2);
    	  print '">Unsubscribe from the <i>' . $view->build_info['title'];
    	  print '</i> category.</a><p />When you unsubscribe, you will stop receiving updates for this category.';
     }
     else{
          print '<a href="/notifications/subscribe/' . $user->uid . '/taxonomy/tid/';
    	  print arg(2) . '?destination=taxonomy/term/' . arg(2);
    	  print '">Subscribe to the <i>' . $view->build_info['title'];
    	  print '</i> category.</a><p />Subscribing gives you updates when new items are published or important updates are made to items.';
     }

If this isn't the correct venue for posting this kind of code, please let me know where might be better. Thanks.

Comments

zazinteractive’s picture

Thanks. I needed to do the same thing

drmonkeyninja’s picture

Big thanks for your code. I've been looking into this a bit and made a few modifications, so thought I'd share my code:-

In template.php

function THEME_preprocess_page(&$vars) {
	global $user;
	
	if (arg(0)=='taxonomy' && arg(1)=='term') {
		
		// Get the term ID
		$tid = check_plain(arg(2));
		
		// Fetch current user's subscribed taxonomy terms
		$q = "SELECT f.value, f.sid FROM {notifications_fields} f
			INNER JOIN {notifications} n ON n.sid = f.sid 
			WHERE n.uid = '%d' AND f.field = 'tid' AND f.value='%d'";
		$result = db_query($q, $user->uid, $tid);
		if ($row = db_fetch_object($result)) {
			$vars['subscribe'] = l('Unsubscribe',
				'notifications/unsubscribe/sid/'.$row->sid);
		} else {
			$vars['subscribe'] = l('Subscribe',
				'notifications/subscribe/'.$user->uid
				.'/taxonomy/tid/'.$tid);
		}	
	}
}

Then in page.tpl.php you just need to include $subscribe where ever you want the link (it will only be available for a taxonomy term page and will show the correct "Subscribe/Unsubscribe" status.

Hope this helps someone.

magibird’s picture

Nice, this was exactly what I was looking for! Thanks. I used the code in #2, but it did not handle anonymous subscriptions, so I added the following "elseif" after the "if" and before the "else" statement:

elseif ($user->uid == "NULL"){
$vars['subscribe'] = l('Subscribe',
'notifications/anonymous/subscribe/taxonomy/tid/'.$tid);
}

Thought I'd share in case this is useful to anyone else!

thirstysix’s picture

Nice, thanks for your code