1 view.inc view::build($display_id = NULL)

Build the query for the view.


core/modules/views/includes/view.inc, line 955
Provides the view object type and associated methods.


An object to contain all of the data to generate a view, plus the member functions to build the view query, execute the query and render the output.


function build($display_id = NULL) {
  if (!empty($this->built)) {

  if (empty($this->current_display) || $display_id) {
    if (!$this->set_display($display_id)) {
      return FALSE;

  // Let modules modify the view just prior to building it.
  foreach (module_implements('views_pre_build') as $module) {
    $function = $module . '_views_pre_build';

  // Attempt to load from cache.
  // @todo Load a build_info from cache.

  $start = microtime(TRUE);
  // If that fails, let's build!
  $this->build_info = array(
    'query' => '',
    'count_query' => '',
    'query_args' => array(),


  // Call a module hook and see if it wants to present us with a
  // pre-built query or instruct us not to build the query for
  // some reason.
  // @todo: Implement this. Use the same mechanism Panels uses.

  // Run through our handlers and ensure they have necessary information.

  // Let the handlers interact with each other if they really want.

  if ($this->display_handler->uses_exposed()) {
    $exposed_form = $this->display_handler->get_plugin('exposed_form');
    // (1) Record the errors before rendering the exposed form widgets.
    $errors_before = form_set_error();
    $this->exposed_widgets = $exposed_form->render_exposed_form();
    // (2) Record the errors after rendering the exposed form widgets.
    $errors_after = form_set_error();
    // Find out if the validation of any of the elements in the exposed form
    // has failed by comparing (1) and (2) above. Don't mess with the view
    // otherwise.
    $exposed_errors = count($errors_after) > count($errors_before);
    if ($exposed_errors || !empty($this->build_info['abort'])) {
      $this->built = TRUE;
      // Don't execute the query, but rendering will still be executed to display the empty text.
      $this->executed = TRUE;
      return empty($this->build_info['fail']);

  // Build all the relationships first thing.

  // Set the filtering groups.
  if (!empty($this->filter)) {
    $filter_groups = $this->display_handler->get_option('filter_groups');
    if ($filter_groups) {
      foreach ($filter_groups['groups'] as $id => $operator) {
        $this->query->set_where_group($operator, $id);

  // Build all the filters.

  $this->build_sort = TRUE;

  // Arguments can, in fact, cause this whole thing to abort.
  if (!$this->_build_arguments()) {
    $this->build_time = microtime(TRUE) - $start;
    return $this->built;

  // Initialize the style; arguments may have changed which style we use,
  // so waiting as long as possible is important. But we need to know
  // about the style when we go to build fields.
  if (!$this->init_style()) {
    $this->build_info['fail'] = TRUE;
    return FALSE;

  if ($this->style_plugin->uses_fields()) {

  // Build our sort criteria if we were instructed to do so.
  if (!empty($this->build_sort)) {
    // Allow the style handler to deal with sorting.
    if ($this->style_plugin->build_sort()) {
    // allow the plugin to build second sorts as well.

  // Allow area handlers to affect the query.

  // Allow display handler to affect the query:

  // Allow style handler to affect the query:

  // Allow exposed form to affect the query:
  if (isset($exposed_form)) {

  if (config_get('views.settings', 'sql_signature')) {

  // Let modules modify the query just prior to finalizing it.

  // Only build the query if we weren't interrupted.
  if (empty($this->built)) {
    // Build the necessary info to execute the query.

  $this->built = TRUE;
  $this->build_time = microtime(TRUE) - $start;

  // Attach displays

  // Let modules modify the view just after building it.
  foreach (module_implements('views_post_build') as $module) {
    $function = $module . '_views_post_build';

  return TRUE;