
As site member, I would like a list of all the content created by users I have flagged as friends. I would like this list available as a tab on my user page. This is important since it helps me keep up with contributions from people I like.
How to Demo:
- Log in to the site as a user (Alpha). Create at least one piece of content.
- Log in to the site as another user (Beta). Create at least one piece of content.
- Log in to the site as a third user (Gamma). Create a piece of content . Flag Alpha and Beta as friends.
- Go to Gamma’s user page. Click on the tab Friends’ posts.
- Verify that the content created by Alpha and Beta, but not Gamma, is listed.
Required Preparations:
- The site should have a documentation page content type, as provided by the first exercise in this suite.
- The site should have the friend flag, as described in a previous exercise in this chapter.
Additional Comments:
- This, as the previous two exercises, is a case where it becomes easier to create a tab at all user pages to display friend's posts, rather than at your own user page only. (And just as in previous examples, it would be quite easy to solve both these cases if we were using Page manager.)
- Creating a view with three relationships is not something you should do unless you know that you have to. Relationships can result in heavy database queries, resulting in poor performance on your site. If the queries are necessary, though, Views is probably a good way of building them – since you (for example) get optimized queries and caching functionality built-in.
Additional Resources:
Downloads:
| Attachment | Size |
|---|---|
| 4 KB |
Suggested Solution:
- Create a new node view. Give it the name posts by friends to a given user and have it list content with newest on top. Create a page with the path user/%/posts-by-friends, listing 10 teasers and using a pager. (See creating new views in the Views basics chapter.)
- In Views' main configuration panel, add (if necessary) a relationship content: author to access the user object for the documentation author. Then add a new relationship flags: user flag to get all the friend flags set on the content author. Finally, add another relationship flags: user to join the user setting the friend flag. (See combining Flag and Views in this chapter as well as adding relationships in the advanced Views configuration chapter.)
- Add a contextual filter user: uid, using the relationship to the flagging user – this will restrict the results to only those where the flagging user matches the provided contextual filter value. (See configuring contextual filters and utilizing relationship in configuration in the advanced Views configuration chapter.)
- Add a menu tab item with the text posts by friends.
Solution Video: