Mastering Drupal 8 Views - Gregg Marshall - E-Book

Mastering Drupal 8 Views E-Book

Gregg Marshall

0,0
39,59 €

-100%
Sammeln Sie Punkte in unserem Gutscheinprogramm und kaufen Sie E-Books und Hörbücher mit bis zu 100% Rabatt.

Mehr erfahren.
Beschreibung

Accelerate your development process using Drupal 8 Views with this advanced, practical guide

About This Book

  • A practical, step-by-step guide with illustrative examples to give you a clear understanding of Drupal 8 views
  • Get the only book on the market that talks about Drupal 8 views and their importance for developers
  • Get an understanding of how views work and how they help to create modular applications

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 who want to get an advanced, practical knowledge of views and how to leverage them in Drupal 8 applications.

What You Will Learn

  • Create displays of content that are automatically updated when you add new content
  • Show maps and rotating image carousels on your site
  • Combine content to create composite displays using the same data in different ways
  • Use fields from more than one content type to create powerful views of multi-table data
  • Modify the field data being displayed, combining it in different ways or changing the HTML that might be normally generated
  • Add headers and footers above and below a views display
  • Make the output of your view look exactly like the designer specifies
  • Change the order in which a list of content is displayed, limit the content shown to only certain content types or values
  • Filter content using values passed to the view in the URL, making a single view work different ways

In Detail

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.

Style and approach

This is the only advanced resource that will show you how to create, customize, and manage views in the Drupal 8 development environment. This has become more important since Drupal 8 has made views part of its core architecture.

Sie lesen das E-Book in den Legimi-Apps auf:

Android
iOS
von Legimi
zertifizierten E-Readern

Seitenzahl: 355

Veröffentlichungsjahr: 2016

Bewertungen
0,0
0
0
0
0
0
Mehr Informationen
Mehr Informationen
Legimi prüft nicht, ob Rezensionen von Nutzern stammen, die den betreffenden Titel tatsächlich gekauft oder gelesen/gehört haben. Wir entfernen aber gefälschte Rezensionen.



Table of Contents

