Drupal 8: your guide to views in core
What changes, if any, can users expect now that the Views module is a part of Drupal core?
A big change that is on the way with Drupal 8 is the incorporation of the Views contributed module into the core Drupal 8 download, as announced back in 2012 by Dries Buytaert in his blog. Views is a module which allows non-developers to build paged listings of content, whether blog posts, news items or photo galleries. It’s one of the most downloaded contributed modules, and hundreds of other contributed modules depend on it. In this blog post I’ll take a look at a few of the new features for Views in Drupal 8.
Admin lists now use Views
One of the first differences you’ll notice when navigating to ‘Structure’ -> ‘Views’ is that the list of default views include admin listings such as ‘/admin/people’ and ‘/admin/content’.
This is great news, because it means that as users we can now customise these views. I’ve been frustrated many times on being confronted with pages and pages of users at ‘/admin/people’ and having no way to search for a particular user. In the new Views-based version of this listing a search box is provided by default to search for username or email address.
This will return any partial match, so searching for ‘Helen’ might return users with the email ‘email@example.com’ or ‘firstname.lastname@example.org’ or someone with the name ‘Helen Smith’. But suppose my site contains thousands of users, and I also want to be able to search for an exact email address? I can go right in and edit the 'People' view.
When editing the existing ‘People’ view, I can see that other than styling differences there aren't many new changes in Drupal 8 Views when compared to Drupal 7. This is great because it means there isn’t a steep learning curve to get going.
In my People view, next to ‘FILTER CRITERIA’, I can click on ‘Add’, to create a new filter, and then select ‘User: Email’ from the list of available fields. I then need to check ‘Expose this filter to visitors, to allow them to change it’ to create a new search field above the people list - just like I would for a view on the front end of the site. I’ll set the ‘Operator’ drop down to ‘Is equal to’ to give an exact match, and update the label to say ‘Email matches’ so that editors know the form is expecting an exact match.
Edit People View Filter
Once I’ve applied and saved the changes, I can see the new field appearing on the people listing page, and I can try out the exact matching - my full email address returns a result, but ‘helen’ on its own does not.
Updated People View
Restful exports using Views
Another new addition to Drupal core alongside Views is RESTful web services. In combination with Views, this means you can create really customised API exports, for example to send data to a CRM or to a mobile app.
To get started with creating a REST export I first need to make sure that the REST and Serialization modules are enabled (by default they are not). And note that the modules listing is now called ‘Extend’ in Drupal 8.
I’ll then return to the views listing page to create a new view. I’ll give it a name of ‘API export’.
Notice in passing that when you create a new view in Drupal 8 there is the option to display more than just content in your view. You can now choose comments, log entries, files, content revisions, taxonomy terms and users as well as plain old content. This makes it much quicker to create particular kinds of view listings without having to search through all the available fields.
For now, I’ll leave the default settings to show all published content listed by the most recent first. Rather than creating a page or a block I’ll select the ‘Provide a REST export’ checkbox, and select a path to export to - in this case I’ll just use ‘api’.
Add new REST view
As I continue to edit the new view, I can see that it has just one display, of type ‘REST export’. There’s only one format option available - ‘Serializer’, which will convert the output to XML, JSON or HAL according to what is requested by the client (the default format is JSON). I could limit which of these options is available under the settings.
By default the export will be set to show the whole entity, but I can change this to show ‘fields’ and select which fields are included.
Edit REST view
Once that’s saved I can visit /api on my site to see my simple JSON content export.
But perhaps I don’t want my export to be available to everyone. Because it’s created in Views, that’s easy to change. I just need to go back and edit my view again, and select ‘Access’ under ‘PATH SETTINGS’. I can select users based on their role here, and restrict my export to authenticated users only.
For more info on creating RESTful views in Drupal 8 have a read of this blog post which I followed to set up the example here. It also gives tips on testing different formats using the Dev HTTP Client chrome extension.
Header and footer options
Another change when editing views in Drupal 8 is that there are a lot more options available when adding a header or a footer to views, and you can now choose to display anything from a rendered file to another view at the top or bottom of your view listing. To test this out, I’m going to try outputting a block above a sample view wherever it appears. First, I create a new view which simply outputs the titles of all my published content. I’ll then select the option to add a header to my view, and choose 'Global: Rendered entity - Block’.
I’m then given the option to add the id of a block that I’d like to display above my view. To simply test this concept I’m going to add the search block above my view listing - in reality, you’d probably choose something more sensible such as a block showing a link to a featured blog post above the main list of blog posts. The machine name of the search block is ‘bartik_search’, so I’ll enter that into the ‘ID’ field and leave the other settings on the default options. When I visit my new view page, I can see my list of article titles, and the search block appearing above it.
The extra options here for view headers and footers look like they will give a lot of flexibility when structuring pages.
This is not an exhaustive coverage of all that is new with views in core - but hopefully it gives you a flavour of some of the possibilities available. Creating and editing Views as an admin has not changed that much since Drupal 7, meaning that the experience is reassuringly familiar.