Drupal 7 Views Cookbook

2 (1 reviews total)
By J. Ayen Green
  • Instant online access to over 7,500+ books and videos
  • Constantly updated with 100+ new titles each month
  • Breadth and depth in over 1,000+ technologies
  1. Modifying Default Views

About this book

Fully revised and updated for 2016, Drupal 7 Views Cookbook allows you to bypass most of the Views' learning curve and quickly take advantage of the capabilities of the Views module to select and present your Drupal content through step-by-step instructions for quickly developing dozens of useful views.

Starting with a brief introduction to Views, Drupal 7 Views Cookbook takes you through recipes for elementary views, such as displaying randomly selected content, to intermediate views, such as customizing links, to advanced views providing multiple interacting displaysof content on the same page. Along the way, there are recipes for theming views, creating a view in your code, creating a custom view handler, administering views, and finally, using some other modules related to the Views module. Most recipes are standalone, so pick them in any order!

Don't limit your site with dull presentation of content in last-in-first-out order. Empower your site visitors with the recipes that Drupal 7 Views Cookbook delivers!

Publication date:
March 2012
Publisher
Packt
Pages
218
ISBN
9781849514347

 

Chapter 1. Modifying Default Views

In this chapter, we will cover:

  • Selecting recent comments for a specific node type

  • Focusing the archive view

  • Changing the front page view

  • Selecting the glossary view entries for a specific user

  • Creating an attached menu for the taxonomy term view

  • Reporting a tracker activity for a certain user role

 

Introduction


The Views module comes with a number of useful predefined views. Not only are they there for you to use, but they are ready to be edited to meet whatever special needs arise.

Since these views are ready to use the moment the module is enabled, the steps necessary to make some changes to them are less than those needed to create new custom views, and so using them is a logical choice for our first chapter.

 

Selecting recent comments for a specific node type


The Recent comments view provides a block containing comments that link to a page providing additional comment content. The default behavior of this view is to select comments from all content types. We will edit this view to enable us to display comments for a specific content type.

Getting ready

  1. Ensure that:

    1. Your site has at least two types of content (the default Page and Article types are fine) and that you have access to both.

    2. The content you wish to use has comments.

  2. Navigate to the Views page (admin/structure/views) and click on the Enable link for the Recent comments view.

  3. Click the down arrow beside the Edit link for the same view, and from the drop-down menu click on the Clone link.

  4. Enter Article comments recent for the view name and click on the Continue button.

How to do it...

We will now have been placed in an edit window for the new cloned view that we have created.

  1. Click on the Add button in the Filter Criteria section.

  2. A window titled Add filter criteria will open.

  3. Click on the filter select box, which currently shows All, and select Content.

  4. Scroll down to Content: Type and check the box. At the bottom of the window, you will now see Selected: Content: Type. Click on the Apply (all displays) button at the bottom of the Add filter criteria box to reveal a new window, titled Configure filter criterion: Content: Type.

  5. In the Content Types section, click on the checkbox next to the content type that you want the view to select. In our case, we will click on the box for article.

  6. Click on the Apply (all displays) button.

  7. Click on the Recent comments link in the Title section and change the title to Recent article comments, then click on the Apply (all displays) button.

  8. Click on the path link /comments/recent in the Page Settings section.

  9. Change the path to article-comments/recent (you don't need to enter the leading slash, it's provided) and click on the Apply button.

  10. At the top of the page, click on the Block display button.

  11. Click on the block name None link in the Block Settings section which opens a window titled Block: Block admin description.

  12. Change the block admin description to the Recent article comments view and click on the Apply button.

  13. Now back in the main view window, click on the Save button at the top right of the screen.

  14. Navigate to article-comments/recent.

How it works...

Most of the views that you will create will probably be Node views, views that use nodes as the primary source of data. This view uses a different entity type: comments. The relationship that was already in place in the view links each selected comment to the applicable content.

The filter that was already present when we cloned the default view, Published or Admin, limits the selection of comments to published content, unless the user has admin capability, in which case all comments will be selected. We added another filter, Content: Type, which we configured to further limit the selection to comments made on articles. Therefore, even though there was another piece of content with a comment, it was not displayed, because it was not an article. However, if we run the original view (comments/recent), we see the result of not having added the additional filter:

