8

php architect: May 2009 · 2011. 5. 1. · April 2010 7 COLUMN: Drupal Corner Creating Custom Displays with Views W hen you are building a Drupal site, there are three areas that

  • Upload
    others

  • View
    0

  • Download
    0

Embed Size (px)

Citation preview

Page 2: php architect: May 2009 · 2011. 5. 1. · April 2010 7 COLUMN: Drupal Corner Creating Custom Displays with Views W hen you are building a Drupal site, there are three areas that

April 2010 6 www.phparch.com

COLUMN: Drupal Corner Creating Custom Displays with Views

CreatIng Custom DIspLaYs wItH VIews

Adrian Webbby

An important part of building a capable Drupal site is creating displays that list your site content. The Views module makes this process fairly easy and very efficient (administratively speaking). In this column, I’ll show you what the Views module is and how to use it.

REQUIREMENTS

Requirements:Server setup meeting Drupals minimum requirements: http://drupal.org/requirements

• PHP version: 5.2+• Drupal version: 6.16• Views version: 6.x-2.10

Recommended projects:• Drush - http://drupal.org/project/drush• Editable Fields - http://drupal.org/project/editablefields

• Views Bulk Operations - http://drupal.org/project/views_bulk_operations

• Draggable Views - http://drupal.org/project/draggableviews

Related URLs:• Views plugins: http://drupal.org/taxonomy/term/89

• Advanced Help Module: http://drupal.org/project/advanced_help

Page 3: php architect: May 2009 · 2011. 5. 1. · April 2010 7 COLUMN: Drupal Corner Creating Custom Displays with Views W hen you are building a Drupal site, there are three areas that

April 2010 7 www.phparch.com

COLUMN: Drupal Corner Creating Custom Displays with Views

When you are building a Drupal site, there are three areas that will occupy most of your plan-ning and time (excluding any custom module

functionality). The first of these areas is content type configura-

tion. Most of the time, you use the CCK system for this, combined with theme templates, which were covered in the previous month’s column.

The second area that will occupy a lot of your time is aggregating your content into useful displays. This is usually done with the Views system, but you may also use custom modules as well (if you have special needs). I will briefly cover Views in this column.

You will then need to put content and aggregate displays together to form coherent, easily under-stood layouts. This can be done through the use of theme templates as well as the Panels system. We will be covering this in next month’s column.

presenting Site ContentWhen you have figured out what content you want for your site, you are ready to decide how you want it accessed by your site users. It is important to put a lot of emphasis on making your content easy to find.

Content can be many things. This is the core of the content management system, so anything we “make” content on our sites, we can easily allow the end users or administrators to manage. So, I like to divide content into two broad categories; the meaty main content (article, profile, etc.), and the struc-tural content (banner, link, etc.).

The more content you have, the more displays

you will need to present it all to the users. You can either create your own modules that define site pages and blocks or you can use the Views module to compose these displays within the site itself. This can save you a lot of time, so I want to briefly go over what the Views module is, when you would use it, and how you use it.

Since the Views system is way too big and complex to cover in a single column, I will outline the impor-tant points and direct you to further resources you can use to become more proficient.

What is the Views Module?The Views module is, fundamentally, a framework within your site that allows you to graphically build queries of your site data, and format the output.

The Views project page is a good starting point for more information and for the download of the mod-ule: http://drupal.org/project/views

Say that you have a bunch of products (content) on your site, and you need some way to display them. You can download the Views module, extract it into your sites/all/modules directory (or use Drush), enable it in the module administration page, and create a view that displays collections of these prod-ucts to your users.

The Views system creates displays, such as pages, blocks, feeds, and content panes (available with Panels). You can also attach Views displays to other Views displays. Custom modules can add new display types that render the output of the queries into dif-ferent formats for use in the site.

Every Views display can become a template in your theme, so it is possible to create an entirely new look and feel for any of the Views that exist without hacking any existing code. This can come in handy when there are site updates.

When to use Views?The short answer; Anytime you can.

As I mentioned earlier, your only alternative to Views for creating custom displays of your content is creating a custom module.

Sometimes, a view will not work for you, and you must create a module. This has traditionally been the case when your list of content was contained in a form. There are modules out that make form content possible; among them are Editable Fields, Views Bulk Operations, and Draggable Views. These are great add-on modules which do most of what you would want to do in your own forms.

