test whether node is viewable
Dave Cohen - February 8, 2007 - 00:29
| Project: | Premium |
| Version: | 5.x-1.x-dev |
| Component: | Code |
| Category: | feature request |
| Priority: | normal |
| Assigned: | Unassigned |
| Status: | closed |
Description
I found the need to test whether a node was viewable by a user or not, and since $node->premium_access is only set when viewing, it was not doing the trick for me.
I added the following function to premium.module and I hope it makes it into future releases...
<?php
/**
* Allow other modules to determine whether full node is viewable.
*/
function premium_is_viewable($node, $account) {
static $cache = array();
if (!$cache[$account->uid])
$cache[$account->uid] = array();
if (!isset($cache[$account->uid][$node->nid])) {
$viewable = FALSE;
if (!$node->premium || user_access('access premium content', $account)) {
$viewable = TRUE;
}
else {
foreach (module_implements('premium_access') as $name) {
$function = $name.'_premium_access';
if ($function($account, $node)) {
$viewable = TRUE;
}
}
}
$cache[$account->uid][$node->nid] = $viewable;
}
return $cache[$account->uid][$node->nid];
}
?>
#1
Seems that we should put the premium bit into $node during the load op. Then it is available to any module that cares without this function.
#2
Whoa, this is a blast from the past.
My only hesitation about adding this logic to the load is performance. But that may be a non-issue. Depends how often a node is loaded without being viewed.
Anyway this is an old suggestion from 4.7 and I haven't been using this module recently. So I don't plan to do much about this.
#3
+1
Also, perhaps hook_premium_access() should be able to return true OR false so we can set the premium_access bit accordingly? I don't know who gets precedence.
#4
#5
Ok, here's a pass at implementing this. There was already a hook for _premium_access in the code, however it was in the node view so modules could no really take advantage of it. I've moved this to the load op, which should allow modules to hook in and override the premium access bit based on their own business logic. I've tested this briefly - I'll take a more in-depth look at it but would appreciate other eyes as well!
#6
Committed to HEAD. Per Moshe's recommendation, we've moved the premium_access flag-setting into the load operation. This allows other modules to view / change the premium_access flag.
Also, modules that return a value for hook_premium_access() can return true or false, affecting premium_access either way.
#7
Automatically closed -- issue fixed for two weeks with no activity.