Support for Drupal 7 is ending on 5 January 2025—it’s time to migrate to Drupal 10! Learn about the many benefits of Drupal 10 and find migration tools in our resource center.
As a site member, I would like to have a list of all the content I’ve written available as a tab on my user page. I would like similar tabs on other users’ pages. This is important since it helps me keep track of my own contributions as well as following what other members write.
Figure 10.14: An example of how content lists may look at a user tab.
How to demo
- Log in to the site.
- Create a documentation page.
- Visit the user account page and click on the tab “Posts”. Verify that the documentation page is listed.
- Create a documentation collection.
- Verify that the documentation collection is listed, too, and (since it is newer) is listed above the documentation page.
Preparation
- The site should have the documentation page and collection content types, as provided by the first exercise in this suite.
Suggested solution
- Add a new view with the name Content by user. Have it display content of all types, sorted with newest on top. Create a page with the path user/%/posts, with a list of 10 teasers per page, and no menu link. Check here
- In the main configuration panel, add a menu item of the type menu tab and give it the link text posts. Check here
- If the contextual filter user: uid is not present, add a new relationship content: author. Check here
- Add a new contextual filter user: uid. Use the title override option to set the title to content written by %1. Use the validation options to verify that the contextual filter value is a user ID. Check here
Comments
- You may or may not have to add the relationship in step 3 in the suggested solution. It depends on how the Views module changes. (Normally, content authors are automatically available in a view of nodes, but this may change.)
- Whenever you have a view using contextual filters, you should have the view returning page not found if the contextual filter value is missing, unless you have reasons to do otherwise. This prevents the view from being used in unexpected contexts.
- The user story doesn't say how the content should be displayed. A teaser list is easy to make and usually pleasant to look at. Changing it to a table, for example, can be made in a future user story if the client wants to change the appearance of the list.
- While this exercise does give an example of using Views, in this case it might make more sense to just use the Tracker module that is included with Core.
The step where a contextual filter is added should be updated to reflect how Views treats user: uid contextual filters in a default node view.
Attachment | Size |
---|---|
09.14 posts by user.png | 92.09 KB |