I've modified the view_unpublished module to also optionally allow users to edit unpublished nodes. Patch is attached.

I thought this might be useful to others as I use the "unpublished" status as a "draft". This modification allows me to allow multiple users to work on the same draft.

The only thing I can't figure out is how to have the user return to the node view page after submitting edit changes. Right now, user is sent to home page. Any suggestions?

Comments

somebodysysop’s picture

StatusFileSize
new1.92 KB

This updated patch seems to resolve the problem of returning to the node view after edit.

somebodysysop’s picture

The view_unpublished.module must be weighted lighter than the node.module to work. That means that the "drupal_goto" part of the code needs to be in a module that is *heavier* than the node.module, or else the "goto" will be executed before the node and other modules can do their thing.

My solution right now is to place the "goto" part of this into another module.

/*
*  Implementation of hook_nodeapi
*/
function view_unpublished_nodeapi(&$node, $op, $a3 = NULL, $a4 = NULL) {
  switch ($op) {
    case 'update':
     if (is_numeric(arg(1)) && arg(0) == 'node' && arg(2) == 'edit' && user_access('use view_unpublished module') && !user_access('administer nodes')) {
       // If node successfully edited, then go to node view
       if ($node->status == 0 && (user_access('view unpublished '. $node->type .' content') || user_access('view all unpublished content'))) {
         drupal_goto("node/$node->nid");
        }
	 }
      break;
  }
}

This needs to be in another module weighted heavier than any other module which will use hook_nodeapi('update').

gforce301’s picture

I am not familiar with this module, however, I have written a few custom ones and I am familiar with the forms API. Could you not with a form_alter hook either:

a) Set the destination of the form to return to. This is in the forms API functionality.
b) Register an additional submit function that processes last, does not change any values, and then performs the goto.

entendu’s picture

I think gforce has the right idea of setting #redirect in the $form object

somebodysysop’s picture

Could someone provide an example of how either of these would work in this situation? Right now, this is the callback code:

+        if ($node->status == 0 && (user_access('edit unpublished '. $node->type .' content') || user_access('edit all unpublished content'))) {
+          $items[] = array('path' => 'node/'. arg(1) .'/edit', 
+            'title' => t('Edit'),
+            'callback' => 'node_page_edit',
+            'callback arguments' => array($node),
+            'access' => true,
+            'weight' => 1,
+            'type' => MENU_LOCAL_TASK
+          );
+        } 
+    }
gforce301’s picture

If I am not mistaken that chunk of code is from a menu hook. The code that produces the form is in the function node_page_edit().

You should implement hook_form_alter in a module. Docs on it are here: http://api.drupal.org/api/function/hook_form_alter/5

Docs on #redirect can be found here: http://api.drupal.org/api/file/developer/topics/forms_api_reference.html...

entendu’s picture

Assigned: Unassigned » entendu
Status: Needs review » Closed (fixed)

Patch in #1 worked fine for me. Committed.

nassimo3’s picture

Priority: Normal » Critical
Status: Closed (fixed) » Active

Hi , is there any patch doing this with the view_unpublished-6.x-1.0 ?

entendu’s picture

Version: 5.x-1.x-dev » 6.x-1.x-dev
Priority: Critical » Normal

Cool, will take a look this weekend.

geerlingguy’s picture

Status: Active » Patch (to be ported)

Subscribe

cindyr’s picture

Please port to D6! And if you can make it play nicely with this: http://drupal.org/node/606516 that would make my day. My week. My month!

physiotek’s picture

same here.
i would need both those patch. but i dont know exactly how to do. i tried with node/606516 and got a bootstrap error. then did patch -R and still have the error. i had to uninstall the module :/ so it would be very appreciated if you could port both of those patch to the official release.
thanks.

physiotek’s picture

just realized the error was due to patch changing the ownership of the file.
so node/606516 is working great but i see that the actual patch is not working anymore.
it is possible to make it work again?
thanks,
pht3k

geerlingguy’s picture

Any update on this issue? I might try to take a crack at it soon.

wgrunberg’s picture

subscribing

wgrunberg’s picture

Is there any alternative to this proposed patch for 6.x when it comes to editing unpublished nodes?
I don't want to give Organic Groups users privileges outside of their group when editing unpublished wiki posts. I am also trying to avoid having to implement a whole work-flow system through the Workflow module, etc.