Mastering Drupal 8 Views
Credits
Foreword
About the Author
Acknowledgements
About the Reviewer
www.PacktPub.com
eBooks, discount offers, and more
Why subscribe?
Preface
A short history
Drupal is always changing
What this book covers
What you need for this book
Who this book is for
Conventions
Reader feedback
Customer support
Downloading the color images of this book
Errata
Piracy
Questions
1. Up and Running with Views
Drupal 8 is here, should I upgrade?
Looking at the Views administration page
Views settings
Views advanced settings
Views basic settings
Live Preview Settings
Adapting an existing View
Editing the Property Maintenance view
Debugging – Live Preview is your friend
Summary
2. Views from Scratch
A first try at listing the available properties
Adding a new view
New page settings
Adding fields
The anatomy of a view
View Types
Content
Content revisions
Comments
Log entries
Files
Taxonomy terms
Users
Custom blocks
Custom block revisions
Other View Types
Displays
Master
Page
Block
Attachment
Embed
Entity reference
Feed
REST export
Display Formats
The unformatted list
The HTML list
Grid
Table
Display Contents
Fields
Content/entity
Search results
Showing the Watchdog logs
Adding the Views
The view edit page
Summary
3. Sorting and Filtering
Changing the sort order
Deleting a sort criteria
Adding a sort criteria
Multilevel sorts
Grouping
Exposed sort fields
Filtering results
The "To Be Published" View for administrators
Filtering by node status
Filtering by content type – compound filters
Filtering by field value
Exposed filters – giving power to your visitors
Exposed filter options
Summary
4. Contextual Filters
Contextual filters are filters in a different place
Adding an image map linked to neighborhood listings
Adding a contextual filter
Finishing the image map
Using default values to "create" options
Adding a block view for unpublished properties
Default filter value options
Finishing up the view and testing
Contextual filter options explained
Options for no value
Default value options
Default value exception options
Value present options
Validation options
Non-permissions-based validation options
Permissions-based validation options
Options inside the More fieldset
Numeric fields
String fields
Glossary mode
URL transformation
Multiple values
Role and taxonomy field options
Null fields
Summary
5. Relationships
Fixing the neighborhood property listing title
Adding a relationship
Relationships often add new fields
What are relationships?
Blue Drop website relationships
Relationship "direction"
Reverse relationships
Automatic reverse relationships
Adding realtor information to the property listings
Adding new fields
Debugging when you forget to specify the relationship
Adding an open house attachment display to the property listings view
Defining what to attach a view to
Adding the relationship
Cleaning up what is displayed
Adding Open House fields to display
Adding exposed filters to the attachment
Other relationship considerations
Picking the right View Type
Lynn's next assignment
Summary
6. Add-on Modules
Adding a rotating carousel to the front page
Install the Views Slideshow Module
Add a new view
Add fields
Adding a filter
Debugging slide rotation
Installing the jquery.cycle library
jquery.cycle options
Mapping content
Creating a view for the map
Adding the geolocation field
Doing away with pagers – creating an infinite page
Creating an infinite scroll property image gallery
Expandable accordions hide and show content
Putting one View inside another
Creating the child view
Creating the parent view
Debugging the configuration
Adding a calendar of open houses
Installing the Calendar and Views Templates modules
Adding a calendar view
Adding a relationship to properties
Final view adjustments
Summary
7. Field Rewrites
Linking to anything
Introducing field rewrites
Linking to content
Excluding from display
Field render as URL options
Changing how fields are output
Open house formatting
Property map enhanced bubble displays
Adding fields to the display
Rewriting several fields into one
Using HTML to format the rewritten field
If-then-else conditional rewrites
No results options
Starting at the end
Going beyond two levels
Other uses for field rewrites
Summary
8. Customizing Views
Display settings
Attachment
Block
Embed
Entity Reference
Feed
Page
REST export
Header, Footer, and No Results Behavior
Pager
Replacing Drupal 7's mini pager
Pager options
Adding No Results and Footer to Available Property Listings
Summary
9. Advanced View Settings
Becoming advanced
Revisiting Header/Footer options
Contextual filters and relationships
Advanced options
Exposed form in block
Using AJAX to update pages
Hiding attachments and enabling contextual links
Changing the SQL queries generated by views
Query settings
Reading from a secondary database server
Developer options
Using aggregation
Aggregation type
Grouping
Making Views go fast
Caching types
Dinner plans
Summary
10. Theming Views
Time to theme
Designing mock-ups for the new site
Changing generated HTML and CSS from the Views UI
Structure of a View's output
The entire View CSS settings
Theme debugging
Row CSS
Field HTML and CSS
When the HTML code needs to change
A quick review of the Drupal 8 theming changes
Drupal 7's PHPTemplate version of views-view.tpl.php
Drupal 8's Twig version of views-view.tpl.php
Twig variables
Enabling theme/Twig debugging
Views templates
Template suggestions
Going live and accolades
Summary
A. Content Types for a Sample Site
Property
Property Owner
Realtor
Open House
County Taxonomy
Neighborhood Taxonomy
Index

Mastering Drupal 8 Views

Mastering Drupal 8 Views

Copyright © 2016 Packt Publishing

All rights reserved. No part of this book may be reproduced, stored in a retrieval system, or transmitted in any form or by any means, without the prior written permission of the publisher, except in the case of brief quotations embedded in critical articles or reviews.

Every effort has been made in the preparation of this book to ensure the accuracy of the information presented. However, the information contained in this book is sold without warranty, either express or implied. Neither the author nor Packt Publishing, and its dealers and distributors will be held liable for any damages caused or alleged to be caused directly or indirectly by this book.

Packt Publishing has endeavored to provide trademark information about all of the companies and products mentioned in this book by the appropriate use of capitals. However, Packt Publishing cannot guarantee the accuracy of this information.

First published: May 2016

Production reference: 1180516

Published by Packt Publishing Ltd.

Livery Place

35 Livery Street

Birmingham B3 2PB, UK.

ISBN 978-1-78588-696-6

www.packtpub.com

Credits

Author

Gregg Marshall

Reviewer

Adrian Ababei

Commissioning Editor

Amarabha Banerjee

Acquisition Editor

Reshma Raman

