Notes on advanced Views

We encourage users to post events happening in the community to the community events group on https://www.drupal.org.
vkareh's picture

The following are some notes from the presentation on advanced Views. I have linked to the module (with the content type and view) used for the demonstration.

We started by creating a simple view using the Views UI, then we loaded it into our code so that we can modify it. For this example, we outlined four steps for using Views in the code:

  1. Load the view
    The view object is loaded and stored in a variable
  2. Build the view
    The view query is built based on all the options that the object contains
  3. Execute the view
    The query is executed and the list of results is populated
  4. Render the view
    The results are converted to the output format (HTML, JSON, etc.)

What we need to remember is that there are different types of manipulations that we can perform on a view between each of those steps. Following is some code that outlines the steps and things that can be done between each of them:

<?php
$view
= views_get_view('my_view'); // Load the view
/* Here you can add items (fields, filters, etc.) or change options (pagination, display settings) */
$view->build(); // Build the view
/* Here you can modify the query before it is executed */
$view->execute(); // Execute the view
/* Here you can manipulate the results before they are rendered */
$output = $view->render(); // Render the view
?>

Since this was a short demo, I focused more on outlining all the different things that are possible with Views in the code - but we still had time to show how add fields and filters. For any new element that you want to add, you should use the view::add_item() function. This is the way to call it:

<?php
/**
* $display_id is the ID of the display you want to manipulate (default, page_1, block_3, etc)
* $type is the type of element (field, filter, argument, sort criteria)
* $table is the database table where this element lives
* $field is the name of the actual field that you want to add
* $options is a list of settings for the item
*/
$view->add_item($display_id, $type, $table, $field, $options);
?>

The linked module contains concrete examples that show the power of this simple function.

Other interesting resources are:

Detroit

Group organizers

Group notifications

This group offers an RSS feed. Or subscribe to these personalized, sitewide feeds: