Looking at just about anything worth doing, a question will often arise beginning with the words, “How do I.” Often the challenge can seem daunting. Then, one finally intuits, discovers or otherwise stumbles upon the answer and simultaneously is offered several alternative opinions, each being offered as the best way to accomplish the same goal. This is the case whether planning a vacation route, taking a photograph, or creating part or all of an application.
There are a number of ways to accomplish what we will be doing in the article. If you spend any time on the Drupal IRC (Internet Relay Chat) channels, you will most likely receive varying opinions as to the best approach and, perhaps, come away more confused than when you started. Sometimes, there is no clear answer. One approach would be to write custom code. Another might be to use the Panels module. Each approach is valid, and has different pros and cons in terms of features, effort, learning curve, and time. Here, we’re going to face each challenge in the same way, with attachment views, which means less coding, less time, and a smaller learning curve.
A view is originally a relational database term, referring to a temporary arrangement of information in the database so that it can be presented in a meaningful way which is different than the underlying table layout. The Views module accomplishes the same thing, and provides the glue to tie itself in to the rest of Drupal and, especially, the ability to theme the result with templates. In other words, it gives you the ability to look at Drupal content in a way you would otherwise be unable to (without custom code).
What is an Attachment view?
A view is the dynamic display of one or more pieces of related content based on one or more criterion.
What does that mean in practice? Let’s consider a simple example. Let’s say we have created a number of nodes of the content type ‘Story’ and assign one or more taxonomy terms to each. Having done that, we want to be presented with a list of teasers for each Story that has ‘travel’ as one of its taxonomy terms.
It’s a fairly common requirement. If you’re familiar with Joomla!, for example, it could be accomplished by means of a Section or Category Blog page. The fact, though, is that the architecture that makes Drupal so extensible results in there being no manner in which to accomplish this using a core module. Enter the Views module, which will allow us to specify that we want a page on which we want to view x number of nodes, their selection based on certain criteria, which in this case will be nodes containing the taxonomy term ‘travel’.
That, in a nutshell, describes views at their simplest. Now, how about Attachment views? Well, to continue using the same example, let’s say that our requirement has changed, and we don’t always want a page based on every node having to do with travel, but want to be able to select destinations from a list of regions shown on the same page, as illustrated in the following figure.
The box on the left shows the available travel regions, each of which is a taxonomy term, with Asia having been chosen. The boxes on the right are node teasers, each of which has Asia among its taxonomy terms.
How can we accomplish this? One method would be to code a custom page in PHP and display it. That would work, but it would also set the page in stone to some extent, bypassing the flexibility that Drupal provides. We could also create a menu of destination regions and put it in the sidebar as a block. That would work too, but the menu would not be dynamic, and would have to be edited each time a region was added, changed, or removed. One further option would be to have two separate views. How can we have two views though? We could create one as a block, but let’s say that the design calls for the selection choices to be in the content area of the page. So, that means we need to find a way to have both views as content. Enter Attachment views.
Reviewing the view requirements
The business for which our website is being built is a commercial builder’s. As with most construction businesses, subcontractors represent the major source of labor. On this site, Subcontractors will be the user type that will need to register, in order to subsequently review jobs and bid for them. There will be other authenticated user types, such as management, job supervisors and admin, but they will have user records created for them and will not need to register. Customers will be anonymous users.
To that end, a custom profile has been created for subcontractors, to capture the necessary information. We’re using the content_profile module so that each subcontractor profile will be a node. We are going to have a menu from which the user will select a contractor for which the details will be displayed. For a given view, we can create various displays. A view to be displayed like a node will have a Page display—’Page’ can be thought of as a web page—and one that is to be displayed as a block will have a Block display.
Considering our menu of subcontractors, and the display of a subcontractor’s details, in conjunction with the terms ‘Page display’ and ‘Attachment display’, the reasonable inference is that the Attachment view will be the menu-style list of subcontractors, and the Page display will be the subcontractor details, the page being larger than an attachment, and the details being larger than the menu. However, that’s not necessarily the case, and in subsequent examples we’ll invert that assignment of content to display.
The description of the subcontractor list may bring the thought ‘Block’ to mind. Often a block can be used in place of an Attachment display, and in fact, the option to create a Block display in the view is just one selection away from the Attachment type. We’re using Attachment displays rather than Block displays because Attachments are not as entity-like in their construction, and are easier to place anywhere within the page content than Blocks, which are more easily placed in regions adjacent to the content area. Attachment views do not include paging as do Page views. We are only going to be showing one subcontractor’s details at a time, so there is no paging issue there.
However, when we list the subcontractors to select from, there could be dozens, or even hundreds, and that will require us to have paging available for that display, so the Page display for our view will be the subcontractor list. We’ll build that first.
Activity 2-1–Subcontractor page view
The Subcontractor page will allow the user to view the details of a subcontractor chosen from a dynamic list. That is, the list of subcontractors will not be something separate that requires editing whenever a subcontractor is added or removed, and the list will be in the content area of the page and not in a navigational menu.
- Let’s create a new view. We’re going to create a node view named subs, as shown in the following screenshot:
- Click Next and the Views panel is presented. The panel will allow us to customize the view settings. We’ll start by creating a Page display for the view.
- We’ll click on Title and change the view title, as shown in the following screenshot. Click Update default display when you are finished.
- Let’s look at some of the other configuration options in Basic Settings. Leave the style settings as it is. A style plugin isn’t needed, because the view will eventually be themed, and since it will only be showing one record it doesn’t require a table or grid. We’ll also leave the Row style set as Fields, as we want the profile data to be displayed as a vertical list of fields. Again, changes can be made when the view is themed. We won’t use AJAX at this time. We do want to use paging with this display. It’s likely that the subcontractor list will be large, and so we’ll only want a small amount being shown at one time. We’ll change the Use pager setting to “yes”, and from the config options choose Mini pager.
- Leave the More link setting at no, we don’t need a More link, and likewise, since each record is a separate subcontractor node, we’re not concerned about unique records.
- As this view is meant only for use by the management of Guild Builders, we’ll want to restrict access to it. Change the Access setting to restrict access to a specific role.
- A role called management has already been created for use by the management staff of Guild Builders. There will probably be more roles added later, such as one for staff and another for the subcontractors themselves.
- We’ll assign access to the management role.
The Views page will always attempt to provide you with a real-time preview based on your settings. Often, the settings are being established in an order that is not conducive to creating the preview, because some information is missing. In that event, you will see a pink warning about this, for example, Fields is the display type but no fields have been chosen. Use the warnings as a way to tweak your memory about what you have left to do, but don’t worry about them, as long as there are none remaining when you think you’re done.
We won’t be caching the view, nor exposing the form in a block, so we’ll leave the settings caching at one and expose form in block at no. There will be a page header and footer, but they can be added later. Empty text won’t be an issue, because the node selection will come from a list based on existing nodes. That takes us to the end of the Basic Settings pane. Let’s move on to Sort criteria.