When developing web applications we typically need to query a database, retrieve some results, and display them to the user. This process looks slightly different in various programming languages and frameworks BUT the underlying principles holds true.
In Drupal a few very smart individuals came together and developed the views module. The views module enables site builders to craft simple to sophisticated queries against the Drupal database and display the results in various different ways. From a developers perspective this may seem much different from the usual workflow we are accustomed to due to the fact that much of the work is done within the UI.
In order to build specific functionality with views we need to understand the basic architecture of a view and how it correlates to the underlying SQL queries that it generates.
Let's take a look at a very simple SQL command:
SELECT nid FROM {node} WHERE uid = “22”
This most likely looks very familiar to you if have ever worked with a relational database system. We have the field we are selecting, the ‘nid’ or node id column. The table we are selected from, node, and a where clause which allows us to filter the results based on some very simple logic. In this case we only want to see the nid from records that have a uid (user id) value of 22.
Within the views interface we have access to all the elements within this basic SQL query through the UI. We can even pass arguments into the view to make these queries a bit more dynamic. Such as:
SELECT nid FROM {node} WHERE uid = $user_id
Where $user_id is a parameter we can configure and pass into in the view in various ways.
The views interface is laid out in a way that breaks apart the typical SQL structure into sections. Below is a table of the core views selections that is exposed in the UI and how they map to your typical SQL query:
| Views UI | SQL Query |
| Fields | The list of fields in the SQL query: SELECT nid FROM {node} WHERE uid = ‘11’ |
| Filter Criteria | The WHERE section of your SQL query SELECT nid FROM {node} WHERE uid = ‘11’ |
| Sort Criteria | The ORDER BY section of your SQL query: SELECT nid FROM {node} WHERE uid = ‘11’ ORDER BY nid ASC |
| Contextual Filters | The parameters we can pass to the WHERE clause in our query: SELECT nid FROM {node} WHERE uid = $user_id |
| Relationships | Joining different tables to your query: SELECT nid FROM {node} INNER JOIN {comments} ON node.nid = comments.nid |
Most of the other fields within the Views UI has to do with how we are going to be displaying the results of our query. Such as what to display if there are no results, adding a header and footer section to the displayed results, etc.
In later posts I will be looking at how we can use these features to put together a set of views that can work together to achieve a functional requirement.
Stay tuned!