geerlingguy’s picture

@#16 - I'm having the exact same dilemma... I might write some custom code for OG, or something to work in tandem with OG User Roles, to accomplish this. Hopefully I can get it done in a way that is easily sharable via a snippet or as a module...

joelstein’s picture

Status: Patch (to be ported) » Needs review
StatusFileSize
new1.74 KB

Attached is a patch which allows users with the proper permission to edit unpublished nodes.

geerlingguy’s picture

I had some trouble with the above patch on archstl.org - but I will look at it a little more.

joelstein’s picture

StatusFileSize
new2.02 KB

Here is an updated patch which respects the original access callback, which makes it work better with other access control modules. Also, make sure you flush your menu cache; otherwise this will patch will not work.

geerlingguy’s picture

Status: Needs review » Reviewed & tested by the community

I'm using this patch on archstl.org to allow Organic Group admins (using the OG Role Assign module as a helper) permission to edit/view any unpublished node within their group. It's working great; after applying the patch (please get this committed asap!), I just had to make sure I flushed the menu cache (if you don't do that, you'll get access denied errors).

Thanks for the help, @joelstein!

nancydru’s picture

A bit more compact...

  if ($node->status == 0) {
    if (user_access("$op all unpublished content") || user_access("$op unpublished $node->type content")) {
      return TRUE;
    }
  }
Anonymous’s picture

I'm not certain where to post this suggestion since it involves two issues. First, this patch worked great and fixed a real problem on our site. Thanks!

Then I realized we didn't have access to the Revisions either. A patch is being worked on by users on this issue http://drupal.org/node/500432 and that patch also works well. However, the edit patch here and the revisions patch do not work together.

It would be wonderful to eventually have both patches added to the module and working well together eventually. I get a missing argument 2 error when I try to put them together, but then I do not know much about PHP programming.

Thanks for considering this. I do like this module because it is so light-weight and doesn't involve a lot of heavy permissions and code.

retorque’s picture

The patch in #20 seems to work, but it overrides TAC-lite. We use TAC-lite to allow users to edit content for their departments and prevent them from editing content for other departments.

retorque’s picture

As a workaround for the patch in #20 overriding TAC-lite, I am currently setting node->status = 1 and executing $old_access_callback(), then setting it back to 0. I'm not convinced that workaround would be the appropriate permanent solution, but it solves my immediate problem.

I can probably figure out how to provide a patch if anyone wants to see it, but I would prefer not to provide one knowing that it's really a workaround, not a proper solution.

nancydru’s picture

Perhaps a setting to determine whether or not allow the edit permissions.

retorque’s picture

I could probably put something like that in place in the next week or so assuming I don't end up spending all of my spare time fighting fires. Should I make this a separate issue?

retorque’s picture

StatusFileSize
new4.42 KB

I'm still not certain I like the process of setting node status to published and back to unpublished to check the old access callback, but this seems to do the job and includes a setting to allow the admin to choose whether to override or apply the pre-existing access callback.

This is my first patch, so hopefully I followed the instructions properly.

nancydru’s picture

That looks unnecessarily convoluted. Don't you just want to not have the "edit" permissions for your site?

retorque’s picture

What I was looking for is for the edit permissions to only apply if the user would have been able to edit the node if it had been published. Without this change in our environment - using TAC Lite for departmental edit permissions - any user with edit unpublished permission can edit any unpublished node, not just the unpublished nodes in his or her department.

I agree that this is a bit of a convoluted method of getting that result, and I would very much prefer to see a simpler solution, but I don't have a simpler solution myself.

I did attempt to just set the node to published without setting it back, but the array seems to be passed by reference. Unfortunately my knowledge of the access control systems beyond hook_perm() and user_access() is a bit limited.

nancydru’s picture

retorque’s picture

For my installation, $old_access_callback() is a reference to node_access(). I ran dpm($old_access_callback); to confirm.

The node_access() function does not explicitly take a reference to $node, but it is passing by reference for me. That may be due to PHP configuration, but if it passes by reference for me, I can't assume it will pass by value for others.

a8w4’s picture

@geerlingguy in #21: This is very interesting! Could you please specify which OG Role module you're referring to (there are some)?

entendu’s picture

Status: Reviewed & tested by the community » Closed (fixed)

I don't seem to have this problem anymore with the new access callback.

