example-filter-by-current-user.html 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. <p>In this example you will create a page that displays a list of the content authored by the current logged-in user. This will demonstrate using Views <em>filters</em> and <em>relationships</em> to dynamically filter the view's contents at display time.</p>
  2. <p>For this example, we are assuming you have a content type "Blog Post".</p>
  3. <h3>Creating the View</h3>
  4. <p>The first step is creating a view for our content list page. Because the page will show the titles of content, this view is considered a "Content" type. Go to <a target="_blank" href="base_url:admin/structure/views/add">add new view</a>, enter the following properties, and click <strong>Next</strong>:</p>
  5. <dl>
  6. <dt>View name</dt>
  7. <dd>content_by_current_user</dd>
  8. <dt>Description</dt>
  9. <dd>List of content authored by the current user.</dd>
  10. </dl>
  11. <p>Choose <strong>Show</strong> <em>Content</em> <strong>of type</strong> <em>Blog Post</em>. You can choose any way you wish to sort the content.</p>
  12. <h3>Creating the page</h3>
  13. <p>Tick the box next to <strong>Create a page</strong>. Enter a page title and a path. For our purposes here, the default settings for the rest of this page are sufficient.</p>
  14. <p>Click on <strong>Continue &amp; edit</strong>.</p>
  15. <h3>Creating the relationship</h3>
  16. <p>In order to have access to the author of the content, it is important to create a relationship between the current content type, and users.</p>
  17. <p>Under <strong>Advanced</strong> in the right culumn, select <strong>add</strong> next to <strong>Relationships</strong>.</p>
  18. <p>Select <em>Content: Author</em> and click on <strong>Add and configure relationships</strong>. Leave the settings as they are and click on <strong>Apply (all displays)</strong>.</p>
  19. <p>You now have access to the user data related to the content you are viewing.</p>
  20. <h3>Filtering the view</h3>
  21. <p>Now you need to filter the view to display only content authored by the current user. This data is now available for the content because you have created the relationship in the step above.</p>
  22. <p>Next to <strong>Filter criteria</strong> click on <strong>add</strong> to add a new filter to your view.</p>
  23. <p>Filter the list of fields by selecting <em>User</em> next to <strong>Filter</strong> at the top. You now have more fields than before due to the relationship you created.</p>
  24. <p>Select <em>User: Current</em> from the list and click on <strong>Add and configure filter criteria</strong>.</p>
  25. <p>Since this field is only visible due to the relationship you created, <em>author</em> will already be selected under <strong>Relationship</strong>. This shows that the relationship you created is being used for the filter field.</p>
  26. <p>Select <em>Yes</em> under <strong>Is the logged in user</strong>, and click on <strong>Apply (all displays)</strong>.</p>
  27. <p>If you have authored content of the type <em>Blog Post</em>, you should now see a list of those posts under the preview section at the bottom.</p>
  28. <h3>Saving &amp; testing the view</h3>
  29. <p>Click on <strong>Save</strong> to save the view.</p>
  30. <p>You can test the view by going to the path you entered in the first part of this example.</p>