While working on the administration section of a medium to large site, you may need to allow different roles to create and edit different content. Typically you want Role X to be able to create and view some content types only. The creation part is easy: go to the Access control section and give Role X the Create and Edit permission for the content types you want this role to be able to create and edit. The next thing you’ll want to do is give the role a way to list all nodes for the content type it is allowed to view/edit and be able to administer these nodes. The logical thing would be to give the role access to the admin/content/node by giving this role the administer nodes permission. The problem is that the filter for this screen will show you nodes of all types regardless of what the role should be able to edit! This hack will guide you to another trick: creating a views page for Role X to be able to administer the nodes he should have access to. Read on for the rest of this hack.

A page to edit and create content types

 


Lets assume Role X needs to be able to create nodes belonging to two content types: content type 1 and content type 2. We will build a view that shows this two contente types. For this goto admin/build/views and create a new view. In the Basic Information section type a name, a description and allow Role X to have access.

The basic information section

 

In the Page section check Provide Page View, assign a URL to access your view, choose Table View in the View Type. Its a good idea to check Use Pager since you might have many nodes once your site starts growing.

The Provide Page View

 

Next, add some HTML to the Header Section so that on the top of the view you have a couple of links to add content of the type Role X can create. This can be hardwired HTML or you can create it dynamically. In this example I’ve done it with static HTML.

The Header Section

 

If the view is empty the Header will not be shown so it’s a good idea to copy the text to the Empty Text textarea. Also remember to click on Full HTML if its static HTML or PHP Code if its dynamic.

HTML content displayed when the view is empty

 

Now its time to select the fields your view will show. In my case I chose Node: ID, Node: Title, Node: type, Node: Edit link and Node: Delete link. You can also add the last update date and author also. The trick is to have the Edit link and Delete link so that the user can quickly view, edit and delete content.

Fields which the view will show

 

Next is the important step: add a filter to only display those nodes which belong to the content type the user is allowed to edit / create. It is also convinient to add a filter on the Node: Published flag and expose it so that the user can quickly filter those nodes which are published or unpublished.

Filters and exposed filters for our view

 

Finally you should select a Sort Criteria. A good choice is to use the Node: Updated Time so that recently updated nodes are displayed on top.

The nodes will be sorted acording to this criteria.

 

Now that you have defined the view you can save it and have a look at it. Here is how mine looks like:

How our view looks like in the end

 

As you can see with this method you can quickly create pages for different roles so that they can edit restricted content types. This method is easy and the results are quite good. What is missing from this page is the ability to apply settings to groups of nodes and a couple of more filters. One thing you might be tempted to do is to expose the *Node: type* filter so that the user can filter content acording to node type. Unfortunately when Drupal builds the filter it will not filter out those content types which you excluded in the filter definition! This is really annoying but I really didn’t look through the views issues to see if this has been reported. I imagine it has.

One Response to “Drupal Hack #2: content page for a role”

  1. Mark says:

    Thanks, this was extremely helpful. I’m fairly surprised that drupal does not contain this type of functionality by default, but as I’m discovering with all things drupal, if it can’t do it out of the box, the solution is not too difficult to find.

Leave a Reply

You must be logged in to post a
video comment.