Tuesday, June 7, 2011

Introduction to Views

Let's say that you wanted to display a list of the last five page nodes created on your site.  The site administrator could keep track of this manually, but this may occupy much of the administrator's time and energy.  The author of the page could remember to update a list, but this may lead to mistakes which may taint the list.  Fortunately we can simply install the Views module for this functionality.

I downloaded the Views module here.  After copying the files to my site's /module/ directory, I enabled the Views module as well as Views exporter and Views UI.  After enabling the module, the Views area link appeared under the Administer and Site Building area.  From here I can administer views for the entire site.

 I clicked the Add button to add a new view to my site.  I entered the name "view_pages" and selected "Node" for a view type.  The view type determines which arguments, fields, sort criteria, and filters can be used to customize the view. 

A view consists of one or more Displays and the default settings.  The different display types include the following:
  • Attachment
  • Block
  • Date Browser
  • Feed
  • Page
There may be more display types available depending on which modules you have installed.  For my purpose, I chose to create two displays:  newest_additions and latest_updates.  The newest_additions displays the newest content nodes created while latest_updates displays the content nodes that have been most recently updated. 

Essentially, these two displays are the same except for the name, fields, and sort criteria.  For newest_additions I clicked on the name to change it to something meaningful.  I then added some fields for the Title of the node, the post date of the node, and a link to view the node.  Finally, I added the post date as sort critera and chose to sort in descending.

I added the latest_updates display in the same way as I added the newest_additions display.  I simply used the node updated date instead of the node post date.  I also used the node updated date as the sort criteria instead of the note post date.

I became a bit confused when my views did not look correct on my site.  They did not look correct because each of my displays used the default settings instead of the display's overridden settings.  To override the settings, I clicked the Override button when editing that settings.  

I then clicked the Save button and my view was ready to be added to a site.  Because my displays were added as Blocks, I could go to the Blocks administration screen (Administer->Site Building->Block) and drag my two blocks to the right sidebar and Voila:  I could see my Latest Updates and Newest Additions views on the right sidebar.

So that is my introduction to views.  I have other ideas for views and I will write another post about them when I have implemented them.

Until then thanks for reading!

No comments:

Post a Comment