Book Image

Drupal 7 Views Cookbook

By : J. Ayen Green
Book Image

Drupal 7 Views Cookbook

By: J. Ayen Green

Overview of this book

<p>Fully revised and updated for 2016, Drupal 7 Views Cookbook allows you to bypass most of the Views' learning curve and quickly take advantage of the capabilities of the Views module to select and present your Drupal content through step-by-step instructions for quickly developing dozens of useful views.</p> <p>Starting with a brief introduction to Views, Drupal 7 Views Cookbook takes you through recipes for elementary views, such as displaying randomly selected content, to intermediate views, such as customizing links, to advanced views providing multiple interacting displaysof content on the same page<a name="_GoBack"></a>. Along the way, there are recipes for theming views, creating a view in your code, creating a custom view handler, administering views, and finally, using some other modules related to the Views module. Most recipes are standalone, so pick them in any order!</p> <p>Don't limit your site with dull presentation of content in last-in-first-out order. Empower your site visitors with the recipes that Drupal 7 Views Cookbook delivers!</p>
Table of Contents (17 chapters)
Drupal 7 Views Cookbook
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
Index

Preface

Views is a contributed module that was originally written by Earl Miles, who is known as merlinofchaos, or simply Merlin in the Drupal community. The module is maintained by him and others in the Drupal community.

Views 1 was written during the Summer of Code in 2005, and was available for Drupal 4.6, 4.7, and for Drupal 5 in 2006. For those still running a Drupal 5 site, there is a Drupal 5 Views Recipes book from Packt Publishing.

Views 2 was first released in 2008 for Drupal 6, and was a major improvement on an already very useful module. There isn't a book with recipes on Views 2, but you can find many good examples of using Views 2 in Drupal 6 Attachment Views from Packt Publishing.

Views 3 for Drupal 7 is still in beta as I write this introduction, but will be released before I get to the appendix!

What is a view?

From a general perspective: You must have just installed Drupal and the default website it creates. You have also added a few articles and assigned a descriptive term to each, that is, a category. Now, you would like to present the visitors with a page containing articles of a specific category. How do you do it? The short answer is...you can't...yet.

Alright, you decide to put that idea aside for now, and instead present all articles, but sorted by their titles. How do you do it? The short answer, again, is...you can't...yet.

The fact is that of the laundry list of thousands of functions available with Drupal, painstaking thoughts go into deciding which of them will be present "in core", that is, in the code when first installed, before anything else is added. Generally, the philosophy is that only the mission-critical functions should be present. Keeping the base platform light and fast is preferable to bloating it with functionality that can, instead, be added via contributed modules. Enter Views.

We will be exploring the capabilities of the Views module throughout the book, so for now, here is a short, in-a-nutshell definition of what this module offers.

Note

The Views module provides the capability, via a program code or the included user interface submodule, to define the criteria by which to select content, process it, manipulate it, and format its presentation. It is, at its heart, a query generator with many additional functional layers.

Many would say that a fully functional Drupal site would be almost impossible to produce without the use of the Views module, and I agree. Now, do not take that as a challenge. Of course it would be possible to write custom modules in order to purposely accomplish a rich site without using the Views module, but why bother?

From a MySQL perspective: If you are not familiar with MySQL, it stands for My Structured Query Language and is the most used database with Drupal. The database contains Drupal's settings as well as the content added to the website.

So, let us say that we have a table in our database, and it is called node, and in this table we keep whatever content we have added to the website. If we want to retrieve all the content from this table, the command given to MySQL would be:

SELECT * FROM node;

This would return all the data stored in that table, each piece of content being a row (a record). If we wanted to retrieve only blog content, the command would be:

SELECT * FROM node WHERE node_type='blog';

If we want to sort the records by the title of the blog entries:

SELECT * FROM node WHERE node_type='blog' ORDER BY title;

Specifying that only three records are desired would be:

SELECT * FROM node WHERE node_type='blog' ORDER BY title LIMIT 0,3;

