 Okay everyone, I'm going to kick off. Welcome to my session. It's a session about my favorite module in Drupal, the views module. It's a slightly technical module for those of you who are looking to find out how to use views. This is probably not it, but there's more about the possibilities that views offers in terms of what you can do in Drupal. So it's a bit of a look under the cover and hopefully at the end of the session you'll have an understanding of what views can and can't do. So even if you've never used views, there should still be something in there for you. Standard Drupal is very singular-minded. It knows about nodes, it knows about users, it knows about taxonomies. It knows about some relationships. It knows, for instance, when a node is created, it knows who created the node. So there's a link from the node to user. There are some custom modules that provide more linkages like the entity reference, node reference type of modules. There are some lists in Drupal but mostly admin and mostly coded in Drupal itself. So the question is what do we do if we want to create our own lists in Drupal? So the answer is views. So views to the rescue and in terms of what you can do with listing data on a Drupal website, the views module really is like a Swiss army knife. It knows how to gather data out of Drupal, it knows how to process that data and it knows how to present that data in lists, for instance. So this is the views UI. This is where you configure views to do what you want it to do. And really beneath or under the covers in views, it's really a very sophisticated SQL statement generator. So if you look at some of the options, the fields options, that's really where the select statement comes from. And before you even go into creative view, you get asked, the big yellow box down at the bottom, you get asked, what sort of content do you want to present in your view? And that's where it asks basically what base table do you want to use? Do you want to show nodes? Do you want to show users? That type of stuff. So that's where the select statement, the select portion of the SQL statement gets its information from. The where clause in an SQL statement comes from your filter criteria and your contextual filters, which are your arguments. The order bar is your sort criteria. You can join your tables to other tables and build relationships between nodes, taxonomies, users, et cetera. And you can use aggregation as well, which is used in the group bar. So you can sum fields, you can count records, that type of thing. The output that you get out of views is broken down into various areas. So at the top of your view, you have your title. You have a header area where you can specify some text or part of the content to display in the header. You have your exposed filters. Then you have your attachments before, which is basically another view. Within a view that you can show before your main view, your pager, your attachments after, and your footer area. The main content is in middle. So that's your style content. And that is basically where you display your content and that comes out of the rows that have been produced by the SQL statement. There are some concepts in Group In views that are worthwhile knowing about. And that's basically how views hangs together, how it works and how it does everything under the covers. So views has a concept called Handlers and Plugins and Templates. The Handlers basically prescribe how views deals with the underlying data in Drupal and they help build the queries. Plugins are more about the query building process and the behavior of views. So Field Handlers tells views how to process the fields that are in your table, the underlying table that is processing. Sort Handlers tells views how to sort, the same with Filter, Relationship Handlers and Area Handlers, etc. So there are all the bits of code that are inside views that tell us how to read the data in Drupal. I'm just going to show a bit of a demo. There will be a little bit of code in here but just bear with me. So it's not just Drupal data that views can process. You can also have other data out of other systems and even custom tables that you can have views, process and display data from. So let's have a look. I have already created a custom table in Drupal and so my custom table is there and I'm now using a function in a custom module called HookViewsData. That HookViewsData function tells views what is in your table and what to do with it. So I have a table called Alumni Roll which is basically a list of students that have studied at a particular university or whatever. And then I tell views what is in my table. All the fields that are in there, what title to display in views, what help text to display in views and then I get to things like Handlers. How does views handle this field? And I say well this is just a field so that's fine. I also wanted to be able, views to be able to sort on it in a table. That's where the click sortable specification means. Then I have things like surname and I say okay, it's a standard field. I want to be able to filter on it and when views filter on it I want views to treat it as a string and I want to be able to sort on it. So if I specify all these things then when I go into views it will come up in the sort options, it will come up in the filter options, etc. For the year again it's a different field in this case it's a numeric field. So I'm telling views treat this as a numeric field, I want you to filter it as a numeric field and I want you to sort it. Then I have another field in my custom table and I'm saying well this is a term ID. So when you look at this field in my table I want you to treat it as a relationship. So this is a term ID, it has a relationship to the taxonomy term data table. I want you to handle it as a relationship field, etc. So that tells views all about my table and then I can go into views and I can views, I can go create a view. And what happened now is that here where I show show content. Now all of a sudden my table is shown here as content that I can create a view for just by specifying to views that I have that table and what it's made up of and what's in there. So it helps if you give it a name. So I'll just quickly have a look. Yeah well whatever. Sorry? Ah no you have to create it by hand. But if you go and look under the covers in Drupal there's exactly the same code for the node table. It describes all the fields that are in the node table. So when I create a view now under filter criteria and under fields etc. All the fields that are specified in my table are all available. The same with the relationship fields. If I want to build a relationship field now that field that I described in my table is now showing up here because views knows about that field. Right. So I have created a view. Let's go back to it. So this is a view that I created based on the table that the custom table that I had and I told views what to do with the data and where to find it and what it's all about. So I can now create a view like this for my custom table. So that's how Drupal have views knows about the underlying data. So in my discipline field there was a term relationship to taxonomy so I can make that a dropdown field. For the graduation year because I specified it as a numeric field I can only filter on it by actually putting a numeric value in and then I can filter it by that particular year. There is some... So if I have another table somewhere and I want to be able to tell views to treat it differently I can also go back and change the way views looks at my data. In this case I have... I have here... used a function called who called views data alter and so okay I'm going to alter the view the way you look at my data and for this table and for this field year and for this filter handler I want you to use a different handler and here I've specified my own custom handler and the code for that is down here but basically all it does it selects the distinct values for the year it changes the filter handler as an in operator i.e. a list operator I collect all the years and that's what it does. So if I go in and enable that module then you should see if I go in and enable that we should clear the case as well if I now go back to my list now you can see the graduation years treated differently by views no longer a treated as a numeric field is now treated as a list field and I've given it given it a list of all the years as options so these are all the years that are in my table so that's one way to that's how views knows about your data another example is working with CVCRM a little while ago and CVCRM has location fields latitude field and longitude field but for some reason the fields in CVCRM are defined as strings, as text fields which means you can't do any calculations on it if you want to calculate the distance to that particular point so you can go in and you can change the way views looks at it and change the data and say well it's no longer treated as a string field but now treated as a numeric field and do that for the argument for the contextual filter and for the straight filter so you can do things like that so if the data in somewhere in Drupal or a module like that is not quite what you need and doesn't quite fit you can go in and alter the way that views treats that data ok, apart from handlers which look at the views data we also have plugins and plugins really control the behavior of views so things like the page details what is this view actually going to be in Drupal is it going to be a page, is it going to be a block is it going to be a content pane in panels for instance so there are plugins for that and there are plugins for how the content itself is actually shown and formatted and there are plugins for how the fields are formatted and displayed there are things like pager plugins that control the pager at the bottom of your list that goes forwards pagers forwards and backwards etc the green ones that are all themable so you have templates that control the display of that which you can go and change there are also other non-themable plugins like the access plugin which determine who can and can't see your view and who can and can't see your data contextual filters that your arguments on inside the URL that can filter on your data and things like query settings and caching so the display plugin where it's shown and the type of display in Drupal etc we've pretty much covered all of that so an example of a display plugin you can have a view that's a page which is pretty data to basic options you always get a page or a block but there are many other options that are provided by custom modules so you can install modules and that will then give you additional options for how to display your data. Content panes and contacts are for panels there's an embed display which you can use in code to show your view it doesn't have a UI the feed displays open layer data which is a mapping overlay that type of stuff then you have your star plugin so this is if you think back to the SQL example your display is all your data the star plugin is how the row is displayed and you have many many different options depending on what sort of modules you install but you have like mapping sliders HTML lists or tables or just a straight unformatted list and then within the row you then have row star plugins which going back to the SQL equation is pretty much your fields within your row so there are different options there as well so what does a plugin look like so plugins as I said are bits of code or objects that control the behavior of views you can install them they usually come with a custom module but you can also build your own and you can also modify your own so that that allows you to extend the usability and use of views so one here is a custom module called the Light Pager Views Light Pager the standard full pager in views is quite processing intensive it does a bit of work behind the scene to work out how many rows are in your views and based on that it works out how many pages there are and how many pages you can forward so a bit of overhead in calculating every time how many rows total rows you're having in your view so there's something called a Light Pager which if you have a very very large group of database with lots and lots of content it can make your views the pager in your views work a lot better and faster so how do you create a plugin well basically it's like everything else with views you just have to tell views that you have a plugin and once you do that it knows about your plugin it knows what to do with it so this is the views light pager it's a contributed module so this is my function this is my plugin and my plugin is a pager plugin if you remember back to a few slides ago there's all different plugins for all sorts of things it's a pager plugin I want to call it Light this is my title this is the help and my handler for this pager is this code here and what it's saying also it's the parent object for this pager it's the full pager so as few lines of code you've told views that you now have another pager as a choice for pager in here this is the actual code that does what the light pager does it basically extends the full plugin so it uses the full plugin as a base and just basically changes the behavior of the full plugin it does a few bits and pieces basically just based around how it works the most important one is this one here and this one says okay well I'm not going to use account query so I'm just going to display a very very basic pager for which you can go backwards but there's no no record of how many pagers there are how many records there are et cetera so this here is an example of the light pager it's as I said it just lets you go forward and backwards but nothing more so that's a plugin and that's how you define a plugin in views it's just another way that you can extend the usability of views fairly straightforward for coders of course another one we can have a look at is the access plugin so if we go very quickly so the access plugin the standard views lets you control access to your view data either with no restriction or restriction by permission or restriction by role but it's very easy to write another plugin as we saw before just like with the light pagers you can write a plugin which basically takes one of these as a base slightly modified and you can have an access plugin that lets you access a view between 4am in the morning and 2am in the afternoon and not at any other time of the day for instance not sure why you would use anything like that but you can do that so all of these things in views none of this stuff is fixed it's all extendable with your own code and with your own plugins and your own handlers so that is so we've looked at the handlers which control how views looks at your data we've looked at plugins which basically controls the behavior of views of your data or what it does with the data then in the third step we have all the data how are we actually going to display all the data that views has gathered so a lot of the plugins that we saw were themeable which means that the output that produced through a template file and there are various levels of template files in views the global template files for the view itself for the display there's a template file for the style there's a template for the roast style and there's templates for fields so there are many different levels within views where you can change the templates of how it actually produces the output so here for instance I'll just have a look I have a view I have a view here with some items which have an image a title and some text there could be news items there could be events for whatever you want to think of and and this is actually this is produced using a bootstrap media object plugin which basically displays the image the title and the description in a responsive way so the style output of that there are a number of templates that you can use or that you can modify this information comes out of views itself if you look at views there's down the bottom right there's templates and it shows all the templates that you are available and that you can use and modify so there's one here called views bootstrap media plugin style demo 2 page so this is a template that is used for that style it's for that particular view and it's for that particular page within the view so this is the most specific level of template you can use so that's the one there so it looks views will look for template files to very general for that whole view or very general for a style down to very specific like I only want for that specific page in my view and only for that particular plugin style so we're going to have a look and and see so I have basically copied the template for the views bootstrap media plugin and I have altered it so that for every row that's displayed through the template I have created a flag and that flag basically turns on and off for every row that's processed so when my flag is on I'm going to add an extra class to my output if it's off I'm going to add class even so that will allow me to have zebra striping on my output the views bootstrap media display doesn't have zebra striping as an option so you have to go into the template and do that yourself so if we look at that we go to here no we don't go there so what we need to do is basically rename this I've renamed it in such a way so that our views wouldn't find it I rename it back so as our views will find it I'm going to just clear my cache and if I now go back to my demo 3 output and so if you notice that now you can see that I have zebra striping on every row so my odds, my evens so that's how you can modify the output of views by playing around with the template so all the templates are all there straight forward templates to modify your best copy the one that you want to change into your own theme library and make the changes that you want then apart from all of that there are also what's called hooks in views and hooks are basically bits of code that can intercept the processing of views at specific points and change what views actually does there are different views and they they run in order from when views starts to when views actually produce at the output so you can use a hook views preview to change the view before it even starts doing anything you can intercept the building of the query inside the view before it builds it you can alter the query itself you can change what views built the query then there's the execute phase where it actually executes the query and actually retrieves the data from the database and you can still change the view before or after that and then there's the render phase where it actually produces the output and there's a pre-render and a post-render phase there as well um alright now I was going to okay so we go back to my first demo this one here um this one allows me to sort the data by surname and graduation year because that's what I've told views that I wanted to be able to sort on that it's the view is by default sorted by surname but my problem is that when I click on the graduation year it's no longer sorted by surname the names are all over the place so I want to change this view and say well if I sort by graduation year then I want my secondary sort to be by surname which typically in views you can't do in views you can specify how to sort the data but for this type of output in a table where you can click on a column header to sort it you can't say well if I click on if I sort by this and I also want to sort it as a secondary sort by surname it doesn't let you do that but anyway that's where hooks come in and that's where we can use our um our code where it was in here so here what I'm doing is I'm using a hook for views query alter and basic what I'm doing is I'm telling you as well I'll let you build a query and I'm quite happy with that but I want to change the query in a specific instance um if you're sorting my query on the discipline um sorry if you're sorting my query by year then I also want you to sort it by the discipline as a secondary sort so I'm basically saying if that's the view the name of the view and that's the display of the view if you're sorting by the year then add another order by um here and I want you to sort also sort on the surname field and I want you to sort it in a sending order right okay I need to so I think this one here is the one okay so here now when I sort by year I've now told views okay well that's fine you can sort by year but I also want you to sort by surname as a secondary sort and you can see that's what it's doing here now so just by making a small change to the query that views has produced I can still influence how it generates the data and how it displays it right okay well that's all the heavy stuff out of the way there are some useful view settings that that you can use when you generating views that that are not often used or not immediately noticeable for instance here there's a tag column which is people typically not used but if you do have a lot of views in your system you can tag your views with a specific tag and then you can order it in that particular order it's a nice way to group like views together there are other settings this one here always show the advanced display settings if you go into the views UI on the right hand side there's advanced settings it's always closed by default if you use that often you can just here you can specify okay always show media advanced settings the other one is the master display every view has a master display it's not shown by default but if you want to change the master display then you can show it there in here you can also disable the views data caching it was very handy if you're building your view because views likes to cache the data so you may change the view if you're not necessarily see the difference if the view is cached this one here show filters on the list of views it's also by default turned off but that can be handy if I saved it first it would work so now when I go to my list I can actually filter filter my views by the tag filter it on the display so there's all sorts of filtering you can do it's really handy if you have lots and lots of views right so we have basically gone through the architectural views how it retrieves our data how it produces how it processes our data how it displays the data and how you can at every step of the way change the way view works or add additional functionality to it but you don't have to do it yourself there are many contributor modules around that provide additional functionality for views and this is where the power of views really lies views may have started out as a list module to produce lists of data but it's grown into something much much bigger than that and there are many many things you can do with views that may not necessarily be immediately clear views modules for views that produce sliders accordions lots of mapping modules that contribute to modules that provide mapping functionality for views so anywhere where you can think of a number of bits of data views there is a views module that will do that so a map will display a number of bits of data or a number of locations a slider has a number of images that are sliding across the stream that type of thing there are charting modules there is a view entity or field module that lets you use a view itself as a field to another entity that type of thing so it's very clever view bulk operations is handy for an administration it lets you do a lot of administrative functions within a view that is standard admin function in Drupal don't there are admin views there are calendar views for booking calendars that type of thing there's a search api which is replaced as pretty much the standard Drupal search but it's much more advanced and much more sophisticated social media views and ctools content panes for panels so views the views module this was a couple of weeks ago were almost 900,000 installs around the world very widely used there are over 500 contributed modules that are classified as being in the views category so there are over 500 modules that do something with views that either extend views put more functionality in views or use views as the source views slide show is by far the biggest that will probably go down I would think one of the disadvantages of views slide views slide show is that it's not responsive so that will probably drop down the flex slider which you see down here that is responsive so that number will probably go up but even things like the calendar module there's 125,000 downloads of that or installs, not downloads around the world carousel another big one search api is also very big for large Drupal sites and it lets you use things like the solar search engine gmap very popular lets you display a google map as part of a view and show locations on that map there are other mapping functionalities which are even more sophisticated views data export entity views attachments and views accordion right views in Drupal 8 there has been some mentioning various presentations over the last day or two at Drupal 8 it's now of course recognized that views are an integral component of Drupal so views no longer contribute to the module that you have to download and install it comes with Drupal 8 out of the box it now views in Drupal 8 powers almost all of the core Drupal core lists and blocks and admin almost everything in Drupal 8 now is powered by views there are now restful views in Drupal 8 so you can not only render your data on normal browser screens but you can also now output JSON and XML data the templates that I showed you before they are now going to change to tweak templates to YAML files and there won't be any more PHP in the template a bit of a change but you can still access variables in your templates in views you typically have your import link here where you can import views that's all gone out of views and that is now part of the configuration manager in Drupal so it's all part of the configuration manager import and export any other config so views are now just part of that there's one new display sorry one new display which is the entity reference display so when you have an entity reference field in Drupal where you want to reference another entity you can actually create an entity reference display view that will display collected data for you so if you want to reference an entity over any particular node type then you can create a view that only references those nodes of that particular type and show that that's your list to choose from if I want to find out more about views Google like me and Google some more like me that's an advanced help module for views which you can activate has a lot of information in it about views API documentation which tells you all about the handlers, the plugins the templates etc yeah no presentation would be complete but at least one kitten in it so anyone have any questions? and that was a pretty heavy session but what I wanted to give everyone a bit of insight into what views actually is and how it works behind the scenes it's probably never used that but it's good to know what views actually does and that you have the option of extending it you have the option of modifying a functionality and there are many many modules that you can use, contributing modules that will give you more functionality so if you're looking to do something and you're not quite sure how to do it look at views it may be what you want maybe what you need look at the contributing modules there's a high chance that you will find something there that will do what you're looking for any questions? well the only thing that would have changed is all the code would have changed to OO code but other than that no just bear with me for a sec I'll learn to type okay if I go to views you will see that it basically hasn't changed so if I do that that's the view UI in Drupal 8 but underneath I'm guessing I haven't had a very close look but I'm guessing that most of the code would be the same and most of the effort in Drupal 8 has to be changed so that's the underlying way the Drupal does things views have just been ported into the core into Drupal core but I don't think much has changed that answer your question yeah as I said before there were many many modules that you can use as a base a handler you would use a handler to access your data what you're talking about if you can find a module that pretty much does what you need but not quite you can, as I've seen before a lot of them are classes you can just extend that class and change some of the methods in the class to do what you want to do rather than what the module already did yeah I mean they're not easy it's relatively easy to take one that's already there and modify that but here the idea with views of course is to use contributing modules and use as much as you can of what's already there rather than building something from scratch is that it? oh thank you whenever I looked at your small responses you're throwing at me for something else with the underrelationships I noticed when you when you were trying to bring in other so you said it's actually linked to two other methods the joint condition is actually and by default so I'm just wondering how would you go about in your SQL if you want to say if you want to introduce the or logical or how would you go about introducing so like would you use a contributing module or would you try to maybe write a handle for it now that probably isn't a module for it what you would do is you would go the wrong way again right so you wanted to change the joint condition from an and to an or in this case what you would do here is you'd make it as part of the use one of the hooks in views and I would probably use one of these post build hook so at this point in time views has built your query it's already built everything in you can go in that use the hook views post build and you can modify the query at that point and just change it from an and to an or that's one it isn't a array and it can be quite difficult to work out how the array actually hangs together it's a bit tricky but you just change some of the settings in there it's not a big long string that you can then alter okay well thank you for your attention