Content Development Editor

Sachin Karnani

Technical Editor

Taabish Khan

Copy Editors

Shruti Iyer

Sonia Mathur

Project Coordinator

Nikhil Nair

Proofreader

Safis Editing

Indexer

Rekha Nair

Graphics

Jason Monterio

Production Coordinator

Manu Joseph

Cover Work

Manu Joseph

Foreword

Without Views, Drupal wouldn't be Drupal. It wouldn't be powering more than 1 million websites*—5% of all websites that have an identifiable CMS**—and there wouldn't be more than 35,000 developer accounts on Drupal.org***.

There are three keys to building dynamic, content-first websites. The first is the ability to model semantic data structures (Content Construction Kit used to be a contributed module; now, it has become entities and fields in Drupal core). The second is selecting what content you want based on any given criteria (part of Views is a database query-building UI). And finally, displaying it how and where you want (the other part of Views is a set of configurable display options ranging from HTML lists and tables to RSS feeds to web service endpoints and more).

Does this sound complicated? Views can be daunting for the first-time user. But in the hands of an experienced Drupal expert, the power and flexibility of Views brings some of the world's most important and interesting websites to life. Another consequence of its complexity is how difficult it has been for me to explain its use to others, even to figure out where to start.

After reading this unusual technical book, it is clear to me that Gregg Marshall has lived and breathed Drupal and Views for many years. This is not just another dull technical manual full of lists of dry facts. Gregg has lived up to the challenge of explaining Views, and not only does he do that, but he also shares his knowledge and experience in an engaging way. He's tied together a collection of useful exercises with the fictitious story of a Drupal service provider, a Drupal client, and her cat.

That evening Lynn logged into the new site. Clicking on the Manage menu item, she clicked on the Structure submenu item, and at the bottom of the list displayed on the Structure page, she clicked on the Views option.

About that time Jackson came in and settled into his spot near her terminal. "Hi Jackson, ready to explore Views with me?"

Looking at the Views administration page, Lynn noticed there were already a number of Views defined. Scanning the list, she said "Look, Jackson, Drupal 8 uses Views for administration pages. This means we can customize them to fit our way of doing things. I like Drupal 8 already." Jackson purred. Lynn studied the Views administration page.

The exercises cover the myriad facets of the Views interface and options and contain tips that clearly come from Gregg's practical experiences in using this tool. The story elements worked well as a vehicle to keep me engaged with the exercises. They also help link situations familiar to Drupal service providers and clients to the practical use of Views to solve them.

Views has been one of Drupal's killer apps since its introduction; it was the most installed contributed module—at 900,000 reported installs****—before it was merged into the core. Drupal 8 was released in November 2015 with a slew of new features, refinements, and lots of tricks up its sleeve. These tricks in Drupal's core include comprehensive multilingual capabilities, improved accessibility, user interfaces to build RESTful APIs and web services, structured semantic data throughout, mobile-first architecture, and plenty more, including Views built right into the Drupal core!

Thank you, Gregg!

Jeffrey A. "jam" McGuire

Evangelist, Developer Relations, Acquia

*https://www.drupal.org/project/usage/drupal

**http://w3techs.com/technologies/details/cm-drupal/all/all

***https://en.wikipedia.org/wiki/Drupal#Community

****https://medium.com/@tacopotze/some-facts-on-the-top-5-000-drupal-modules-e4d685adc081#.r5x6mutd0

About the Author

Gregg Marshall has been using Drupal since 2006. While he installed Drupal 5, his first production site was Drupal 6. He started his career as a software engineer, switched to sales/marketing in an unrelated industry, and drifted back to development. After attending DrupalCon 2010 in San Francisco, Gregg switched from channel marketing that used Drupal for microsites to being a full-time Drupal developer.

Gregg works as a consultant/contractor, frequently mentoring in-house development teams on Drupal best practices. His current contract is with WebNY, where he serves as the senior Drupal architect for the state of New York, helping build a single platform for all the state's websites.

Gregg is a speaker at Drupal Camps and is a professional speaker to 30+ associations and corporate meetings. He has over 400 published articles in a variety of publications. Gregg has a bachelor's degree in chemistry, a master's degree in electrical engineering, and a master's degree in business administration. He is an Acquia Certified Drupal Grand Master.

