Daily Archive for April 22nd, 2008

Drupal: View all nodes created by a user

I found myself needing to automatically get back all the albums created by a specific user of a site I am working on. Originally I thought that this was going to be a bit more of a challenge. The hardest part ended up being that I found several ways of doing it and each of them were different. I ended up getting the result that I wanted but, not as fast as I wanted it. So maybe this will help someone else trying to accomplish a similar task.

For the process I used you will need the CCK module and the Views module. Then simply follow the steps below to view a list of nodes created by the user that is logged in.

  1. Create your content type using the CCK
  2. Go to the views admin and create a new view
  3. Check the ‘provide page’ check box
  4. Specify a url path for the view (in my case I used album/user)
  5. In the fields section set the fields that you want to show up (this is only useful in list view really)
  6. Set the arguments (this is the most important step, it lets you get nodes by a user)
    ->User: UID is Author - Default: Return page not found
  7. Expand the Argument Handling Code and paste this in it
    if (!args[0]) {
      global $user;
      $args[0] = $user->uid;
    }
     
  8. Set the filters
    ->Node:Type - is one of - (the node type you created)
    ->Node: Published = Yes
  9. Save the view

Now when you navigate to the URL that you supplied in your URL field for the view you should see any nodes of the type you chose. The custom argument handling we added will set the user id to the current logged user if one isn’t specified.

This view was helpful because I was able to use the Node Reference module for the CCK to add a node reference to the albums in my Songs node. Now I had a drop down list of the users albums so that they can relate the new song to an album, but only one of their albums.




Bad Behavior has blocked 128 access attempts in the last 7 days.