This page is archived
We're keeping this page up as a courtesy to folks who may need to refer to old instructions. We don't plan to update this page.
Alternate resources
There's a reason Views is the most popular module on drupal.org and its pointy-clickly user interface is only a part of that. In this series we cover the ins and outs of writing modules that implement the Views API. Once you've realized the power of creating complex lists of nodes, users and other content via the Views UI, the next logical desire is to allow people to do that with the content provided by your custom module as well. This series will take an in-depth look at exposing your own database tables to the Views module so that users can use them as a place to pull content from including the fields themselves and metadata about how they can be used to create relationships to other content on your site.
After getting the basics out of the way we'll also take a look at writing our own custom field handlers to expose our module's data to views so that it can be sorted, filtered, and queried in new ways. We'll also look at implementing Views plugins to do things like add custom access control options to views and to add new output styles.
Once you understand a bit more about how Views works under the hood and how easy it is to tie in to that system you'll be reimagining your solutions for all sorts of different problems.
There's a reason views is the most popular module on drupal.org and it's pointy-clickly user interface is only a part of that. In this series we cover the ins and outs of writing modules that implement the Views API. Once you’ve realized the power of creating complex lists of nodes, users and other content via the views UI the next logical desire is to allow people to do that with the content provided by your custom module as well. This series will take an in-depth look at exposing your own database tables to the Views module so that users can use them as a place to pull content from including the fields themselves and meta-data about how they can be used to create relationships to other content on your site.
After getting the basics out of the way we’ll also take a look at writing our own custom field handlers to expose our module’s data to views so that it can be sorted, filtered, and queried in new ways. We’ll also look at implementing views plugins to do things like add custom access control options to views and to add new output styles.
Once you understand a bit more about how views works under the hood and how easy it is to tie in to that system you’ll be reimaging your solutions for all sorts of different problems.
In this lesson, we give a brief overview of utilizing a very important feature when it comes to working with Drupal and that's Drupal.org. We show you how to get to the Views API documentation that is now part of Drupal's API documentation and teach you how to use the search function for finding particular function.
Additional resources
To get things started, in this lesson we'll create a new module, and use hook_views_api() to let Views know we want to use its API.
One of the most common things to do with Views is export a view from your site and store it in code as a default View. This is what Features module does when you create a feature with a view in it. In this lesson we'll export our sample site's view and add it to our module with hook_views_default_views().
In this chapter Joe will take a look at where to get a copy of the databasics code and what it does already. Throughout the rest of this series we will be working towards replacing functionality from the databasics modules with views integration in order to allow for greater flexibility in the ways that our module can be used. We'll talk about each of this pieces of functionality in this chapter.
Additional resources
Databasics module on GitHub
Overview of the Database Layer in Drupal 7
In this video Joe looks at using hook_views_data()
in order to describe a database table to views such that views is capable of making queries to that table. We’ll talk about describing the table itself, and how to get it to appear in the views UI as an option that can be used as a base table.
Additional resources
Modules Needed
In this video Joe continues the discussion of using hook_views_data()
to describe a module's table to the views module. Building on what was covered in the previous chapter we now deal with exposing one of the individual columns from our table as a field that views can use for filters, sorts, and to display.
Additional resources
Modules Needed
In this video Joe describes the process of adding the rest of the fields from the databasics module to our implementation of hook_views_data() including how to differentiate between different data types like strings of text and numeric values and how this changes the views module's behavior. Then Joe talks about how to tell views about various tables that can be used in relationship to the databasics table via foreign keys like the node ID.
Additional resources
Modules Needed
In this video Joe will walk through writing a custom field handler for views. Demonstrating how to extend the set of handlers that come with the views module in order to provide new functionality or for dealing with new types of data. In this case we'll look at how to treat the numerical values in the databasics module as percentages in views. The method used to write a field handler in this video is similar to overriding other handlers like filters and sorts that will be discussed in coming chapters and provides some important foundations for understanding how to work with all views handlers.
Additional resources
This video continues the process of adding a custom field handler started in the previous chapter.
Additional resources
In this video we'll look at adding yet another field handler, but in this instance we will be adding a Views field that does not directly map to an actual field in the database. Instead we will be creating our own variation on some data and adding it as a field that can be used in our views — a field that shows the percentage of page views as a black bar graph.
In this lesson Joe takes a look at writing a custom filter handler by building on the knowledge gained about writing handlers from the previous lessons. Filter handlers control how data in a table is treated when being used in the context of a views filter including things like how the data is represented, what the form for filtering looks like and more.
In this lesson Joe demonstrates how to extend the default views sort handler and create a new one for use on our table that will allow us to sort the data returned from a query with the rows that belong to the currently logged in user at the top of the list. This lesson builds on information from the previous couple of lessons regarding implementing views handlers and general coding for views best practices.
Area handlers are used in the header, footer and empty text areas when creating a View. In this lesson, we’ll walk through creating our own area handler that can be placed in the footer to provide a summary of all the rows in our View.
In this lesson, we pull our work together by creating a new view on the site that uses the work we've done so far with exposing our data, and creating our handlers. Once we create the view, we'll export it and add it to our module as a default view.
In our original Databasics module, we had some access control around who could see the tab we are providing on the user page. Now that we have switched that tab to being a view, we need to still add that access control back. In this lesson, we will work with a new feature of Views: plugins. We will create an access plugin that gives us the freedom to add our own custom access control, along with settings to give our users a choice about what access they'd like to use.
In this lesson, we’ll take a look at how to modify the HTML output from Views, explore some of the default templates that Views provides, and learn about the various permutations of names we can give our template files to override output for everything from a large set of Views to a single field on a single View. We’ll also discuss the difference between displays, styles, rows, and fields when it comes to theming a View.