So, if you need a list of content (in any format), then Views is your best friend. If you need a really custom form that encompasses a list of content or site data, then you will probably need to craft your own module. Check the Views plugins first at http://drupal.org/taxonomy/term/89, but there are more modules being added all the time that might just do what you need.

how Do you use the Views interface?The Views interface has a reputation of being two things; powerful, and confusing to newbies. It is definitely powerful, as there are so many options

Page 4: php architect: May 2009 · 2011. 5. 1. · April 2010 7 COLUMN: Drupal Corner Creating Custom Displays with Views W hen you are building a Drupal site, there are three areas that

April 2010 8 www.phparch.com

COLUMN: Drupal Corner Creating Custom Displays with Views

that you can use to create very custom displays for your content without touch-ing any code or SQL statements. Because it has so many options, it can be easy to get lost unless you know the intricacies of the Views system. This is particu-larly true with the argument and relationship configurations.

So, where do you start when working with Views?

1. Administrative permissionsAfter you have installed Views, you will need to make sure your user has the “administer views” privilege. For more information on roles and permissions, see the Drupal handbook section on managing users at http://drupal.org/handbook/modules/user. I would also HIGHLY recommend installing the advanced help module at http://drupal.org/project/advanced_help. This will allow you to take advantage of a lot of useful help information about Views, and other modules within your site.

2. Views SettingsWhen you are developing with Views, you might consider browsing through the Views settings at {yoursite}/admin/build/views/tools. Most of these settings can be left as is, but you will find them handy if you are debugging a View or want to know more about what Views is doing under the hood.

3. Current ViewsYou can see a list of all the Views created on the site by going to {yoursite}/admin/build/views (See Figure 1). When you first go to this page after installing the Views module, you will notice a few grayed out Views listed. These are default Views provided by the Views module and other modules on your sys-tem. If you click the “Enable” link, you will be able to edit the View, it will be active on the specified paths, and it will create the blocks defined in the View. These default Views can provide nice sample Views if you are not familiar with View construction.

4. Creating ViewsMost likely, you will not have any active Views when you first install the Views module. So, you will either have to enable a default View (as described above) or

create a new one by going to {yoursite}/admin/build/views/add. You can also import/export Views between sites.

When you click on the “Add” tab in the Views administrative section or go to the add link mentioned above, you will notice a rather short form that asks for a View name, description, tag, and View type, with the dreaded “Next” button (See Figure 2). Most of these fields are relatively self-explanatory. The View type field allows you to select between different types of Views. You can query and format nodes, node revisions, files, locale strings, categories (terms), users, and access logs. That’s a lot! Contributed modules can add more Views types. Based upon

FIGURE 1

Page 5: php architect: May 2009 · 2011. 5. 1. · April 2010 7 COLUMN: Drupal Corner Creating Custom Displays with Views W hen you are building a Drupal site, there are three areas that

April 2010 9 www.phparch.com

COLUMN: Drupal Corner Creating Custom Displays with Views

our experience, node Views are the most frequently needed.See http://views-help.doc.logrus.com/help/views/view-type for more informa-

tion on view types.The next section of the View add form (which is also the View edit form) gives

you a lot of options and can be somewhat intimidating at first, but it is a really easy interface after you get the hang of it (See Figure 3). One thing you will no-tice about the Views interface is that it is super AJAX-ified. This feature allows us

to navigate and configure the View settings very quickly. The View edit interface is divided into sections.

Views DisplaysOn the left, you have displays, which are the individual pages, blocks, etc. that this View defines. There is a default display that allows other displays to in-herit settings in each of the other sections. This can be a real time saver when

FIGURE 2 FIGURE 3

Page 6: php architect: May 2009 · 2011. 5. 1. · April 2010 7 COLUMN: Drupal Corner Creating Custom Displays with Views W hen you are building a Drupal site, there are three areas that

April 2010 10 www.phparch.com

COLUMN: Drupal Corner Creating Custom Displays with Views

creating Views with many displays. By default, Views ships with display types; page, block, feed, and at-tachment. Other contributed modules add displays that you can create in your Views. See http://views-help.doc.logrus.com/help/views/display for more information on View displays.

View SettingsYou can set a tag to group your views by. So, for example, if you have a content type “blog”, you can specify the tag “blog” in your blog-related Views and then filter applicable Views when looking at the list-ing at {yoursite}/admin/build/views. Tags can give you a quick way to find related Views when you have a lot of Views in your system, which happens often in larger projects.

