Drupal Tips and tricks: Viewing content authored by the current user

River survey activity screen image Figure 1. A screen image from the River survey activity, as new data is added the table fills up.

Today’s lesson learned was about Drupal, the content management system we’re using for handling field data…

Chris and I had gotten to a stage where we can add data to the site using a web form as part of a river survey activity. This is great – data is being collected – but we were not entirely certain where it was going or how to get it back. The next step was to create a view that would show what’s been collected so far. Again, not rocket science as in theory we’ve done this type of thing previously, but it was a bit tricky as we used a field type called ‘field collection’ that we had not used before. The field collection lets us add multiple values for something, for example, the river depth readings taken at regular intervals across a river.

Once we had a view working that would show the data, we remembered that we also needed to filter the content so that it only showed the content authored by the current user. The view we we’re using already had two filters on it: one to show only published content and a second to show only river-measurements. I added a third filter that would check for the author – in previous version of Drupal this was done slightly differently, so I thought I’d better write this blog entry to remind us of the process.

“In Drupal 7, Views 3, you must add the Relationship of Content: Author on the right side under the Advanced settings. Once you’ve added that relationship, you will be able to see the User: Current filter.”

According to the other responses to the above stackoverflow question, this can also be done using a contextual filter. When I tried the that approach, it worked OK in the view preview, but when I saved the view it did not seem to work in the actual page view. I think this may be due to a clash with the URL path as we’re already using the URL path to include the visitor group identifier, so maybe the contextual filter gets confused if an identifier on the URL is not a valid user.

Anyway, I used the above relationship and content filter approach and it works well (see Figure 1). Hopefully, we can re-use this approach in any other views where we want to filter what gets displayed according to the author.