Book Image

Drupal 5 Views Recipes

Book Image

Drupal 5 Views Recipes

Overview of this book

The Drupal View modules give you flexibility and freedom to customize the display of your web site's content. Although there are more than 100 views-enabled modules, few site administrators use Drupal Views to its full potential. This book will enable you to realize the fullest potential of this powerful resource by providing a wide variety of powerful recipes for creating and displaying a wide variety of views ñ essential classics you will use again and again to innovative display methods that will make your Drupal site stand out. Pick and choose the ones you would like to prepare for your web site. In this book you will find ninety-four recipes to create a wide selection of views. The list includes event listings, interactive calendars and timelines, maps, proximity search, podcasting, carousels, Views Fusion, and many more. You will also explore default views, views with CCK, and master a variety of ways to associate views with related content. Most people think of Views for site visitors. But Views can also be handy for site administrators. You will get to know the Views Bulk Operations module, along with Editable Fields, and Views Custom Fields. (You'll probably wonder why you never used them before!) If you want to take Views to the next level, the book contains a code-rich chapter on theming. However, you will find most of the recipes detailed by the author do not require any original coding at all. As you progress through the recipes, you will be immersed in such Drupal Views topics as fields, arguments, filters, exposed filters, sorting, style plug-ins, formatters, cloning and copying views. Because Drupal is a worldwide and ever adapting system, the author also includes great tips and resources for navigating the online Drupal community and expanding your knowledge of the recipes. Finally, there is an extensive Appendix, which includes listings of all default views, formatters and style plug-ins for Drupal 5, along with a categorized list of patches.
Table of Contents (22 chapters)
Drupal 5 Views Recipes
Credits
About the Author
Acknowledgement
About the Reviewer
Preface
Default Views in Drupal 5 Modules
Formatters
Style Plugins
Views Hooks for Coders
Modules Included in Recipe Ingredients
Additional Resources and Modules Mentioned in Recipes
Selected Noteworthy Patches to Views
Index

Recipe 1: Installing the Views module


Note

Ingredients *

Drupal 5: http://drupal.org

Host login access

Views: http://drupal.org/project/views

Views UI: contained within the Views module

*These ingredients are prerequisite for all of the recipes that follow, and will not be listed in subsequent ingredient lists.

The Views module has a straightforward installation, much like other Drupal module installs. The following recipe instructions incorporate a mix of Windows tools and the command line for locating files, creating directories, downloading files, and unzipping them. Consider following the recipe, but feel free to substitute file management approaches specific to your operating system and preferences. This recipe is followed by a page listing helpful command line shortcuts that you should incorporate into your practice if they are available on your host.

Finding or creating the <DRUPALROOT>/sites/all/modules directory

  1. Log in to your web server. You will need to know your host name, login name, and password.

  2. Locate the Drupal root directory on your server (a common directory location is /home/your_account/public_html).

  3. If this is the first contributed module in a brand new Drupal installation, you will need to create the modules subdirectory.

    • From your Drupal root directory, type the following to change into the <DRUPALROOT>/sites/all directory:

      cd sites/all
      
    • Make a new modules directory:

      mkdir modules
      
    • While you're there, you may as well create the theme directory, if it is not there already. We will use this directory in Chapter 7, Techniques for Theming Views

      mkdir themes
      
    • Now change to the modules directory:

      cd modules
      

