 Welcome. We'd like to thank you for attending this session. Mastering Drupal 8 Views. My name is Greg Marshall. I'm a digital technology developer manager or in more simple words, I'm a Drupal architect for Accenture Digital which is part of Accenture and technically inside that Accenture Interactive. I've been on Drupal.org since 2006 so roughly 10, 11 years. I went to my very first DrupalCon in 2010 in San Francisco. That's when I decided to switch from being a user of Drupal to a developer in Drupal. I happened to be a aquia grandmaster in both Drupal 7 and Drupal 8 and I wrote a book. Hi, my name is Amanda. I have a bachelor's degree in business and a master's certificate in energy and sustainability and when neither of those really helped me get a job, I ended up going to Drupal Easy Academy, my dad's suggestion. I've been a Drupal.org user since 2008 because I had previously tried my hand at Drupal a little, but then I decided to focus on school. So I initially got into Drupal in 2008 but I decided to focus on school because I thought it would be better for it to help me get a job and then ended up coming back to it later. My first DrupalCon was Baltimore. I'm an aquia certified site builder. I interned on a UN humanitarian response Drupal site. I'm working right now as a Drupal developer at Brad Jones LLC but I'm looking for something a little more four times so if you know of anything let me know. This is Jackson. He's one of the characters in my dad's book. Okay because I work for a large company we have the standard legal disclaimer. Basically what this says is I am not here on behalf of Accenture. I am here as an individual and they take no responsibility for anything I might say. So to put this talk into place and sort of set the stage for it as a family we get together whenever the kids are in town and we'll tend to go out to a restaurant and have breakfast. And normally at those breakfasts if it were before last fall when Amanda started Drupalese Academy we would talk about sewing or crafts or travel or cooking or things like that. This particular breakfast was in November last year and my oldest son and his new wife were down visiting from Wisconsin and we're at this restaurant and Amanda and I are sitting at one end of the table debugging a problem she's having with a Git repository. And at that point at this family breakfast it dawned on me family meals will never be the same again because all of a sudden we have something to talk about. So what we decided to do just for fun is take that conversation and sort of adapt it to views and talk about views like we were having breakfast at Sam's number three. So Amanda how's that project with you ongoing? It's going great. We're getting deeper into Drupal theming. I've been able to build a few views. I thought I was really understanding views from my class but I'm still struggling with the terminology. Terminology is always a problem with Drupal. Drupal tends to overload most terms so we have the same word will have different meanings depending on what context it's in. So let's set the stage with some basic words. Views is the views module which in Drupal 8 is part of core but before Drupal 8 was actually its own separate project and the way to think about views as a module is it creates dynamic lists so it's a list builder for Drupal. Those of you that have been in computing long enough can think of it as the crystal reports for Drupal. So crystal reports was a report writer back in the days of Debase and other databases and then when you talk about view in the singular you typically are talking about a single one of these lists. As an example of something you would do with views think about the case where you want to display the five most recent blog posts as a block on your website. So if you did it manually what you would have to do is you would have to add the blog post, publish the blog post, then go back to the block, edit the block, add the title and link deleting the title and link of the oldest blog post and that would give you the five most recent and you would have to do those two steps every time you did something. With views what you can do is add the blog post and auto-magically the block will update itself with the five most recent blog posts. So you get this kind of dynamic list building that becomes so important to Drupal. Views was the number one contributed module for Drupal 7. So if you looked at the list of modules the module that was most used in almost every website about 80% of the websites was views. So when it came into core that was a very natural thing. I think it would have probably gone into Drupal 7 except it's so big that it was hard to get it integrated in time. But now with it being part of core the front page and many of the Drupal administration pages are now views. So if you look at it the front page itself is just a simply a view. The content list editing page is a view. The content list maintenance page. The files view is a view. The online is actually a view. The people list so the user maintenance screen that's part of Drupal is now a view. And what that means to us as developers and site builders is that literally any one of those can be modified and adapted to be specifically what you want. Once you get down to a single view then you have to think about the parts. So there are some components to a view that sort of work from the top down or from the biggest part. The baser view type is what you're building a view of. Then you have a display within that baser view type. Within that display you can pick a format and then you have a display and then you have a display type within that and then you have filters and sorts which then let you make your view more. So if you look the view edit screen is very complex and it's complex because it does so much. So if we look at it in three sections basically the first part is the fields we're going to display the filters and the sort criteria and how we're going to display it whether it's got a title. At the very top it says it's a block so that's the display itself and then the fact that it's an unformatted list is the display type. In the middle you have block settings or page settings if it's a page something general to that part of it then almost every view has a header, a footer and a no results option and these are areas where you can figure what happens either above, below or if nothing is going to come out of the query that you've built that goes with that view. And finally views comes with built in paging. So if you have a lot of data and you don't want to display it all in one page 10,000 records you can display them by default 10 at a time and it'll just let you go forward backwards the first the last depending on which pager you use. And then finally on the right you've got the advanced section which in a normal views installation is by default closed. One of the recommendations I make is that you change the settings, your views settings off the views listing page to always display the advanced section because there are two parts of it that really you want to use. One is contextual filters and the other is relationships and we'll come back to that. The rest of the advanced truly is advanced and very frequently you'll find you won't use but those two contextual filters and relationships you do use. So Amanda does that help? Yes it does thank you. Okay so let's you're still doing a side project for a real estate company aren't you? Yes. Okay so tell you what let's show everyone how you can adapt a view. Let's take an existing content list view and we want to adapt it to be a view that's displayed when a real estate agent logs into that website and is given only their properties that they are allowed to maintain. And we're only going to let them edit. We're not going to let them delete or do a lot of the other things it would do. So oh this is what I want it to look like when you're done. So I want just the address, the status, the updated and none of that extra clutter. So my first step is to duplicate the content maintenance view. I don't want one little mistake to screw up my whole project. Then under the then I change the title to property maintenance by clicking on the word content under title. Under the field section I remove the bulk operation form by clicking on the field name, clicking on the remove link at the bottom. I also remove the content type and the author user fields. Since this view will only display the current user's properties. I also remove the translation language filter since that site only has English. Finally I change the content type filter from being exposed and being fixed and selecting the property content type. It's important to save your changes so that they're permanent. This shows the before and after the edits. That was relatively easy but a fresh copy of Drupal only has two content types. Basic page and article and they don't have many fields. Why is views around? Is it only for administration screens? Good question. Fuses around because of structured content. So in Drupal 7 we added fields module. Before that it was called cck but it gave you ability to structure your content and add lots of different fields. Something else that's unique in Drupal 8 is that relationships are built in. So in Drupal 7 you had to add the entity relationship module or entity reference module or the node reference module in order to have one content type referred to a different content type. In Drupal 8 that's just built in as part of it. So you have different kinds of fields. You can have text fields, you can have number fields, boolean fields, file fields, image fields or reference fields to other nodes. Then through contributed modules you can add different kinds of field types. One of the ones that I see frequently and I know you're using on your real estate site is a geolocation field which allows us to put longitude and latitude on that website in order to do that. So if you look at the data structure for the real estate website you're working on, it's very complex and to the audience I know you can't read that, it isn't intended to be read but basically what it says is there are a lot of fields, there are different types and basically there are four kinds. There's a property type, there's an owner or a realtor, they're similar but different and there's an open house type. And if I actually drew a map of how these all go together, you've got the property type in the middle, it's being referred to by an open house and the direction of these arrows will become important later. It uses realators, it also has property owners and it can have more than one of each so ten people can own a property together or you can have two realators help sell it. And then there are two kinds of entities that most people don't think of as being separate like nodes but they're taxonomy terms so we have a county taxonomy and a neighborhood taxonomy that we've defined for this particular site. So why don't you walk us through building a new view for this? It's pretty easy to get going. It just starts with a simple form and as the boxes are checked it becomes more complex. The new view wizard only does the most common use cases, page and block. Other displays like attachment have to be manually created on the edit page. So if we look at the starting wizard page, the questions are pretty easy. You have to give your view a name which will automatically create the internal name that Drupal uses. Then you tell views the views type or what kind of entity you want to base the view on. Content is the most common so it's also the default. Then assuming you pick content you can select a specific content type like property and the initial sort order. Finally you can check boxes to create a page display or a block display or even a rest endpoint. Of course in Drupal nothing is as simple as the starting wizard would leave you to believe when I click the create a page a whole bunch of additional settings magically appear. Let's zoom in on just the page settings you'll see. I can set the page title and the path. I can select what kind of display I want on that page. In my property maintenance view it was a table so I'm going to try that now. I can also set up a pager for when there are too many results to display on a single page and I can add an RSS feed. Fortunately except for the base view type I can change anything I set up here later on the view edit screen. So dad what are the various parts of a view? Okay well we went through them very briefly but let's talk about the various in more detail. So you've got the base or view type. This is the kind of view that you first set. This is the one thing that once you set it you can't change it. And so the most common use is content. The other thing you can do is you can actually do a view of content revisions which the first time I looked at this I wondered why. And then I was chatting with some people in Minnesota that work for a state agency and they by law are required to publish the edit log for every page that they have on this particular website. So the ability to do that automatically and just attach it as part of the page is a really convenient for them. Because comments are now entities and not a unique thing you can do views of content of comments. You can do views of log entries so if you've got the DB log turned on. Now the challenge with this is it's a very interesting capability but of course one of the recommended best practices is that you turn off the DB log in production which means your view goes away with it because you have nothing to reference. You can do views of the file system you can do views of taxonomy terms and you can do views of users. So you have lots of different base or view types that you can work from. Once you've picked a view type then what you do is you pick a display type and the displays can be page which is the most common or block which is almost as common. There's a unique display that's part of every view probably never noticed is there and that's called the master or default display. It's there in every view it disappears as soon as you create any other display. So there is a settings on your settings page that you can go to to always display the master but it's very handy if you're building theme and variation displays to edit the common part in the master display and let it ripple through and then just do the unique parts the over rides in the individual displays. You can have a display called an attachment and that's a view or a kind of display for a particular view that is attached to the same view so if you have a page you can create an attachment that creates basically a second view as part of that view and they can either share the same parameters or they can also have different fields completely. An embed which interestingly enough really doesn't do anything in early versions of Drupal 8 I haven't checked it recently but it was pretty clear that the intent was at some point to limit your ability to embed a view in code only to the embed view type so that it would become a little cleaner. At the very minimum it's a recommendation I would make to use it if you're going to embed that view in code because that's a signal to a builder that this particular view is being used in code to create displays you might not want to edit it without understanding what the implications of that are. You also have an entity reference view which is a very interesting one so when you create an entity reference field in a content type you right now the default behavior is an autocomplete so as you start to type the title of the entity that you want to be referencing from the other content type that's what shows up. What you can do with this view is you can click the widget change it to the entity reference select a view and now you have all of the selection and sorting capabilities that views provide you in a pull down list that you can use with entities so instead of autocomplete you can give people just what they need to know. The next and last one is an RSS feed that's been around in views since the beginning and it allows you to create RSS feeds that are used by other websites for aggregation and then the more general as of Drupal 8 REST export so now what you can do is if the REST modules enabled the REST export becomes available you have the ability to use views with contextual filters to actually structure what kind of data and how that data is going to come back out through the REST export. Once you've done that then you have format options for whatever you're looking at so you'll have a grid which in Drupal 8 is actually a grid and not an HTML table like it is in Drupal 7 and Drupal 6. You have the HTML list, you have an unformatted list, you have tables and you have anything that can be added by different modules. Views accordion is one that I've seen used on a lot of sites. It gives the ability to create accordions very easily. You have a view slideshow which creates those carousels that every designer tells you you should never have and every website has and then you can actually create maps so you actually have modules and parts of modules like the geolocation module provides a views integration and so I can create a view as long as I've put the geolocation data in as the output I can go ahead and feed that directly into a Google map. What's interesting is of course every format has options the table being the most interesting of them it has options upon options so you can actually make once you've created a table with a view you can have it be click sortable so the sort you define isn't necessarily the sort that the user has to see. You can take two fields and combine them together in a single column so in one of the examples I did you have the realtor's name and his phone number separated by a comma that's something very easily to do within that. Once you've defined the format then what you do is you define typically the fields the alternative is you can use display modes and so I can say I want content with either the full display mode or the default display mode or a user to create a display mode I personally believe that it's better to use fields in this case because what it does is it puts the ordering of the fields and how they're formatted along with the view whereas if you use a display mode it's somewhere else in Drupal and you have to remember that you're using that display mode to control how this particular view is being put out. And so each field of course has lots of settings in the case of date you can pick the date format that you're going to use to pick the time zone that's going to go with you can decide whether or not to have a label attached with this particular field or not if so what the text should be whether it's the field label or whether you want to override that and put your own on you can do style settings and rewrites and things like that and one of the things that I found very powerful ever since views added it in Drupal 6 I think is the field rewrite and so you check the box that says override the output of this field and all of a sudden you have the ability to rewrite this field as everything that's above it or using all the values that's above it so one of the common mistakes that gets made is well I've got it it's in my field list but I can't see it in this list it has to be above it in the list but one of the options you have on the field is exclude from display and you'd wonder why would you put something in a view if you're going to exclude it from the display why bother well the field rewrites is actually where you use that and so it's very easy to take two relatively disjointed fields and reassemble them let's say as a link with a very custom URL being built as part of it or you can do more complex kinds of things the other thing that I found really interesting is in Drupal 7 the field replacement patterns are square brackets the token module and if you notice these are the double brackets which it was very very late in doing this that I realized that looks a whole lot like twig and sure enough that field is actually a twig template inside your view so anything you can do in twig you can do for a field rewrite inside view so just as an example I put a for loop and I had it display characters separated by asterisks simply by rewriting a field with the twig capability so it's a very very powerful capability so let's talk about filters why don't you tell us how they work filters let you select just the part of the data that a view might return for example one of our favorite shows is tiny house nation which is a show about houses smaller than 500 square feet I thought it would be fun to have a page of tiny houses on the real estate site to do that I set a filter to limit the results to listings where the property square footage is less than 500 square feet I could do that on an extra display of the main property view so it was really pretty easy exposed filters let site visitors choose what values to see like in my real estate site this lets visitors select ranges for square footage asking price and number of bedrooms just like just the right house at the right price for them so dad I see that for a neighborhood I can filter by the neighborhood taxonomy but do I have to create a different display for each neighborhood each with the filter set the way I want or is there a better way I've seen a lot of views built with 10 different displays one for each taxonomy term there is a different way and that way is called a contextual filter and it's one of the cases where views is sort of confusing because you have the filters on the left hand first column and the contextual filters are sitting over on the right hand advanced column and there's really nothing more advanced about a contextual filter than it gets the value you're looking for to filter from the URL and so if you'll notice we have available property listing by neighborhood and it says city center with a percent 20 when I develop the taxonomy for the property I didn't think about using it as a contextual filter and I put spaces in my taxonomy terms and when you have a space in taxonomy term when it's put into the URL it has to have the percent 20 it's translated into what URL friendly thing but what it lets me do is literally by saying city center I can now give an available property listings just for city center and that's the part of it and the way I did that was I went into contextual filters I clicked add I add the taxonomy term and I configure it with the particular taxonomy I want display only the things I want it also has a very powerful feature which is something I use a lot where if you don't have a value in the URL what do you want the default to be and you can set that to a fixed value you can actually have it read some of the context like what user is logged in or what page or node you're on so there's all this extra information so you'll find a lot of times you're using contextual filters not using the URL but using the ability to pick up default values out of other things so Amanda walk me through how you change that order views wouldn't be too useful if everyone displayed results by date the entity was created but as you said this is Drupal so anything is possible in views I can change the ordering or sort criteria to be any field and either ascending or descending I can also have multiple levels of sort so if the first sort criteria neighborhoods return several results I can sort it by another level like title and can sort within that group looking at the settings for the sort criteria field I can see you can expose the sort to let visitors change if they want ascending or descending in this example I'm sorting a date field which adds a granularity option so I can sort down to the second or maybe just the day and then use the second level sort to sort by another field like alphabetically by title sorting is pretty easy but using the structured content like you described I wanted to have a few that shows fields from the realtor in my property listing but when I try to add the field it doesn't show up is there a way to do that of course this is Drupal that's where relationships come in so the other advanced option that you'll use a lot is called relationships when you add a relationship you're basically associating one content type through an entity reference to another content type and when you do that you actually acquire the fields from both content types now remember when I put up the picture that showed the property with all of the other content types that could be related to it and I had arrows drawn so it went from property to realtor from property to owner from property to taxonomy terms but it happened to go from open house to property that those arrows are important when you're using relationships and the reason is if you follow the arrow it's relatively performant if you don't follow the arrow Drupal has to basically read the entire other side and figure out what happens to be pointing backwards so Drupal doesn't have a problem doing that for you I think in Drupal 7 you had to use a module to accomplish that but now so what you have are two options if you pick a particular field name you're going to see two options and that is content that's referenced from and content using so if it's content using I'm following the arrow if it's contents referenced from I'm going out to the other side and trying to find things that are pointed at me does that make sense to everyone once I do that and you add a field from the second content type it won't show up and the reason is you also need to go in and tell it which relationship to actually use and the reason for that is Drupal allows you to recycle fields from content type to content type so in theory you could have a view you could have two relationships both of which has a field that's based on the same exact field and you need to be able to tell it which one it is so unfortunately in that relatively fringe case causes Drupal to want to be more general so in every case you have to go back and tell it exactly which content type you're using once you do that and almost always when you pull the pull down list you only get one option so it's not a big deal the other thing is you'll notice now when you're looking at my field list I've got title twice so I did something that probably wasn't the world's best idea for a user interface I use the title field that's part of every entity especially every content type in the property it happens to be the address and that's what I changed the label to in the realtor it happens to be the realtor's name and that's what I changed the label to so on the user input screen it isn't so bad but when I'm doing development I have to now keep track of there's a title that's the address that's part of property and there's a title that's realtor and you notice it's marked as hidden and that's because if you looked at realtor phone you would find out that the realtor phone is actually rewriting the field to be realtor name comma realtor phone so Amanda before we take questions why don't you show a few of the views you built on the front page on the front page we have two views using addon modules the picture of the house is actually a carousel or slide show that changes about every five seconds below it is a map of available properties when you click on a map it opens a box with the relevant data looks like I need to figure out why that one is showing up twice this is the basic what's for sale page showing a view with various exposed filters that visitors can select this is a groupings by neighborhood this is another display from the main property display that is using a contextual filter to only show the neighborhood city center finally this is a views infinite scroll picture gallery of available properties it starts off only showing four properties but as you scroll down it adds properties four at a time it's kind of like how facebook does it's timeline that's the end of the presentation part of the talk however we want to remind everyone that on Friday is sprint day please come Amanda will be there she's actually helping me be a sprint mentor this year and I'll be there and we hope we'll see all of you there because it's through the community getting together on days like sprint days that we have a more powerful environment to work in and finally there's excuse me there are evaluations on every page so if you go to the session URL for this particular session there's an evaluation you fill it out we would love to hear your feedback so with that we'll take questions job offers whatever so there are microphones because we're being recorded if you would use the microphone we would dearly appreciate it everybody's got a views question what is the best practice to embed a view into a node do you use the tweak way or do you use a contributed model my favorite way and I think the one that's cleanest within the drupal way of doing business is I create a view that's a block and I place the block with visibility on that particular node or node type so views can build blocks blocks can easily be placed on particular nodes and if you want a particular view that goes with a particular node to change depending on which node it's on that's where contextual filters comes in really handy so you can have a you can have a contextual filter which can't have as a block can't have a URL and the only way it'll find out what filter it needs to use is by using something like the node ID from the page it's being displayed on okay and what if you want to embed the view inside the content so you have some text some intro text and then after that the view and in the end gallery something that's more challenging be very honest I haven't done it in so long I know in drupal 7 there was a module that would let you do that basically added it to content types huh well not panelizer not panels or panelizer I think what you're talking about is like almost like through the CK editor the whizzy wig you'd have a button that would say insert a view here and you could do it otherwise if you can if you can split it into two fields then you can use blocks again because it'd be really easy to put a view in between two others and ultimately when you want to go really kinky you can use paragraphs or panels and basically have stacks I know we we did a drupal 8 project with paragraphs and so we had different paragraph types one of which was a view and so we could add a view in a particular but it was that what's very popular style now where there are a bunch of bands and each band might be a slightly different kind of discipline okay and inside the paragraph how do you embed the view inside the paragraph you can't do it with HTML you'd have to do it by generating the HTML through to do it right you'd have to do it by generating it from something that could combine them like that so you can't really put a view in the middle of a paragraph easily cleanly okay not within a single field like I said you could have two fields body one body two and easily put a view in between them but and I think like I said in drupal 7 there was a module that lets you put in it's sort of like a token huh views insert okay I knew it was it's been a while but basically it lets you format like a token and as long as you have the token module turned on it's just yet another way to insert variables within your copy thank you other questions to ask about I think one of the things you can do in terms of output is to tap into the rest APIs and things like that so instead of outputting HTML you can do JSON and others like that you can start decoupling a bit and would you recommend using views to create these sort of feeds or are there better ways of doing that for rest purposes with the caveat that I am not a front-end developer I may have pretended to be one a couple times and so I don't do javascript with frameworks and decoupled kind of drupal approaches the way I'm hearing from developers that do they're doing it one of two ways they're using the JSON API module which is a slightly different philosophy than the rest API that's built in the core but gives you that same capability very frequently it depends if you have relationships that you want exposed together basically denormalizing views is a great way to do that if you have content that's just simply being output you can do that directly from the rest API that's part of core so there are really two, three different ways and you might mix or match a couple of them together but more and more I'm hearing that people are doing true decoupled are using the JSON API because it seems to have a more natural interface than the built-in rest API. Are there questions? I feel like there's nothing else hello with that we thank you, if anyone's interested in the book there are postcards in the back, there are postcards up here, they have a discount code so if you're going to buy the book you're going to buy it probably in the next couple weeks because it's cheaper with that we'll thank you for attending and have a great year book on. We do time wise 45 minutes, wasn't it? Or was it? Yeah, it was 15 and a half hours. Yeah, it wasn't a question, I just thought you should tomorrow I've got a user filter issue and I'm going to be working with it, I think I can solve it a bit I just want to see what improvements is XML from Feeds Okay, so there are content types? Yeah, it's got a unique