Acknowledgements

To my wife, Lorraine, who has been completely supportive of writing this book while also commuting to New York to work, and my children, Christopher, Patrick, and Amanda, who have stepped in to free up my time.

I would like to thank the team at WebNY, who have been great to work with as my two month contract enters its fourth year. I would like to acknowledge especially Jennifer Warner, Meredith Case, and Jim Kavanaugh, who encouraged me in this endeavor.

A special thank you to the technical reviewer, Adrian Ababei, who has offered a ton of great suggestions and pointed me to a number of Views options I hadn't noticed before.

Finally, a thank you to Dries Buytaert for Drupal and Earl Miles for Views; obviously, without them, this book would be empty.

About the Reviewer

Adrian Ababei is a senior Drupal developer and architect with over 14 years of experience in designing, architecting, implementing, and supporting interactive websites, applications, and solutions. He has more than 11 years of experience in working with Drupal alone.

During the last 5 years, he was the lead Drupal developer and technical director for a Toronto-based Drupal shop, AllWeb247.

I would love to be part of another Packt project and review another Drupal book. I would like to thank my family for their support and love: George, Alina, mom, dad, and Camy.

www.PacktPub.com

eBooks, discount offers, and more

Did you know that Packt offers eBook versions of every book published, with PDF and ePub files available? You can upgrade to the eBook version at www.PacktPub.com and as a print book customer, you are entitled to a discount on the eBook copy. Get in touch with us at <[email protected]> for more details.

At www.PacktPub.com, you can also read a collection of free technical articles, sign up for a range of free newsletters and receive exclusive discounts and offers on Packt books and eBooks.

https://www2.packtpub.com/books/subscription/packtlib

Do you need instant solutions to your IT questions? PacktLib is Packt's online digital book library. Here, you can search, access, and read Packt's entire library of books.

Why subscribe?

Fully searchable across every book published by PacktCopy and paste, print, and bookmark contentOn demand and accessible via a web browser

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.

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 <[email protected]>, 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 <[email protected]> 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 <[email protected]>, and we will do our best to address the problem.

Chapter 1. Up and Running with Views

Drupal 8 was released on November 19, 2015, after almost 5 years of development by more than 3,000 members of the Drupal community. Drupal 8 is the largest refactoring in the project's history.

One of the most important changes in Drupal 8 was the inclusion of the most popular contributed module, Views. Similar to including CCK in Drupal 7, adding Views to Drupal 8 influenced how Drupal operates, as many of the administration pages, such as the content list page, are now views that can be modified or extended by site builders.

Every site builder needs to master the Views module to really take advantage of Drupal's content structuring capabilities by giving site builders the ability to create lists of content formatted in many different ways. A single piece of content can be used for different displays, and all the content in each view is dynamically created when a visitor comes to a page. It is so important, it was the only contributed module included in the Acquia Site Builder certification examination for Drupal 7.

In this chapter, we will discuss the following topics:

Looking at the Views administration pageReviewing the general Views module settingsModifying one of the views from Drupal core to create a specialized administrative page

Note

Drupal is always changing

This book was started just after Drupal 8.0.0 was released and has been finished shortly before Drupal 8.1.0 is about to be released. In between are 8.0.1 through 8.0.5, a new release about every two weeks. With each release, a number of issues are resolved. Some of those issues affect how Views operate 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 a new option might appear.

Drupal 8 is here, should I upgrade?

"Jim, this is Lynn, how are things at Fancy Websites?"

"Hi Lynn, things are going well."

Lynn continued, "I read that Drupal 8 is being released on November 19. From our conversations this year, I guess that means it is time to upgrade our current Drupal 6 site. Should I upgrade to Drupal 7 or Drupal 8?"

"Lynn, we're really excited that Drupal 8 is finally ready. It is a game changer, and I can name 10 reasons why Drupal 8 is the way to go":

