• No results found

Creating a random ad block

In document Drupal 7 Views Cookbook (Page 50-55)

Anyone who has spent time on the Web has seen advertisements, often in the form of a banner. In a Drupal site, an ad is typically in a block, which is most often a region of the screen other than the main content area. Adding content to a block manually results in a static ad, which could be all that is required, but if dynamic ads are needed, creating a view with a block display is a good way to go. We're going to create a block that presents an ad for a randomly-selected product, and have it display within the content area.

Getting ready

1. This recipe uses a custom content type, Product, the details of which are in Appendix B, Bundles.

2. Create at least one node of this content type—more if you want to see the random selection at work.

How to do it...

On the Views list page:

1. Navigate to the views list (admin/structure/views). Click the +Add new view

link. Enter Product ads as the View name.

2. Check the Description box and enter Random product ad block. 3. Select Content from the Show list and select of type to be Product. 4. Uncheck the box for Create a page.

5. Check the box for Create a block.

6. In the Display format select boxes, select unformatted list, of teasers, without links, and without comments, respectively, set the Items per page to 1, and click the

Continue & edit button.

That creates the view, and now we just need to add a few settings:

1. Click the Content: Post date (desc) link in the Sort criteria section, and click the

Remove button.

2. Click the Add button in the Sort criteria section, scroll down and check the box for

Global: Random, and click the Apply (all displays) button. 3. Click the Apply (all displays) button.

4. Click the None link for block name in the Block settings section, enter Random ad in the textbox and click Apply.

5. Click the Save button.

6. Navigate to the Blocks admin page (admin/structure/block).

7. Scroll down to Random ad in the Disabled section, select Content from the select box, and click the Save blocks button.

How it works...

We created a view and limited its selection to product content. We set the sort option to random, which causes Drupal to select a random record from those available. We set the

record quantity to 1, so that only one product is shown. We specified that the format be a

teaser, that way we see the title, description, price, and image. At that point, if we had created a page display the view would have been displayed on its own page. Instead, because we created a block display and assigned the block to the content area, it shows below the main content.

There's more...

In Chapter 5, Theming Views, we will cover theming a view. The current format of this view

cries out for theming, such as making the price larger and floating the text next to the image.

Using a view content filter

Often, it can be overwhelming to navigate a large quantity of content, browsing through it page

by page. Filtering allows the selection of content to be reduced using specific criteria. We will create a view that allows the user to filter content.

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 with 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 for sale as the View name.

4. Check the Description box and enter Homes for sale.

5. Select Content from the Show list and select of type to be Home.

6. In the Display format select boxes in the Create a page section, select Unformatted list, teasers, with links, and without comments, respectively, then click the

Continue & edit button.

Having created the view, we need to add a few settings: 1. Click the Add button in the Filters section.

2. Scroll down and check the checkbox next to Content: Zip code (field_zip_code), then click the Apply (all displays) button.

3. In the configuration window for Content: Zip code (field_zip_code), click the Expose

this filter to visitors to allow them to change its button, ensure that the checkbox for Required is unchecked, change Label to Zip code, check the Expose operator checkbox, then click the Apply (all displays) button.

4. Click the Save button. 5. Navigate to homes-for-sale.

We can select Is not empty (Not NULL) from the Zip code filter select box, to specify that we want any content with a zip code, but there is currently a bug in that filter choice and selecting it causes the filter to disappear except for the Apply button. Instead, we will select Length is longer than and enter 4 into the textbox, signifying we want any zip code of at least 5 characters, and click the Apply button. We could

also specify a zip code using another filter, such as Is equal to, to further limit the display.

How it works...

A view is essentially primarily SQL statements, in terms of the data acquisition. In effect, a

filter results in an equivalent WHERE clause being added to the SQL statement to gather a subset of the available data. So, what we've done is create a view that gathers all content. We reduced the number of nodes the view will receive from the database by specifying that they must be of the content type Homes. Finally, we provided the means to select a subset of those

records by exposing a filter allowing the user to specify a zip code. By unlocking the operator in the filter, the user is free to request, for example, a specific zip code, zip codes greater than a specified value, all zip codes not equal to another, or every zip code.

In document Drupal 7 Views Cookbook (Page 50-55)