How can I programmatically enable permission for anonymous users to view comments on a single node? Comments are open, but by default anonymous users are not allowed to view comments. I would like them to on one single node.
If all you are wanting to do is allow anonymous users to view comments on a single node, you could take the dull ax approach by first allowing all anonymous users to view comments, eg:
and then, in your theme, copying the default
### is the
nid of the node you are wanting your anonymous users to View the comments and then making the following changes:
node.tpl.php, there should be something along these lines near the end of the file:
<?php print render($content['comments']); ?>
This is where the actual comments are displayed. Since this is the template that will be used for every node except the one you want special comment treatment, change this to:
<?php if ($logged_in): ?> <?php print render($content['comments']); ?> <?php endif; ?>
which will have the effect of displaying the comments only if the user is logged in. And, since the
node--###.tpl.php doesn't have this newly added
if surrounding its printing of the comments, it will display the comments for both anonymous and authenticated/logged in users.
Of course, you could also just have modified
node.tpl.php like this:
<?php if ($logged_in || $node->nid==###): ?> <?php print render($content['comments']); ?> <?php endif; ?>
and not copied it to
node--###.tpl.php, but then if you wanted to extend this to another node, you'd have to enter the logic directly in the template, and not just copying
Just remember to flush your caches to let Drupal know about your changes.
There are a couple ways to handle this and it depends on when you want to make the change.
What you'll want to change is $node->comment to one of the following:
You could use a hook_node_presave() function to change the value prior to saving based on some logic. Or you just need to get access to the node object through other means like node_load(). Just make sure to save the node if you're loading it up to make the change.