Downloading and uncompressing the module

  1. Open the browser of your choice.

    Note

    Most of the screenshots in this book use the Firefox browser—a popular choice for many developers. Recipe 8 covers Firefox installation.

    Go to http://drupal.org/project/views, and read the page. There are several different versions of Views available for download. We are looking for the Download link marked Recommended for 5.x.

  2. Download the module from the Views project web page. Here is one way to do that:

    • Right-click in Firefox on Download.

    • Select Copy Link Location (in Internet Explorer, this link will be called Copy Shortcut). You now have text similar to the following in your clipboard:

      http://ftp.drupal.org/files/projects/views-5.x-1.6.tar.gz

      A file with the .tar.gz extension is affectionately known as a "tarball", reflecting the days when files were backed up onto tape archives.

    • Switch to your Drupal server window. Be sure you are still in your <DRUPALROOT>/sites/all/modules directory.

    • Type:

      wget <Paste><Enter>

      Note

      In many shell environments, you can use the right mouse button to paste text from the clipboard. In Windows, you can use Ctrl+V.

    • The result on your screen, after pasting the download link, will be:

      wget http://ftp.drupal.org/files/projects/views-5.x-1.6.tar.gz
      

    After pressing Enter, the download begins. You are provided with a status report of the percent complete as shown in the following screenshot:

  3. Uncompress the module to the <DRUPALROOT>/sites/all/modules/views directory. Enter:

    tar xvf views-5.x-1.5.tar.gz
    

    Depending on your system setup, you may need syntax such as this:

    tar -zxvf views-5.x-1.5.tar.gz
    

    You may also use an alternative uncompression tool, such as 7-Zip or WinRAR. WinRAR offers a graphical interface in Windows, but is also available with a command line interface for Mac and Linux systems. The output of the tar xvf views-5.x-1.5.tar.gz command is seen in the following screenshot:

Enabling the module

  1. Return to your browser window. Go to the Module page at: http://YOURSITE.com/admin/build/modules.

    If you are not already logged into your Drupal site as an administrator, you will need to do so. Scroll down to the Views fieldset and enable the Views and the Views UI modules by clicking the two associated checkboxes.

  2. Click on the Save configuration button.

    Congratulations! You will now see a message (similar to the screenshot that follows), signaling the successful installation of our module.

Recipe notes

UI is an abbreviation for "User Interface". The Views UI module offers a point-and-click approach for selecting content and output types, controlling view sort order, and so on. The Views UI can even generate a views programming code for you, as we'll see in Chapter 2. If the Views UI module were not enabled, you could still see the existing views and create new views through code. However, the Views UI module is quite helpful: even "Ninja" Drupal programmers will typically enable it.

Command line tips

These shortcuts will assist you in your Drupal development (your host environment may, or may not, have all of these shortcuts enabled).

  • Press Ctrl+U in the command line to clear everything to the left of the cursor. For instance, if you paste something, and then realize the wrong text was in the clipboard, it may be easier to clear the whole line than to press backspace many times.

  • Press Ctrl+K on the command line to clear everything to the right of the cursor.

  • Press the up arrow to re-use a previous command line. You may also search your command history with Ctrl+R.

  • Type !$ on the command line to reuse the last argument of the previous command line. For example:

    more views_ui.module views.module
    

    lets you page through the text of both modules. The last argument of that command was views.module. If your next command is:

    less !$
    

    this will print as:

    less views.module
    

    (This tip is difficult to find with a search engine. A Google search for !$ yields nothing.) Quit both the more and less commands by typing:

    Q
    
  • Use "man" pages. Man is shorthand for the manual.

    man less
    

    reveals that

    Less is a program similar to more but which allows backward movement in the file as well as forward movement.

  • Type cd to go to your home directory.

    To return to the previous directory you were in, type:

    cd -
    
  • If you are not sure where you are in your directory structure, type:

    pwd
    

    This is the "print working directory" command, which provides the full path to your current directory. This is especially useful if your prompt is not set up to already display the full path.

  • Use the Tab key for command line completion. In step 6 of the recipe above, you could type:

    tar xvf v<tab>
    

    If there is only one file that begins with v in the directory, the Tab key will conveniently complete the full name of the zipped .tar file:

    tar xvf views-5.x-1.5.tar.gz
    

    Press Enter to run the command.

    If you already had the votingapi module installed, for instance, you would need to enter:

    tar xvf vo<tab>
    

    to distinguish voting from views.