Book Image

Mastering Drupal 8 Views

By : Gregg Marshall
Book Image

Mastering Drupal 8 Views

By: Gregg Marshall

Overview of this book

Learn how to build complex displays of content—all without programming. Views were used on more than 80% of all Drupal 7 sites; now they are part of the Drupal 8 core. While most site builders and site owners are aware of views, they don't understand how to take full advantage of their power to create many amazing pages and blocks. If they use views, they might build 10 different view displays with different filters, without knowing that a contextual filter would require only a single display. Using our sample company, we'll take its existing content and evolve an ever more complex and powerful website for that company, starting with adapting the administration the user sees and moving on to making complex pages of information for site visitors. While the book is written for Drupal 8, the similarities between Views in Drupal 7 and 8 make this a useful reference for Drupal 7 site builders also.
Table of Contents (20 chapters)
Mastering Drupal 8 Views
Credits
Foreword
About the Author
Acknowledgements
About the Reviewer
www.PacktPub.com
Preface
Index

Preface

To make this book a little easier to read, I use a story of a Drupal user who has advanced to the point of being able to do their own site building. Setting the stage for the story woven into the book, let's start with a bit of background for the story.

Blue Drop Realty started business in 2002 as a part-time "hobby" of the owner, Lynn. As their business grew, they decided to build a website in 2005 using Dreamweaver to lay out their pages. This worked okay for about 5 years, but it was taking too much of Lynn's time to update the pages, and the site's design was starting to look dated. The business had grown enough that they had a new website developed by Fancy Websites, Inc., who used Drupal 6 to build the new website. With Drupal, the office manager and even some of the salespeople were able to make changes to the pages.

Lynn is joined in this story by her grey tabby cat, Jackson, an alley cat, literally, who adopted Lynn's family a couple of years ago. Once Jackson got comfortable with the new surroundings and being around a family, he frequently would curl up on Lynn's desk next to her monitor when she would work at night. Lynn has gotten into the habit of talking to Jackson as she works—he has proven to be a good listener.

Their Drupal 6 website is still working fine for Blue Drop Realty, but knowing that when Drupal 8 is released, support for Drupal 6 will end shortly afterward, Lynn is considering options to upgrade to the latest version of Drupal. She has gotten comfortable with Drupal 6 and even makes some "programming" changes to the site using the Drupal user interface as a site administrator. Lynn was able to add a news release content type herself, but she had to have Fancy Websites, Inc. create the views to display a list of releases on a news page. As she worked with Drupal, Lynn learned how to modify some views and even build a relatively simple list view on her own.

Jim is the developer at Fancy Websites, Inc., which built Lynn's original Drupal 6 site. As Lynn learns and becomes more comfortable with Drupal, Jim will morph from the developer who built the site to Lynn's mentor, as she herself will grow from a new content editor to site builder. More than anything else, Jim will help Lynn whenever she gets stuck.

A short history

Drupal is an open source content management system used by over 1,200,000 websites. It started as a message board written by Dries Buytaert in 2000 while in college at University of Antwerp. The original site Dries built was drop.org, a mistake when he went to register dorp.org, dorp being Dutch for village. When he decided to open source the software in January 2000, he named it Drupal.

Views has been described as the report writer for Drupal. It is that and a whole lot more.

Drupal progressed rapidly through many revisions, with Drupal 4 released in June 2002, Drupal 5 in January 2007, Drupal 6 in February 2008, and Drupal 7 in January 2010. Drupal 8, which is a major rearchitecting of Drupal, has taken almost 5 years, a third of Drupal's total lifetime, to be completed.

In May 2006, a contributed CCK module, Content Construction Kit, was released for Drupal 4.7 and became the standard for defining fields via the user interface, replacing the older Flexinode module, which dates back to February 2004. Before this, any modifications to the basic structure of a node would require manually defining database tables for a field and creating all the functions to create, edit, display, and delete the field when the corresponding node is changed or viewed.

The Views module was first created (at least the first commit was made to it) on November 25, 2005. The first release, 4.6.x-1.x-dev, was on December 1, 2005. The first non-development release, 4.7.x-1.0, was almost a year later on November 11, 2006. The first release contained 3,177 executable lines of PHP. The current Drupal 7 release of Views contains 57,155 lines of non-comment PHP. Its supporting module, CTools, contains another 39,939 lines. The Drupal 8 version of Views contains 58,925 lines, with most of CTools absorbed into Drupal core. Just as telling is that Drupal 8 Views has 19,921 lines of tests, where Drupal 7 only has 9,238 and Drupal 4.7 has none.

Before the Views module, any display not contained in the Drupal core or a contributed module required manually creating the necessary SQL queries, executing them against MySQL (at the time the only database supported by Drupal), and then taking the results and formatting them into HTML. This required significant PHP and MySQL programming experience. With the release of Views, users with limited programming experience could create powerful displays from the user interface.

At the time of writing, Views for Drupal 7 has almost 600 supporting contributed modules. As of the release of Drupal 8, Views for Drupal 8 has 65 supporting contributed modules, but many module contributors are just starting to port modules to Drupal 8, so the number will rise dramatically in the months to come.

Drupal is always changing

