Document status: [Done] Create base page for Views 2.x documentation [TODO] almost everything
Views is a flexible query builder that will allow your module to expose its data so that Views can build queries with it. While Views has a pretty good idea of how Drupal Core is laid out, you as a developer, must tell Views what to do, or it can't handle your data.
But, if you give Views the proper information, Views can incorporate your module's data and help you build queries.
Views needs to know the following things:
- Each table that contains data that Views might use.
- Whether or not this table is a 'base' table, which means that it is presented as a 'view' type. For example, node, users and comment are all 'base' tables.
- How this table relates to existing base tables; these relationships are 'default' or 'implicit' relationships. There are also explicit relationships which can be added via the UI.
- What fields users can display in list/table type views.
- What fields users can sort their views on.
- What fields users can filter their views on.
- What arguments users can provide their view to do filtering via the URL and create summary views.
Your module should define default views so that users can immediately do obvious things with your data without having to build their own views, and several hooks to help you modify a view at various points while it is being built.
In addition, you can also define displays, styles, row styles, default argument handlers and argument validators, which are not necessarily related to tables.
How Views works
At its very core, Views uses an object which is a generic query builder. When the $query object is instantiated, it needs to be told what the 'leftmost' table is. This is the 'base' table, or the 'view type'. When tables are added to the view it needs to know how they are linked to the base table. This helps Views do a lot of setup without the user having to know or care about the exact relationship between a particular piece of data and the core node table.
Armed with this relationship data, when fields are added to a view, either for display, or for sorting or filtering, the query object automatically determines the relationship of that field's table to the node table, and adds the appropriate joins. The core methodology of this system is quite simple.
What your module needs to do
At a minimum, if your module wants to use Views, it needs to implement one hook:
This function returns data that will describe how your module's tables relate to the Drupal node table, as well as what fields can be displayed and sorted, and how your tables may be filtered. This hook can return just one or many tables. This function should be placed in a file named MODULENAME.views.inc in your module's directory. Views will automatically find it and include it when necessary.
If you want to provide default views that your users can immediately use, implement
hook_views_default_views(). You can use the views exporter tool to create this hook. This hook should be placed in MODULENAME.views_default.inc.
There is a lot more documentation for Views 2 in the advanced help packaged with Views. Go to admin >> advanced help >> views >> api (help/views/api).