bsandor’s picture

Status: Closed (fixed) » Active

I made this issue active as I was working on applying #20 to view unpublished 6.1.2 without any success.

function view_unpublished_menu_alter &
function _view_unpublished_node_access

seam to be the problematic parts.

Or I missed something? Is it necessary to set module weight? My understanding is it's not necessary.

This is my code that gives 'recoverable fatal error: Object of class stdClass could not be converted to string' to the line ' if (user_access("$op all unpublished content") || user_access("$op unpublished $node->type content")) {'

function view_unpublished_menu_alter(&$items) {
  $old_access_callback = $items['node/%node']['access callback'];
  $items['node/%node']['access callback'] = '_view_unpublished_node_access';
  $items['node/%node']['access arguments'] = array('view', 1, $old_access_callback);
  $items['node/%node/edit']['access callback'] = '_view_unpublished_node_access';
  $items['node/%node/edit']['access arguments'] = array('edit', 1, $old_access_callback);
}

/**
 * Returns true if the user has 'view/edit all unpublished content' or if
 * they have the permission corresponding to the node's content type.
 */
function _view_unpublished_node_access($op, $node, $old_access_callback = 'node_access') {
  // Only check permissions on nodes that are unpublished.
  if ($node->status == 0) {
    if (user_access("$op all unpublished content") || user_access("$op unpublished $node->type content")) {
      return TRUE;
      }
  }
  // If none of the above conditions were satisfied, then use original access callback.
  return $old_access_callback($op == 'edit' ? 'update' : $op, $node);
}

How can I apply #20 to view unpublished 6.1.2?

entendu’s picture

Status: Active » Closed (fixed)

@bsandor, I just released 6.x-1.3 of this module. Please install the update, it should work fine for you.

bsandor’s picture

Hi @entendu,

Frist of all: thanks for your very quick answer.

Am I missing something? There's no edit access defined in this module. I also need edit access.

My environment is an OG + OG Group Post.

I set up Workflow Fields Module. I set permissions with it for each workflow states.

What I see is: it's working fine in any other states except when my nodes are in Suggested state (nodes are unpublished here).
Also the Group admin gets the editing option. This is great.
I cant see any editing option for any group members. Not even for the node author.
When my Group Admin edits a node that a group member created it gets no longer accessable. Not for edit or view.

Also my impression is that editing and viewing permissions should be separate on Permissons settings.

Thanks in Advance. And thanks for this great module.

bsandor’s picture

Status: Closed (fixed) » Active

It doesn't work for me.

See previous post.

entendu’s picture

@bsandor, core provides the edit permissions, take a look under the "node" sections of the permissions screen. If you grant edit & view unpublished permissions, you will be able to edit an unpublished node.

bsandor’s picture

@entendu,

I don't have any 'edit unpublished' permissions at all, should I. No permission like this on whole permissions page.

Also I don't have Uninstall for View Unpublished Module. I guess it stores some info in the DB.

What is the way for me to delete them? As it might cause this problem for me if I should see 'edit unpublished permissions'...?
I installed patch #20 which might cause this for me. And then installed v1.3 which has no update if i am right.
Also My module weight was 0. I manually changed it to 1. ( as it should be if I'm right).

My install is nice and working fine so I presume something went wrong with this module somehow.

It might just not respect 'OG Roles' OR 'Workflow Fields' Modules? D6 permissions: big mistery.

Thanks in advance.

entendu’s picture

Oh, are you trying to grant access to edit *only* unpublished nodes of a certain type, not *any* (published or unpublished) nodes of a certain type?

zoen’s picture

Hi bsandor,

I found that with the 6.x-1.3 version, even though the edit perm *is* granted, the View and Edit tabs on the unpublished nodes don't render. I don't know why. Have you tried going to node/[nid]/edit to see if your user gets an Access Denied?

entendu’s picture

ZoeN is correct.

hachreak’s picture

Hi guys,
I should edit an unpublished node also if I'm not the admin and I don't have the permission "administer-nodes".
How can I do? :)

doublejosh’s picture

Having this problem myself.
This module seems to remove the permission to edit unpublished content that you don't own.

On the current dev branch for: 7.x-1.x-dev (2012-Jun-08)

entendu’s picture

Issue summary: View changes
Status: Active » Closed (won't fix)

Deprecating 6.x.