There's more...

As you have seen, more than one filter can be applied to the selection of content. One additional filter to consider is Comment: Approved, which limits the displayed comments to those that have been approved, or not, depending on the chosen setting.

Later in the book, we will modify this view so that instead of only showing Articles, we can specify which type of content by changing the URL.

 

Focusing the archive view


The Archive view displays a list of links that are the months in which content was published.

Each link leads to a page that presents teasers of each of the pieces of content published that month. We will add a filter to the view so that only the current user's content is considered.

Getting ready

  1. Ensure that your site has content posted by more than one author (for testing purposes).

  2. Navigate to the Views page (admin/structure/views) and click the Enable link for the Archive view.

  3. Click on the down arrow beside the Edit link for the same view, and from the drop-down menu, click on the Clone link.

  4. Enter Archive for current user as the view name and click on the Continue button.

How to do it...

We will edit the clone we have created, and make some modifications to it to provide a new view.

  1. Click on the Advanced link.

  2. Click on the Add button in the Relationships section.

  3. Check the box for Content: Author and click on the Apply (all displays) button.

  4. In the Configure Relationship window that appears, click on the Apply (all displays) button.

  5. Click on the Add button in the Filter Criteria section and a window titled Add filter criteria will open.

  6. Check the box for User: Current, then click on the Apply (all displays) button.

  7. A window titled Configure filter criterion will open. Click on the radio button for Yes in answer to Is the logged in user, then click on the Apply (all displays) button.

  8. Click on the Monthly archive link in the Title section, change the title to User monthly archive and click on the Apply (all displays) button.

  9. Click on the /archive link for the path in the Page Settings section, change the path to user-archive (no leading slash) and click on the Apply button.

  10. Click on the Block button at the top of the page.

  11. Click on None for Block name in the Block Settings section, change the block admin description to User archive and click on the Apply button.

  12. Click on the Save button.

  13. Navigate to user-archive:

How it works...

This is an example of a view that uses content as the data source. The original filter limits the selection of content titles to those that are published, and produces a links list of the months in which they were published. We added another filter, which further limits the selection to content created by the current user.

 

Changing the front page view


The composition of the front (home) page in Drupal can be themed to alter its appearance, but the structure and sources of its content are determined for you. If you leave things as they are, the content area of the front page will consist of whatever nodes you have promoted, in any quantity up to the maximum that is set in the site configuration, and that's pretty much the extent of your control... or is it?

We're going to create a new front page.

Getting ready

For this recipe, we're going to be using a custom content type, Gallery, the details of which are given in Appendix B, Bundles. Feel free to duplicate it, create a content type more meaningful to you, or just use one of the existing content types. You can also enable this content type as a Feature (see Appendix C, Resources). The content type also makes use of an image style so that the image in each piece of content can be presented in a uniform manner.

  1. Create two entries of the content type you choose to use.

  2. Navigate to the Views page (admin/structure/views) and click on the Enable link for the Front page view.

  3. Click on the down arrow beside the Edit link for the same view, and from the drop-down menu, click on the Clone link.

  4. Enter Alternate front page for the view name and click on the Continue button.

How to do it...

We will edit the clone we have created, and make some modifications to it to provide a new view.

  1. Click on Full next to Use pager in the Pager section, select Paged output, mini pager, and click on the Apply (all displays) button.

  2. Change Items per page to 1 and click on the Apply (all displays) button.

  3. Click on the Add button in the Filter Criteria section.

  4. Scroll down to Content: Type, check the box, and click on the Apply (all displays) button.

  5. In the Configure filter criterion window that opens, check the box for the Gallery content type and click on the Apply (all displays) button.

  6. Click on the Save button.

  7. Navigate to the site information page (admin/config/system/site-information).

  8. In the text box for the path to the default front page, change node to frontpage and click on the Save configuration button.

  9. Navigate to the site home page to view the page:

  10. Clicking the pager will load the next piece of content:

How it works...

The default version of the front page view is designed to provide precisely what the Drupal front page does. We created a view that shows only content of the custom Gallery type, composed primarily of an image, with one piece of content per page. Then we let Drupal know that this new view should be used as our front page.

 

