2. METODOLOGÍA
2.2. Planteamiento del prototipo del caminador
The node ID (nid), the value that is unique to each piece of content in combination with a URL, provides a direct path to that content, but what if we want a query where the path
Getting ready
1. This recipe uses a custom content type, Home, the details of which are in Appendix B, Bundles.
2. Create at least two nodes of this content type, each having a different zip code.
How to do it...
On the Views list page:
1. Navigate to the views list (admin/structure/views). 2. Click the +Add new view link.
3. Enter Homes links as the View name.
4. Check the Description box and enter Homes for sale.
5. Select Content from the Show select box, Home from the of type select box, and set sorted by to Title.
6. Enter zip/% as the Path.
7. In the Display format select boxes, select Unformatted list, teasers, without links and without comments, respectively, then click the Continue & edit button. We will define the general criteria for selecting records:
1. Click the Advanced link to expand that section.
2. Click the Add button in the Contextual Filters box, check the checkbox next to Content: Zip code (field_zip_code) (note: there are two fields with similar names),
and click the Apply (all displays) button.
3. In the resulting configuration box, in the section titled WHEN THE FILTER IS IN THE URL OR A DEFAULT IS PROVIDED, check the checkbox for Override title
and enter Homes for sale in the %1 zip code in the textbox that appears below it, then click the Apply (all displays) button.
We will create the block from which the user will select the filter value:
1. Click the + Add button next to Page in the Displays list. 2. Select Block from the list that drops down.
3. Click Home links in the Title section, select This block (override) in the For select box, enter Zip codes with homes for sale: in the textbox, and click the Apply (this display) button.
4. Click the Content link next to Show in the Format section, select This block (override) from the For select box, click the radio button for Fields, and click the
5. Click the Apply (this display) button in the subsequent window.
6. Click the Content: Title link in the Fields section, select This block (override) from the For select box, and click the Remove button.
7. Click the Add button in the Fields section, ensure This block (override) is selected in the For select box, scroll down and check the checkbox for Content: Zip code, and click the Apply (this display) button.
8. Uncheck the Create a label checkbox, click the Rewrite Results link to
expand that section, check the checkbox for Output this field as a link, type zip/ [field_zip_code] into the link path textbox, then click the Apply (this display)
button.
9. Click the Content: Title (desc) link in the Sort criteria section, select This block (override) from the For select box, and click the Remove button.
10. Click the Add button in the Sort criteria section, scroll down and check the checkbox for Content: Zip code (field_zip_code), and click the Apply (this display)
button.
11. Click the None link next to Block name in the Block Settings section, type Zip codes in the textbox, and click the Apply button.
12. Click the Content: Zip code link in the Contextual Filters section, select This block (override) from the For select box, and click the Remove button.
13. Click the Settings link next to Query settings in the Other section, select
This box (override) from the For select box, check the Distinct checkbox, then click the Apply (this display) button.
14. Click the Save button.
15. Navigate to admin/structure/block, at the bottom of the Disabled list
click the select box in the Zip codes row, select Sidebar second, and click the Save blocks button.
16. Navigate to the home page and you will find the block with the zip code list in the
How it works...
We created a basic content view, but instead of controlling the content selection with an
exposed filter through which a user provides the filtering value, we used a dynamic filter that allows the URL to provide the filter argument and created a block that presents zip codes
that link to our view with the selected zip code as an argument. The end result is a URI value becoming a WHERE clause in the SQL that retrieves the content, but with this method the selection can come from links selected by the user.
There is a deficiency in the Views module's ability to limit content selection to distinct values… it doesn't always work. If you have more than one piece of content with the same zip code, you might see it appear more than once in the selection list. This is purely a cosmetic issue, as the links resolve to the same URL.
There's more...
The decision to use a dynamic filter or an exposed filter would be based on the intention. If it is to allow the user to define a zip code, we would instead have used an exposed filter. The
argument capabilities provided by views is quite sophisticated, with the ability to use multiple arguments, default arguments, various argument validation methods, and more. Look for arguments to appear in subsequent recipes.