Showing Your Views to the World: Creating Displays
Now that you have a view, you will want to turn it into something usable that you can place on your Web site somewhere for visitors to see. This requires creating a display, placing it into some type of Drupal container, and positioning that container in a particular place. This operation may sound complex, but it does not have to be. We've already talked about the various display-specific settings that you can set during creation or editing of a display. It's now time to look at those displays and put them to use.
Views provides four types of displays: block, page, attachment, and feed. We've talked about the specific settings for those types already, but we haven't delved into the process of using them to create Web pages or parts of Web pages. In the following exercise, we'll create a display using a block—Drupal provides blocks as part of the core functionality.
Exercise 7-3. Creating a Block Display
Every view needs a display before it can be inserted on an actual page for use. A block is the easiest type of display to create and understand.
- Open the view created in Exercise 7-2 (recent_items) by going to its Edit page.
- In the left-hand drawers, change the drop-down choice to Block, and click the "Add display" button. You should now see a new drawer under Default that says Block. Note that if you do not save the view before exiting this page, your new display will be lost.
- In the Basic settings box, notice the Name field. It says Block, which matches the display type. For the base view that you'll be cloning, you can leave this name alone. For a cloned view (e.g., for Joe's Shirts), you might want to name it something more specific, such as Block_shirts.
- Change the display-specific setting if desired. You can give the block an administrative title, making it easier to find in the blocks administration page. You can also set up caching here for the block. Note that this is Drupal core's block caching, not Views based.
- Save your changes.
Now you have a display that can be shown as a block. It can be administered from the Blocks page.
With the creation of a base view and block display, you are ready to clone that view for use and make it exactly what you want with only a few modifications, rather than having to re-create the entire view every time you want to customize it.
Exercise 7-4. Cloning a View to Create More Specific Content
Cloning a view creates an exact duplicate of an existing view. You are required to give a new name to the cloned view to ensure its uniqueness, and you are allowed to change the description and tag. If you haven't created any entries for Joe's Shirts, please do so before continuing with this exercise.
- Open the recent_items view.
- Choose the Clone item in the menu bar.
- You will be taken to the Add screen. Change the view name to recent_items_shirts.
- Change the view description to "Most recent shirts posted."
- Click the Next button.
- In the Filters box, click the + sign to add a new filter.
- Choose the filter Node: Type.
- Choose the Operator "Is one of" and the Node type "Joe's Shirts."
- Update the settings.
- Save your changes.
This set of steps creates a view that displays only Joe's T-shirts. The displays from the base view are copied to the new view as well, and are ready to be put onto pages.
Now you have a base view and a specific view. Both of these views use the very basics of Views functionality. Of course, having a view defined doesn't do your users any good if it's not shown to them. At this point, we need to create a place for the view to be contained. One of the easiest ways to do so is to create a block.
Blocks are a core Drupal containment system. It's necessary to know about them to use Views effectively, although we cannot hope to cover every use of blocks within this book. Blocks are one of the most common containers for content in Drupal. They can be placed on your page and hold many types of content. For example, blocks can be easily placed within the left or right sidebars, the header or footer, or the main page content. Even more helpfully, the Blocks page under Administer >> Site building >> Blocks has small boxes on the page to show you exactly which region is which.
The process of creating a block display with Views does not just create a display; it also creates the block for you. Blocks containing views are administered just like any other block in Drupal. In some respects, a block looks very much like a node. Blocks were originally used for customization purposes—using them was an easier way to embed custom PHP code into a Web site. As time has passed, this characteristic has become less useful; PHP embedded in blocks is not easily upgraded, nor is it stored in source control. Source control is critical for many organizations, especially those that are doing serious code development work.
To embed a view into a block using the UI, you must create a block display within the Views interface. The blocks UI does not allow you to import a view into a block.
Exercise 7-5. Working with Blocks
In this exercise, we will place a block that contains a view in the right sidebar of the page.
- Navigate to Administer >> Site building >> Blocks.
- Review the list of available blocks. In the lower section of the page is the Disabled blocks list. Locate the recent_items_shirts block.
- Use the drop-down menu options to choose a placement or use the grab handle to drag the block into the region where you want the block to appear. For this exercise, choose the right sidebar. If you use the drop-down menu, the block will immediately jump to the selected region.
- Save the block. If you forget to save the block and continue working, the block will remain disabled.
- Choose the configure link to the right of the block name.
- Change the block title to "Newest shirts!"
- Save the block.
Observe your page. You should see a section in the right sidebar that contains your new block, and a list of the most recent shirts added.
Using this type of block view can be very useful. Blocks can be configured so that they are shown only to certain users, only to users who are authenticated, only on certain pages, and so forth. With just a few small changes, you could create two similar views and place them in the same location, where site visitors would see a view with an article name and a teaser, but authenticated users or subscribers could have that same view link to a full article.
A view can be created as an entire page. We've already created a block view for Joe's most recent items, and one for just his shirts. Users may want to see a full list of shirts, however, and the block view can link to the full page view. The page display automatically creates an alias that can be used in the URL to take you directly to the page containing that view.
Pages are a place where overrides really start coming into play with Views, and it becomes very important to understand exactly how they work. In a block display, a limited amount of information should be presented; presenting too much information crowds the block and overwhelms the user. However, a full page view is where users expect to get a majority, if not all, of the information they are seeking.
Exercise 7-6. Creating and Using a Page Display
In this exercise, we will create a page display to go along with our recent shirt block. We want to allow users to see all of Joe's shirts, so we'll need to let them page through the list. We might also want to take a look at the formatting to see if another option would be cleaner for displaying this information on a full page than a basic list.
- Navigate to Administer >> Site building >> Views and choose Edit next to recent_items_shirts.
- In the default display, under Basic settings, change the More link to "yes," if it is not already set.
- Change the Items to display to 5; now only five items will be shown on the page.
- Save the view.
- In the "Add display" drop-down menu, select Page. Click the "Add display" button.
- Now you will have a page display available. Highlight the page display so that you can make changes to it.
- Under the Basic settings, Items to display is set to 5. Click the 5, and look down to the box. Change the setting to 15, and click first Override and then Update. Now, for this display only, 15 items will be displayed. Notice that the status changes to "using overridden values" and the button changes to "Use default," as seen in Figure 7-15.
Figure 7-15 Configuration box changes after overriding the default
- Now that there are more items on the page, it might be useful to add a pager. Change the Use pager setting to "Full pager."
- Under Page settings, change the Path variable to "shirts" and update the settings.
- In the Fields box, you may choose to add extra fields—you may have a purchase price, an image of the shirt, comments about it, and so on. If you add any fields, click Override.
- Save the view.
If you navigate to http://www.example.com/?q=shirts now, you will see a full page of shirt content. If more than 15 items are available, a pager will appear that helps you to navigate through all of the pages. Thus your users can scroll through all the available shirts easily. If you have enough shirts to pass the block's display limit, your block will display a "More" link that takes you directly to the shirts page.
Attachments, put simply, embed a view within another view. The glossary view is the most obvious example of an attachment. Within the glossary view, the page view is the list of nodes; along the top is the attached view, which summarizes how many nodes begin with a particular letter and number.
Another use for attached views is to embed an archive of recent activity within the content page. Most archive listings appear in sidebars or on their own pages. This format represents a change from the more typical river of news style employed by many sites.
Attachments can inherit arguments from the view they are attached to. This capability gives you the ability to filter the entries displayed.
RSS feeds are the most common way to notify people of new content on a site. Most blogs use a feed, which lets their readers follow the blog in their choice of blog reader, lets readers easily keep track of which posts have been read, and, most importantly, lets readers easily get information pushed to them rather than forcing readers to seek it out.
Creating a feed view is almost as straightforward as it gets. Feeds do not allow for field selection, eliminating that list of choices. The feed style gives you two options: use the site's mission statement for the feed description or enter your own. Only the Row style: Node can be used with feeds, and the style options are to use the default RSS settings, send the node title, send the title and teaser, or send the full node. The feed display does not override any of the default options. Feeds are not generally user visible and are certainly not easily readable by the user owing to their XML-based output.