Mobile device compatibility is built into Drupal 8's DNA. Analytics show that 32% of your site traffic is coming from buyers using phones, and that's up from only 19% compared to last year.Multilingual is baked in and really works, so we can go ahead and add the Spanish version of the site we have been talking about.There's a new theme engine called Twig that will make styling the new site much easier. It's time to update the look of your site; it's looking pretty outdated compared to the competition.Web services are built in. When you're ready to add an app for your customer's phones, Drupal 8 will be ready.There are lots of new field types built in for useful stuff such as telephone numbers, e-mail addresses, and more, so we won't need to add half a dozen contributed modules to let you build your content types.Drupal 8 is built using industry standards. This was a huge change you won't see, but it means that our shop will be able to recruit new developers more easily.The configuration is now stored in code. Finally, we'll have a way for you to develop on your local computer and move your changes to staging and then to production without having to rebuild content types and views manually over and over.The WYSIWYG editor is built in. The complex setup we went through to get the right buttons and make the output work won't be necessary in Drupal 8.There's a nice tour capability built in so that you can set up custom "how to" demonstrations for your new users. This should free up a lot of your time, which is good given how you are growing.I've saved the best for last. Your favorite module, Views, is now built into core! Between Fields in Drupal 7 and now Views in Drupal 8, you've got the tools to extend your site built right into core.

"The bottom line is I can't imagine not going ahead and upgrading to Drupal 8. Views in core is reason enough. Why don't I set up a Drupal 8 installation on your development server so that you can start playing with Drupal 8? We're not doing any development work on your site right now, and we still have staging to test any updates."

"That sounds great, Jim! Let me know when I can log in."

Less than an hour later, the e-mail arrived; the Drupal 8 development site was set up and ready for Lynn to start experimenting.

Based on the existing Drupal 6 site, Lynn set up four content types with the same fields she had on the current site. These content types are outlined in Appendix, Content Types for a Sample Site. Jim was able to use the built-in migrate module to move some of her data to the new site.

Lynn was ready to start exploring Views in Drupal 8.

Looking at the Views administration page

That evening, Lynn logged into the new site. Clicking on the Manage menu item, she then clicked on the Structure submenu item, and at the bottom of the list displayed on the Structure page, she clicked on the Views option.

About that time, Jackson came in and settled into his spot near her terminal. "Hi Jackson, ready to explore Views with me?"

Looking at the Views administration page, Lynn noticed there were already a number of Views defined. Scanning the list, she said "Look Jackson, Drupal 8 uses Views for administration pages. This means we can customize them to fit our way of doing things. I like Drupal 8 already!". Jackson purred. Lynn studied the Views administration page shown here:

Views administration page

As Lynn looked at each view, the listing looked familiar; she had seen the same kind of listing on her Drupal 6 site. Trying the OPERATIONS pull-down menu on the first view, she saw that the options were Edit, Duplicate, Disable, and Delete. "That's pretty clear; I guess Duplicate is the same as Clone on my old version of Views. I can change a view, create a new one using this one as a template, make it temporarily unavailable, or wipe it completely off the face of the earth."

"I wonder what kind of settings there are on the Settings tab of this listing page. Look, Jackson, there's a couple of subtabs hiding on the Settings page." As Lynn didn't want to mess up her new Drupal site, she called Jim. "Hi, Jim. Can you give me a quick rundown on the Views Settings tab?"

"Sure," he replied.

Views settings

"Looking at the Views Settings tab, you'll notice two subtabs, Basic and Advanced. Select the advanced settings tab by clicking on Advanced to show the following display:

The Views advanced settings configuration page

Views advanced settings

Let's look at the Advanced tab first since you'll probably never use these settings.

The first option, Disable views data caching, shouldn't be checked unless you are having issues with views not updating when the data changes. Even then, you should probably disable caching on a per-view basis using the caching setting in the view's edit page in the third column, labeled Advanced, near the bottom of the column. Disabling views' data caching can really slow down the page loads on your site.

Tip

You might actually use the Advanced settings tab if you need to clear all the Views' caches, which you would do by clicking on the Clear Views' cache button.

The other advanced setting is DEBUGGING with an Add Views signature to all SQL queries checkbox. Unless you are using MySQL's logs to debug queries, which only an advanced developer would do, you aren't going to want this overhead added to Views queries, so just leave it unselected.

Views basic settings