Basic SettingsIn the basic settings section, you have quite a few options that are, more or less, standardized across different display types. You can set the display name and title, style of the View, whether it outputs in-dividual fields or whole rendered objects (such as a rendered node), items to display, access settings, etc. Most of these are self-explanatory, but there are a few that are not intuitive. Most of the set-tings have help information, though. See http://views-help.doc.logrus.com/help/views/style for more information on the display style settings and http://views-help.doc.logrus.com/help/views/analyze-theme for more information on the display theme structure.

relationshipsRelationships allow you to join related data to the result set you are querying. Suppose that you have two content types defined; book, and author. The book references the author. Now, suppose that you want to create a list of books with the book name and author name. We can create a relationship in our View display for book results that brings in the author data as well. Thankfully, CCK integrates nicely with Views, so we can create a relationship based upon a node reference field. Then when we specify the author name field, we can pull from the right data. See http://views-help.doc.logrus.com/help/views/relationship for more information on Views relationships.

ArgumentsArguments allow you to filter your result set based upon values passed into the view, normally via the page path (URL query string), but also through the View itself or if the View is called from code. Suppose you have a View that can return any list of content on your site. Now suppose that you would like to have certain pages that display only certain content types. You could create an argu-ment that accepts valid node types on the site, so we could then filter our results based upon the node type with only a single View display. Normal view page without node type argument: {yoursite}/view_path. View page filtered by {node_type}: {yoursite}/view_path/{node_type}. See http://views-help.doc.logrus.com/help/views/argument for more information on Views arguments.

fieldsFields are the actual data that is displayed to the user for each result from the View query. This section is only applicable if you have selected the “Fields” row style in the “Basic Settings” of the View display. In the last column, we discussed CCK and how you could extend your content with custom fields and create arbitrarily complex node types. Well, every field you create with CCK is easily able to be embed-ded into Views. So, if we had a book node type with an author, title, ISBN, etc., we could create View fields that would render this data for all results. See http://views-help.doc.logrus.com/help/views/field for more information on Views fields.

Sort CriteriaIt is possible to add your own sort criteria to the View results. Say that you want to order all of your results by time created or you want to order by last name. You can have multiple sort criteria, and you can arrange them to create diverse sorting capabili-ties. See http://views-help.doc.logrus.com/help/views/sort for more information on Views sorting.

filtersFilters are another way to reduce your data set. You can think of filters as SQL “WHERE” clauses. These are not passed in as parameters or through the URL like arguments, but they can be changed by the end user (if you so choose). Filters can be static, or they can be exposed as forms to the end user so the user can dynamically change the result set. If you have enabled “Use AJAX” in the “Basic Settings” section,

Page 7: php architect: May 2009 · 2011. 5. 1. · April 2010 7 COLUMN: Drupal Corner Creating Custom Displays with Views W hen you are building a Drupal site, there are three areas that

April 2010 11 www.phparch.com

COLUMN: Drupal Corner Creating Custom Displays with Views

then the results can be changed without reload-ing the page. Normally, you at least filter your View posts by “published”, meaning that the posts are ready for external viewers to read. See http://views-help.doc.logrus.com/help/views/filter for more information on Views filters.

Closing thoughtsYou can preview the View settings by using the “Live preview” section of the page (under the settings form). This allows you to test your results before you finish the View. This can be useful for testing the various query settings quickly. It does not always render exactly as it would in the real display though. So be warned!

The Views architecture is very customizable, so it is easy to extend Views to whatever purpose you need via modules. The theme system is also com-prehensive, so you should have no problem format-ting the output exactly like you want it. See http://views-help.doc.logrus.com/help/views/api for more information about extending Views through mod-ules, and see http://views-help.doc.logrus.com/help/views/using-theme for more information about them-ing Views.

As you can see, there is a lot to Views. It takes time to get familiar with all of the options, but once you get proficient with Views, you will have a very powerful site development tool at your disposal.

AdriAn is a software architect at CollectiveColors. He spends most of his day crafting solutions to interesting problems with scalable open source application frameworks, such as Drupal, Flex, and PureMVC. Currently, he is most interested in web service platforms, rich internet applications, organizational management systems, and innovative ecommerce models. Learn more: http://collectivecolors.com/about/adrian-webbContact: [email protected]: http://collectivecolors.com/blog