• No results found

Creating an attached menu for the taxonomy term view

In document Drupal 7 Views Cookbook (Page 37-40)

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,

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.

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

In document Drupal 7 Views Cookbook (Page 37-40)