I've written some code in hook_nodeapi designed to unpublish a node if "save as draft" is checked, and publish it if "save as draft" is unchecked. This works the first time a node is inserted and updated. However, if you wish to again save the same node as a draft, it will not unpublish.
case 'submit':
if (module_exists('save_as_draft')) {
// Unpublish if save as draft is checked.
if ($node->save_as_draft == 1) {
$node->status = 0;
}
// If save_as_draft is clicked off and the publish status is 0
// and this user does not have the 'administer nodes' permission,
// then change publish status to 1
if ($node->save_as_draft == 0 && $node->status == 0 && (!user_access('administer nodes'))) {
$node->status = 1;
}
}
break;
I *think* my code isn't working because of this code in save_as_node_nodeapi:
case 'submit':
$current_vid = db_result(db_query('SELECT vid FROM {node} WHERE nid = %d', $node->nid));
$node->original_node = node_load($node->nid, $current_vid);
break;
case 'update':
if (isset($node->original_node)) {
// Update node table's vid to the original value.
db_query("UPDATE {node} SET vid = %d, title = '%s', status = %d, moderate = %d WHERE nid = %d", $node->original_node->vid, $node->original_node->title, $node->original_node->status, $node->original_node->moderate, $node->nid);
drupal_set_message(t('Your changes have been saved as a draft.'));
}
break;
The original node status (Published) is written, or something, but the node status isn't changed.
Question: Does anyone know if there's something I can do so that I can unpublish the nodes when "save_as_draft" is checked on in an update?
Or, is there a better way to allow a user who can't have the "administer nodes" permission to publish/unpublish his own nodes?
Thanks!
Comments
Comment #1
somebodysysop commentedOK, came up with my own solution:
I basically write the current status instead of the previous status. Not sure how this affects anything else, but it solves the problem I had. Would appreciate any comments on this approach.
Comment #2
ymmatt commentedSeems interesting, but without beating around the bush: What situation arose where this was necessary?
Comment #3
somebodysysop commentedI wanted users to be able to save "drafts" of their nodes without having to give them the "administer nodes" permission. By "draft", I mean submit an "unpublished" node that will not appear on any lists anywhere.
Do do this now requires the "administer nodes" permission, which gives you the "Publishing options" fieldset in the node submission form, in which you either check or uncheck the "Publish" option.
The save_as_drafts.module seemed to me like an end-around to the problem. Except, "saving as draft" as it is defined in this module, is NOT saving it as "unpublished". So, I wrote some code in my modules hook_nodeapi to handle that.
I finally solved the problem with this change:
Now, when I click on "save as draft", the node is saved as unpublished. When I unclick "save as draft", it is published -- unless the user has the "administer nodes" permission.
Anyway, it's exactly what I needed. And I accomplished it without having to touch the save_as_draft.module itself.
Comment #4
swortis commentedI think think this is a fabulous idea - really necessary for many production environments. However, I'm not seeing a "save as draft" button anywhere. Where should I be looking?
Comment #5
ymmatt commentedSounds like a decent work-around. However, your code would fit well in its own module independent of save as draft.
Swortis - Your comment belongs in another support request/issue, once this is posted I'll answer your question there. I'm trying to keep the issue cue relevant and clean.
Marking this as fixed
Comment #6
somebodysysop commentedYes, just for the record, my recommended solution is to use or create separate module, and within hook_nodeapi of that module:
So far, this is working great. No need to modify save_as_grant.module itself for this. But, obviously, save_as_grant.module is required for it to work.
Comment #7
Anonymous (not verified) commentedAutomatically closed -- issue fixed for two weeks with no activity.