example-filter-by-current-user.html
File
View source
<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> <p>For this example, we are assuming you have a content type "Blog Post".</p> <h3>Creating the View</h3> <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> <dl> <dt>View name</dt> <dd>content_by_current_user</dd> <dt>Description</dt> <dd>List of content authored by the current user.</dd> </dl> <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> <h3>Creating the page</h3> <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> <p>Click on <strong>Continue & edit</strong>.</p> <h3>Creating the relationship</h3> <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> <p>Under <strong>Advanced</strong> in the right culumn, select <strong>add</strong> next to <strong>Relationships</strong>.</p> <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> <p>You now have access to the user data related to the content you are viewing.</p> <h3>Filtering the view</h3> <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> <p>Next to <strong>Filter criteria</strong> click on <strong>add</strong> to add a new filter to your view.</p> <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> <p>Select <em>User: Current</em> from the list and click on <strong>Add and configure filter criteria</strong>.</p> <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> <p>Select <em>Yes</em> under <strong>Is the logged in user</strong>, and click on <strong>Apply (all displays)</strong>.</p> <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> <h3>Saving & testing the view</h3> <p>Click on <strong>Save</strong> to save the view.</p> <p>You can test the view by going to the path you entered in the first part of this example.</p>