Finally, if there was another table, blog-topic, this table uses the same identifying value as the node table, nid (such as a driver's license number), and if we want to return its data along with the node data, we would relate the two records to each other, shown as follows:

SELECT * FROM node JOIN blog-topic ON node.nid=blog-topic.nid WHERE node_type='blog' ORDER BY title LIMIT 0,3;

Views does all that for you, as well as gives you many options to format its output to suit your needs.

Tip

The term View comes up in other places in computing, such as with SQL, but in the context of Drupal, it almost always refers to a dynamic display created with the Views module.

Views is a particularly versatile module, in terms of interactions with the developer, who will use it in any or all of the following three ways:

  • Via the UI (user interface) for creating views that are editable by the admin or other authorized users

  • From within a custom module, creating and/or invoking a view programmatically

  • Indirectly, using modules that themselves create programmatic views

Views offers many of the tools necessary for meeting your needs:

  • Template hints and model templates

  • Several types of default views

  • Various display types to meet the needs of the layout such as page, block, and attachment

  • A number of output formats such as tabular, grid, and list

  • Hooks

  • Pluggable features such as handlers and formatters

  • Instantaneous AJAX previewing

It is no wonder that Views is consistently the most popularly downloaded module at Drupal.org!

What this book covers

Chapter 1, Modifying Default Views, gives an introduction to the Views UI by modifying some of the views that come with the module in order to make useful versions of them.

Chapter 2, Basic Custom Views, covers creating elementary views and how to get them to provide the information you need.

Chapter 3, Intermediate Custom Views, goes beyond the basics to introduce concepts such as presenting teasers for a specific type of content, adding a header and footer, using AJAX for page changes, and producing custom links.

Chapter 4, Creating Advanced Views, covers advanced topics such as the use of multiple displays, using dynamic filters with depth, and restricting access to Views.

Chapter 5, Intermediate Custom Theming Views, shows you the various ways to manipulate the output of a view so that it has the look that you need.

Chapter 6, Creating Views Programmatically, shows how to create a view from within the module code rather than using the UI.

Chapter 7, Views Administration, covers some of the tools for administering your Views environment.

Appendix A, Installing Views, provides instructions for installing the Views module.

Appendix B, Entity Types and Fields, gives instructions for creating the various content types and other Drupal elements used in the recipes.

What you need for this book

You will need a reasonably advanced computer and an Internet connection. All software required to do the recipes can be freely obtained from drupal.org.

Who this book is for

This book is for developers or technically proficient users who are fairly comfortable with the concepts behind websites and the Drupal environment.

Conventions

In this book, you will find a number of styles of text that distinguish between different kinds of information. Here are some examples of these styles, and an explanation of their meaning.

Code words in text are shown as follows: "However, we do not want to use that argument, because we will not be retrieving content based on the nid in the attachment, we will be retrieving content based on tid."

A block of code is set as follows:

<style type="text/css">
#cc-container {
  width: 180px;
}
.cc-odd, .cc-even {
  padding: 6px;
  border: 4px solid black;
  width: 120px;
  position: relative;
  text-align: center;
}
.cc-odd {
  left: 0;
  background-color: #aaa;
}
.cc-even {
  left: 60px;
  background-color: #eee;
}
.cc-value {
  font-size: 36px;
}
</style>

When we wish to draw your attention to a particular part of a code block, the relevant lines or items are set in bold:

<?php foreach ($rows as $id => $row): ?>
  <div class="cc-<?php echo ($ctr % 2) ? 'odd' : 'even'; ?>">
      <?php $ctr--; ?>
      <div class="cc-value"><?php echo $ctr; ?></div>
      <div class="<?php print $classes_array[$id]; ?>">
        <?php print $row; ?>
      </div>
  </div>
<?php endforeach; ?>

New terms and important words are shown in bold. Words that you see on the screen, in menus or dialog boxes for example, appear in the text like this: " Click on the Title link in the Title box and change the title to Recent article comments, and then click on the Update button."

Note

Warnings or important notes appear in a box like this.

Tip

Tips and tricks appear like this.

Reader feedback

Feedback from our readers is always welcome. Let us know what you think about this book—what you liked or may have disliked. Reader feedback is important for us to develop titles that you really get the most out of.

To send us general feedback, simply send an e-mail to , and mention the book title through the subject of your message.

If there is a topic that you have expertise in and you are interested in either writing or contributing to a book, see our author guide on www.packtpub.com/authors.

Customer support

Now that you are the proud owner of a Packt book, we have a number of things to help you to get the most from your purchase.

Downloading the example code

You can download the example code files for all Packt books you have purchased from your account at http://www.packtpub.com. If you purchased this book elsewhere, you can visit http://www.packtpub.com/support and register to have the files e-mailed directly to you.

Errata

Although we have taken every care to ensure the accuracy of our content, mistakes do happen. If you find a mistake in one of our books—maybe a mistake in the text or the code—we would be grateful if you would report this to us. By doing so, you can save other readers from frustration and help us improve subsequent versions of this book. If you find any errata, please report them by visiting http://www.packtpub.com/support, selecting your book, clicking on the errata submission form link, and entering the details of your errata. Once your errata are verified, your submission will be accepted and the errata will be uploaded to our website, or added to any list of existing errata, under the Errata section of that title.

Piracy

Piracy of copyright material on the Internet is an ongoing problem across all media. At Packt, we take the protection of our copyright and licenses very seriously. If you come across any illegal copies of our works, in any form, on the Internet, please provide us with the location address or website name immediately so that we can pursue a remedy.

Please contact us at with a link to the suspected pirated material.

We appreciate your help in protecting our authors, and our ability to bring you valuable content.

Questions

You can contact us at if you are having a problem with any aspect of the book, and we will do our best to address it.