Selecting the glossary view entries for a specific user


The glossary view presents a list of all content organized by the first letter of the title. This is a convenient view for site visitors that know the name of the content they are seeking, or who simply want to browse. We're going to give the user the ability to browse the content created by a specific author.

Getting ready

  1. Ensure that your site has content posted by more than one author (for testing purposes).

  2. Navigate to the Views page (admin/structure/views) and click on the Enable link for the Glossary view.

  3. Click on the down arrow beside the Edit link for the same view, and from the drop-down menu, click on the Clone link.

  4. Enter Author glossary for the view name and click on the Continue button.

How to do it...

The glossary view, and now the clone that we created, has two displays: the page display that lists content, and an attachment display that lists each letter of the alphabet for which there is content, along with the number of applicable pieces. We will make some changes to the existing displays and create an additional one that will provide the author list.

  1. Click on the +Add button in the Displays section at the top of the page and select Attachment from the list.

  2. Click on the Attachment link next to display name and change the name to New attachment, so we can tell one display button from the other, and click on the Apply button.

  3. Click on the Add button in the Sort criteria section, select This attachment (override) from the For select box, check the box for User: Name and click on the Apply (this display) button, and in the configuration window that opens, click on the Apply (this display) button.

  4. In the Attachment Settings section, click on Before next to Attachment position, select the radio button for After, and click on the Apply button.

  5. Click on the Not defined link next to Attach to in the Attachment Settings section, check the box for Page, and click on the Apply button.

  6. In the Fields section, click the Content: Title link, select This attachment (override) from the For select box, and click on the Remove button.

  7. Do the same and remove the Content: Updated date field.

  8. Click on the Advanced link to open that section, then click on the Settings link next to Query settings in the Other section, select This attachment (override) from the For select box, check the box for Distinct, check the box below it for Pure Distinct, and click on the Apply (this display) button.

    That is the work needed for the attachment itself. Now, we need to make a few minor changes to the settings from the page display.

  9. Click on the Page display button.

  10. Click on the Advanced link to expand that section.

  11. In the Other section, click on the Yes link for Use AJAX, select No, and click on the Apply button.

  12. Click on the Content: Title link in the Contextual filters section, select This page (override) from the For select box, and in the section titled When the filter value is Not in the URL ensure provide default value of type fixed value of a is the setting.

  13. In the section When the filter value is in the URL, check the box for Override title, enter Content beginning with %1, and click on the Apply (this display) button.

  14. Click on the Add button in the Contextual filters section, select This page (override) from the For select box, scroll down and check the box for User: Name, and click on the Apply (this display) button.

  15. In the subsequent configuration window, in the When the filter value Is in the URL section, check the box for Override title, enter Content for author %2, and click on the Apply (this display) button.

  16. Click on the (author) User: Name link in the Fields section, ensure that the checkbox for Link this field to its user is not checked, scroll down and click the Rewrite Results link, check the box for Output this field as a link, enter author-glossary/all/[name] as the link path, then click on the Apply (all displays) button.

  17. Click on the /glossary link next to path in the Page settings section, change the path to author-glossary (no leading slash) and click on the Apply button.

  18. Click on the Normal: Glossary link next to Menu in the same section, change the title from Glossary to Author glossary, and click on the Apply button.

  19. Click on the Save button.

  20. Navigate to the home page. First invoke the view simply by click on the Author Glossary link in the navigation menu, and then making a selection of one of the letters shown. Notice the new attachment below the list of content. Yours will vary from the following image, based on the authors on your site:

  21. Then, click on the name of an author in the author attachment.

How it works...

We added a new attachment display to the existing view, giving it three displays. The first is the page display, which is the section of the view containing the information about content. The second is the original attachment, which appears first on the page, and is formatted as a summary of the available titles using the first letter of the title, giving a total for each letter. The new attachment appears last, and contains the names of each author who has content on the site.

We specified that the new attachment would inherit two arguments from the page display (which receives the arguments), those being the additional portions of the URL. The first argument is the title of a piece of content, or "all" to specify that all should be used. The second argument is the name of an author, or is omitted entirely for all authors to be selected. We turned off AJAX processing of the links, because when using AJAX the page is not reloaded, so the new arguments are not picked up for the title.