Moving to the Basic tab, there are a number of settings that might be handy, and I'd recommend changing the default settings. Click on Basic to show the following display:

The Views basic settings configuration page

The first option, Always show the master (default) display, might or might not be useful. If you create a new view and don't select either create a page or create a block (or provide a REST export if this module is enabled), then a default view display is created called master. If you select either option or both, then page and/or block view displays are created; generally you won't see master. It's there; it's just hidden.

Sometimes, it is handy to be able to edit or use the master display. While I don't like creating a lot of displays in each view, sometimes I do create two or three if the content being displayed is very similar. An obvious example is when you want to display the same blog listing as either a page or in a block on other pages. The same teaser information is displayed, just in different ways. So, having the two displays in the same view makes sense.

Tip

Just make sure when you customize each display that any changes you make are set to only apply to the current display and not all displays. Otherwise, you might make changes you hadn't planned on in the other displays. Most of the time, you will see a pull-down menu that defaults to All displays, but you can select This page (override) to have the setting change apply only to this display.

Using the master display lets you create information that will be the same in all the displays you are creating; then, you can create and customize the different displays. Using our blog example, you may create a master display that has a basic list of titles, with the titles linking to the full blog post. Then, you can create a blog display page, and using the This page (override) option, you can add summaries, add more links, and set the results to 10 per page. Using the master display, you can go back and add a display block that shows only the last five blog posts without any pager, again applying each setting only to the block display. You might then go back to the master display and create a second block that uses the tags to select five blog posts that are related, again making sure that the changes are applied to the current block and not all displays.

Finally, when you want to change something that will affect all the displays, make the change on the master display, and this time, use the All displays option to make sure the other displays are updated. In our blog example, you might decide to change the CSS class used to display the titles to apply formatting from the theme; you probably want this to look the same in every possible display of the blog posts.

Note

CSS and theming is covered in Chapter 10, Theming Views.

The next basic setting for Views is Allow embedded displays. You will not enable this option; it is for developers who will use Views-generated content in their custom code. However, if you see it enabled, don't disable it; doing this would likely break something on your site using this feature.

The last setting before the LIVE PREVIEW SETTINGS fieldset is Label for "Any" value on non-required single-select exposed filters, which lets you pick either <Any> or -Any- as the format for exposed filters that would allow a user to ignore the filter.

Live Preview Settings

There are several LIVE PREVIEW SETTINGS fieldsets I like to enable because they make debugging your views easier. If the LIVE PREVIEW SETTINGS fieldset is closed (if these options are not showing), click on the title next to the arrow, and it will open. It will look similar to this:

LIVE PREVIEW SETTINGS

Tip

I generally enable the Automatically update preview on changes option. This way, any change I make to the view when I edit it shows the results that would occur after each change. Seeing things change right away shows me whether a change will have an effect I'm not expecting.

A lot of Views options can be tricky to understand, so a bit of trial and error is often required. Hence, expect to make a change and not see what you expect; just change the setting back, rethink the problem, and try again. Almost always, you'll get the answer eventually. If you have a view that is really complex and very slow, you can always disable the live preview while you edit the view by selecting the Auto preview option in the gray Preview bar just under all the view's settings.

The next two options control whether Views will display the SQL query generated by the Views options you selected in the edit screen.

Tip

I like to display the SQL query, so I will select the Above the preview option under Show SQL query and then select the Show the SQL query checkbox that follows it.

If you don't check the Show the SQL query option, it doesn't matter what you select for above or below the preview, and if you expect to see the SQL queries and don't, it is likely that you set one option and not the other. Showing the SQL query can be confusing at first, but after a while, you'll find it handy to figure out what is going on, especially if you have relationships (or should have relationships and don't realize it). And, of course, if you can't read the query, you can always e-mail me for a translation to English.

The next option, Show performance statistics, is handy when trying to figure out why a Views-generated page is loading slowly. But usually, this isn't an issue you'd be thinking of, so I'd leave it off. You want to focus on getting the right information to display exactly the way you want without thinking about the performance. If we later decide it's too slow, the developer we'll assign to it will use this information and turn the option on in development.

The same is true about Show other queries run during render during live preview