Posted by kassissieh on December 3, 2009 at 8:13pm
I am using Drupal's blog module to provide a lightweight electronic portfolio tool to students and teachers. I could use your feedback on the approach I've taken using content_access and hook_form_alter. Is this the best/safest way to do this? I still feel like a relative newbie to making these modifications.
For a fuller example of the reason for using blog as a portfolio, please see http://www.kassblog.com/item/1030
I added a checkbox field to the blog content type.
<?php
function cgs_blog_form_alter(&$form, $form_state, $form_id) {
if (isset($form['#node']) && ($form['#node']->type == 'blog')) { // apply only to "create blog entry" form
// add a submit function
$form['#submit'][] = 'cgs_blog_form_submit';
}
}
function cgs_blog_form_submit($form, &$form_state) {
// load content access functions
require_once(drupal_get_path('module', 'content_access') .'/content_access.rules.inc');
// load node object
$node = node_load($form_state['values']['nid']);
// set anonymous user grant array
$settings['view'][0] = 1;
// change node access permissions for this node
if ($form_state['values']['field_blog_public'][0]['value'] == 'Make this post public') {
// add view grant for anonymous users to this node
content_access_action_grant_node_permissions($node, $settings);
} else {
// remove view grant for anonymous users to this node
content_access_action_revoke_node_permissions($node, $settings);
}
}
?>
Comments
Very complicated way of accomplishing this...
Is there a reason you didn't use CCK to add in the check box? Seems to me I'd just use CCK and Views and not even worry about the blog module.
This is a much safer and more flexible solution. For content display I've found that 95% of the time Views/CCK can solve what I want and only occasionally do I need to use form_alter if another module adds something I don't want in all situations.
Ex Uno Plures
http://elmsln.org/
http://btopro.com/
http://drupal.psu.edu/
but one thing ...
Thank you for the suggestion. I see what you mean, but what about node access permission? The node itself needs to be visible to the public only if the author has checked the box. Otherwise, it should use default permissions. I did use a CCK field for the checkbox, BTW. I also want to give users the ability to change their mind about access permission of their node.
If I used two separate content types (one for community viewing and one for the public), then the user would not be able to easily change the access permission for a node, because one can't change the content type of an existing node.
Does that make sense?
Richard
Workflow access
Workflow access would do this with no additional code or form_alters needed.
Workflow access would also bring the added benefit of setting draft posts.
Cheers,
Bill
FunnyMonkey
Yes
Thanks, Bill. Yes, that works nicely. I added the patch from http://drupal.org/node/428376 to move the workflow fieldset up near the top of the node edit form.
Do content_access and workflow_access play nicely together? I've used content_access to define access permission for some content types and occasionally for per-node access.
Richard