The link produced for each author name links back to the same view, providing "all" as the title being searched (so, all titles) and the author's name. Thus, the page URL when selected from the menu item appears as author_glossary with no arguments, in which case all records are retrieved; when an author link is clicked, a is inserted as a default; and author_glossary/all/author_name, where all titles belonging to that author are retrieved.

There's more...

Attachment displays are a boon to the value of your views and your site. You can find several examples of their power in my book Drupal 6 Attachment Views, also by Packt Publishing.

 

Creating an attached menu for the taxonomy term view


In this recipe, we will be using an attachment display as a menu.

Getting ready

  1. Ensure that some of your content has taxonomy terms (tags) assigned to it.

  2. Navigate to the Views page (admin/structure/views) and click on the Enable link for the Taxonomy term view.

  3. Click on the down arrow beside the Edit link for the same view, and from the drop-down menu, click on the Clone link.

  4. Enter Taxonomy term menu for the view name and click on the Continue button.

  5. The view has an additional display that we do not need, so let's remove it. Click on the button for the Feed display, then the down arrow beside the clone Feed link, and delete the feed.

  6. Click on the Save button.

How to do it...

We will be adding an attachment display that will be used as a menu for the view.

  1. Click on the + Add button in the Displays section and select Attachment.

  2. Click on Content beside Show in the Format section, select This attachment (override) in the For select box, change the setting to Fields, click on the Apply (this display) button, and then the Apply (this display) button in the subsequent configuration form.

  3. Click on the Advanced link to expand that section, click on the link for each of the Contextual filters, and then the Remove button.

  4. Click on the Add button in the Relationships section, select This attachment (override) from the For select box, scroll down and check the box for Content: Tags (field_tags), and click on the Apply (this display) button and again in the subsequent configuration window.

  5. Click on the Add button in the Fields section, select This attachment (override) from the For select box, scroll down to Taxonomy term: Term ID and check the box, then click on the Apply (this display) button, and in the subsequent configuration window, check the box for Exclude from display and click on the Apply (this display) button.

  6. Again, click on the Add button in the Fields section, select This attachment (override) from the For select box, scroll down to Taxonomy term: Name and check the box, then click on the Apply (this display) button.

  7. In the subsequent settings box, uncheck the Create a label checkbox and click on the Rewrite Results link to expand that section.

  8. Check the box for Output this field as a link, and enter taxonomy/term-menu/[tid] as the link path.

  9. Click on the No Results Behavior link to expand that section, check the box for Hide if empty, leaving the Hide rewriting if empty box checked as well, and click on the Apply (this display) button.

  10. Click on the link for each of the two entries in the Sort criteria section, select This attachment (override), and click on the Remove button.

  11. Click on the Add icon to add a sort criterion, check the box for Taxonomy term: Name, and click on the Apply (this display) button.

  12. Ensure Ascending is selected in the subsequent configuration window, and click on the Apply (this display) button.

    Those changes took care of the data requirements of the new attachment display. Now, we need to make some changes to the structural parts of it before we finish.

  13. Click on the 10 items link next to the Display a specified number of items link in the Pager section, change the 10 to 0, select This attachment (override) from the For select box, and click on the Apply (this display) button.

  14. Click on the None link in the Title section, select This attachment (override) from the For select box, enter Terms as the title, then click on the Apply (this display) button.

  15. In the Attachment Settings section, click on the Yes link next to Inherit contextual filters, uncheck the check box for Inherit, then click on the Apply button.

  16. Click on the Not defined link next to Attach to in the Attachment Settings section, check the box for Page, and click on the Apply button.

    Almost done now. The original view had dynamic filters that we removed. We need to add a new one now.

  17. Click on the Page button in the Displays section.

  18. Click on the Advanced link to expand that section and then the Add button for Contextual filters, select This page (override) from the For select box, check the box for Content: Has taxonomy term ID, and click on the Apply (this display) button.

  19. In the subsequent configuration window, in the When the filter value is Not in the URL section, select Provide default value, in the Type select box select Fixed value, enter all in the Fixed value textbox, then click on the Apply (this display) button.

  20. Click on the path link next to Path in the Page Settings section, change the path to taxonomy/term-menu, and click on the Apply button.

  21. Click on the Save button and navigate to taxonomy/term-menu to see the results. The taxonomy terms will be listed along with all content, but clicking one of the taxonomy terms will result in only content tagged with that term being shown:

How it works...

Content can have taxonomy terms, often known as "tags," associated with it. We took a view that uses a node's taxonomy in a different way and edited it to present the terms associated with published nodes as a menu from which we can choose one as a filter. Most of the work we did was removing settings we would not need or adding new ones. The three primary changes were adding the attachment display to use as a menu, changing the contextual filter for the page display to be the taxonomy term that we select via its link, and setting the term to output in the attachment display as a link pointing back to our view with the term name appended.

Note

Drupal treats additional information in a URL as arguments, so, for example, if a page address is my/page and the URL is my/page/2/even the values 2 and even are processed as arguments, or parameters, to be passed into the view.

There's more...

The next steps for a real site would be to theme the attachment display to list the terms horizontally, perhaps add a pager for both it and the page display, and maybe change the position setting for the attachment display to after rather than before or have it appear side by side with the content.

 

Reporting a tracker activity for a certain user role


The default tracker view lists posts created by users. We will create an exposed filter to allow the admin to filter the view by user role. Filters require certain criteria to be met in order for records to be selected. Often, the value of the filter is one that can remain constant, such as requiring that content be published in order to be visible. There are times, however, when you will want the ability to change the value that is filtered. Rather than having to edit the view each time you want this value to change, you can simply elect to have the view provide a widget with which the value can be supplied.

Getting ready

  1. Ensure that your site has content posted by more than one author, and that the authors represent more than one role.

  2. Navigate to the Views page (admin/structure/views) and click on the Enable link for the Tracker view.

  3. Click on the down arrow beside the Edit link for the same view, and from the drop-down menu, click on the Clone link.

  4. Enter Tracker role for the view name and click on the Continue button.

How to do it...

  1. Click on the Add button in the Filter criteria section.

  2. Check the User: Roles checkbox and click on the Add (all displays) button.

  3. In the new configuration window that opens, check the box for Expose this filter to visitors…, check the box for Allow multiple selections, and click on the Apply (this display) button.

  4. Click on /tracker next to Path in the Page Settings section, change the path to tracker-role (no leading slash) and click on the Apply button.

  5. Click on Normal: Recent posts next to Menu in the Page Settings section, change the Title to Recent posts with selectable role and click on the Apply button.

  6. Click on the Advanced link to expand that section.

  7. Click on the link for Content: User posted or commented in the Contextual filters section, select This page (override) from the For select box, and then click on the Remove button.

  8. Click on the Add button in the Contextual filters section, scroll down and check the box for User: Roles, then click on Apply (this display).

  9. In the When the filter value is Not in the URL section, ensure that Display all results for the specified field is selected, and click on the Apply (this display) button.

  10. Click on the Save button.

  11. Navigate to tracker-role and you will see the exposed filter as shown in the following screenshot, which you can use to choose to see nodes created by users of a specific role:

How it works...

We changed the contextual filter for the view to use the author's role. Because this is a view based on content and not users, the information will come from content records and not user records, which means that the list will only contain roles for which content exists. In choosing the role in the exposed filter, its rid (role ID) gets passed in the URL back to the view, which then lists only the content that has been created by a user with that role.

About the Author

  • J. Ayen Green

    J. Ayen Green is a software architect, developer, author, poet, and rascal. He is the author of titles including Drupal 7 Views Cookbook, Drupal 6 Content Administration, and Drupal 6 Attachment Views. Green enjoys nature, critters, photography, the arts, experiencing other cultures, riding horses, and discovering new ales. He and his wife, Sofia-Aileen, make their home in Atlanta when they're not traveling. Track him down @accidentalcoder or theaccidentalcoder (dot) com.

    Browse publications by this author

Latest Reviews

(1 reviews total)
Did not go into enough depth of possible integrations with other plugins e.g. geolocation.
Book Title
Access this book, plus 7,500 other titles for FREE
Access now