6

I am using views to show some published content. I would like to give users with the role preview the ability to see all content within the view – published and unpublished. So they can preview how the page would look like after publishing content. My thoughts: I need to add a filter-criteria:

Published (=Yes)

 OR

Current-User-Role (=preview)

Ideas?


edit #1: to make it easier, I could also allow to see the content for all authenticated users.


edit #2: I have about 15 views where I need the preview functionality. they have different structure (filter criteria, viewmode, context etc) that means I need to keep it flexible. means probably that I need to define a filter and validate the data in the template.php

2
  • 2
    If you want to give user access to unpublished content you can have a look at view_unpublished
    – Attiks
    Commented Oct 11, 2011 at 14:10
  • @Attiks: thank you, it solved my problem, even nobody has any idea if Views will include the current user attributes into its filter. would you like to post an answer? Commented Oct 17, 2011 at 15:59

7 Answers 7

2
+50

As said in the comment: If you want to give user access to unpublished content you can have a look at view_unpublished

You can also try to use this in combination with the 'Content: Published or admin' filter, but I didn't have a change to try it.

A filter like you're describing can be a good addition to views, consider creating an issue for it.

1

If I understand well your question, I think that you can use the Access > Permissions > Access restrictions settings in your view, to limit view results to only users inside respective role.

enter image description here

Update: If your are needing allow to the same view shows only content of views according user's roles, you can install Content Access module http://drupal.org/project/content_access. Then the same View will only show to the user the content that he can see, according settings in each Content Type > Access Control tab. (Note: Maybe the module will require rebuilding content access permissions before you see changes)

enter image description here

Maybe will be useful see other alternative of module to control access to content, like ACL http://drupal.org/project/acl, but I have no experience with that module.

I hope information be useful.

3
  • thx for answering but I explained myself not good enough and you misunderstood: Everyone should see the view. the view should pick all the content, that the current user is allowed to see. so the unpublished content within all the content, is only visible for logged-in user (with a particular role) Commented Oct 1, 2011 at 14:34
  • 1
    thank you. the module view_unpublished has actually solved my problem, but it looks useful anyway. Commented Oct 17, 2011 at 15:58
  • is great to know that you have solved the issue!
    – MWt
    Commented Oct 17, 2011 at 16:09
0

You could do one thing for a temporary fix quick. Create two views separately and use some logic while rendering it at the theme layer. I could help here if you are interested in the hack.

1
  • thx, but a hack is not very handy if you are working on a big project. Commented Oct 13, 2011 at 11:32
0

Views has a filter option for Content: Published or admin. It will show unpublished content to users that have permissions to see it. Choosing that as the filter rather than Content: Published should automatically show unpublished content in line with the published as long as the user has the permissions to view the unpublished content.

3
  • It does not solve the problem. I dont want admins with the permission "administer nodes" to see all content (this is the 'admin' which is meant in the filter.) but a user with a role, which may cannot edit anything but only view. Commented Oct 14, 2011 at 10:27
  • Ah, my mistake. My understanding from your question was that you wanted users to see their own content inline even though it isn't published, which would be solved with the permissions to let users edit their own content. So you want a role that can view all unpublished content, but not necessarily edit it, but you also don't want regular admins to be able see it?
    – Paul Jones
    Commented Oct 14, 2011 at 10:30
  • 1
    I wish it would be so easy ;) Commented Oct 14, 2011 at 10:31
0

The View Published or Roles module does just that.

-1

You could write a custom module to implement

  1. a new permission view unpublished content
  2. check if the connected user has the permission to view the content if unpublished when processing the view

In your CUSTOM.module file :

/**
 * Implements hook_permission().
 */
function CUSTOM_permission() {
    return array(
    'view unpublish content' => array(
    'title' => t('View unpublished content'),
    ),  
  );
}

/**
 * Implement hook_preprocess().
 */
function CUSTOM_preprocess(&$variables, $hook) {
    global $user;
    if ($hook == 'views_view_unformatted') {
        $view = $variables['view'];
        if ($view->name == 'THE_MACHINE_NAME_OF_YOUR_VIEW'){
            foreach ($variables['rows'] as $key => $row) {
                $result = $view->result[$key];
                $node = node_load($result->node_nid);
                if ( $node->status == '1' || ( user_access('view unpublish content') && $node->status == '0' ) ){
                    $variables['rows'][$key] = render(node_view($node, 'teaser')); // or render it full if you prefer
                }else{
                    unset($variables['rows'][$key]);
                }
            }
        }
    }
}

You install your CUSTOM module Then you give this permission to your specific role And set the view to display content without filter on node status

Hope that helps :)

4
  • I have about 15 views where I need the preview functionality. they have different structure (filter criteria, viewmode, context etc) that means I need to keep it flexible. means probably that I need to define a filter and validate the data in the template.php, like you did. – I just hoped to have a solution where I dont to have to write it by myself. the problem seem not that special to me (simple preview for editorials). Commented Oct 13, 2011 at 11:54
  • after trying different things out I had success with the module view_unpublished using the permission 'View any unpublished content'. The content-type specific permissions wont work, but I dont need it anyway. As it solves my problem, its fine for me, but I am still curious how to add a filter criteria 'current users role'. thx to @Attiks & Gueno Commented Oct 14, 2011 at 10:45
  • 1
    Loading individual nodes using node_load in a preprocess hook has severe performance issue (one query per node in the result set). You should at least use node_load_multiple(). Since views is already used to build the results set, it would also be better to add the needed status field. Also, the code completely ignore the standard views rendering and use separated node_view(). At least a single node_view_multiple() should be used, but the best would be not the ignore Views' work. Finally, if the views include paging, this preprocess will breaks it. Commented Oct 15, 2011 at 11:10
  • @PierreBuyle : You are completely right, I should have coded this better using both node_view and node_load in their multiple method. As well as this code could break the rendering of the content as we overwritte it, but this code was for the case where you only list nodes' ID in the view, I forgot to mention that... But I have to correct you, this would not break paging, as I tested it and it worked. Thank you anyway for your comment which makes good relevant critics on my code :)
    – Gueno
    Commented Oct 21, 2011 at 12:39
-1

In Drupal 7 (any Views 3.x?) you can and/or filters together.

[my filters] AND user has this role OR node is published

otherwise I wonder if something like Workflow could cut it.

1
  • see question: the recent rc1 version has User:Role as a filter-creteria which relates on the author, not the current user. Commented Oct 17, 2011 at 15:47

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.