This book was started just as Drupal 8.0.0 was about to be released and was finished shortly before Drupal 8.1.0 was about to be released. In between were 8.0.1 through 8.0.5, a new release about every two weeks. With each release, a number of issues were resolved. Some of those issues affect how Views operates or the wording of some screen text or prompts. The evolution of Drupal and Views will continue long after this book is done. As a result, it is very likely that some screens and prompts described here might not match your copy of Drupal exactly. The differences are minor changes in wording to improve the understandability or clarify the meaning. Do not be alarmed if there are small differences between this book and Views when you use it. These differences will be mostly cosmetic, although it is possible that a new option might appear.

What this book covers

The first thing you will notice is that this isn't your traditional technical book written in a dry third-party tone. It reads a bit more like a novel, following Lynn as she learns how to master Views. Jackson, her cat, and Jim, her mentor, are along for the ride and to let the story change viewpoints from time to time (and to give Lynn "people" to talk to).

Views is an amazing part of Drupal 8. I hope you will enjoy this approach to explaining it.

Chapter 1, Up and Running with Views, deals with the Views main administration page and setting pages and does a quick run-through of View's edit screen by modifying an existing core view.

Chapter 2, Views from Scratch, starts the in-depth exploration of Views, starting with building a simple property listing that most real estate sites might have. We will define some Views terms such as View Types, displays, Display Formats, and Display Contents. We'll end with an interesting view that displays selected events from Drupal's watchdog log to users that might normally not be able to see them.

Chapter 3, Sorting and Filtering, introduces sorting and filtering. Sorting lets you specify the order you want your results to be displayed in. We will move on to limiting which results are even displayed using filtering. You can also expose a filter to the user and let them select which results to show. Finally, we'll show how to group the results when a view results in multiple rows for each value.

Chapter 4, Contextual Filters, takes filtering to the next level by adding contextual filters. Contextual filters use the URL to pass the selection criteria to the filter.

Chapter 5, Relationships, shows how to use relationships to gain access to even more data to display. Using the built-in entity reference, any reference field can be used to add all the fields in the referenced content to the available fields list.

Chapter 6, Add-on Modules, adds more Display Formats using add-on contributed modules. Using add-on modules, we can add a slideshow or a rotating carousel as easily as enabling the module, installing the JavaScript library it uses, and defining a very simple view.

Chapter 7, Field Rewrites, is about field rewrites. It is uncommon that the default output of a view exactly meets your needs, including the if-then-else values, in which if a condition is true, one value is displayed, but if it isn't true, another value is displayed.

Chapter 8, Customizing Views, covers the rest of the options in the center column of the view edit page, such as custom headers, footers, and special messages if a set of filters (defined, exposed, or contextual) results in no results.

Chapter 9, Advanced View Settings, deals with all of the advanced settings except contextual filters and relationships, which are covered in earlier chapters.

Chapter 10, Theming Views, winds up the book with how to theme or style the output of Views. Some CSS support can be easily added through the user interface; other, more complex changes might require replacing the Twig templates used by Views with custom templates that do manipulations not possible from the user interface.

What you need for this book

Throughout the book, we have used Google Chrome as our browser. You can use any modern web browser and text editor, but I highly recommend you use this open source software to make any of the projects discussed in this book.

To follow along and try these Views, you will need a functioning installation of Drupal 8. Acquia Dev Desktop is an easy way to install Drupal on Windows or Mac (https://www.acquia.com/downloads). Alternatively, you can use WAMP or MAMP to install Drupal 8 locally. Or, you can also use free hosting at Acquia Cloud (https://www.acquia.com/free) or Pantheon (https://pantheon.io/).

For quick experimenting, https://simplytest.me/ offers free sites that can be created in a few minutes and last up to 24 hours. For other environments, take a look at the Drupal 8 requirements at https://www.drupal.org/requirements.

Who this book is for

This book is for web developers, web designers, and website administrators who use Drupal 8, have some basic knowledge of managing and developing apps with Drupal, and want to get an advanced, practical knowledge of Views and how to leverage them in Drupal 8 applications.

Given that Drupal 8's version of Views is a port of the Drupal 7 version (Views 7.x-3.x), much, if not most, of the book is just as applicable to Drupal 7. The user interface is virtually identical, but some options have more choices.

Conventions

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

Code words in text, database table names, folder names, filenames, file extensions, pathnames, dummy URLs, user input, and Twitter handles are shown as follows: "Each row is wrapped in <LI> tags."

A block of code is set as follows:

SELECT node_field_data.title AS node_field_data_title, node_field_data.nid AS nid
FROM 
{node_field_data} node_field_data
WHERE (( (node_field_data.status = '1') AND (node_field_data.type IN  ('open_house')) ))
ORDER BY node_field_data_title DESC
LIMIT 10 OFFSET 0

New terms and important words are shown in bold. Words that you see on the screen, for example, in menus or dialog boxes, appear in the text like this: "She clicked on the Apply (all displays) 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 disliked. Reader feedback is important for us as it helps us develop titles that you will really get the most out of.

To send us general feedback, simply e-mail , and mention the book's title in 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 at 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 color images of this book

We also provide you with a PDF file that has color images of the screenshots/diagrams used in this book. The color images will help you better understand the changes in the output. You can download this file from https://www.packtpub.com/sites/default/files/downloads/MasteringDrupal8Views_ColorImages.pdf.

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 could 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/submit-errata, 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.

To view the previously submitted errata, go to https://www.packtpub.com/books/content/support and enter the name of the book in the search field. The required information will appear under the Errata section.

Piracy

Piracy of copyrighted 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

If you have a problem with any aspect of this book, you can contact us at , and we will do our best to address the problem.