You are here

function view::build in Views (for Drupal 7) 6.2

Same name and namespace in other branches
  1. 6.3 includes/view.inc \view::build()
  2. 7.3 includes/view.inc \view::build()

Build the query for the view.

1 call to view::build()
view::execute in includes/view.inc
Execute the view's query.

File

includes/view.inc, line 594
view.inc Provides the view object type and associated methods.

Class

view
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.

Code

function build($display_id = NULL) {
  if (!empty($this->built)) {
    return;
  }
  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';
    $function($this);
  }

  // Attempt to load from cache.
  // @todo Load a build_info from cache.
  $start = views_microtime();

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

  // 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.
  $this
    ->init_handlers();

  // Let the handlers interact with each other if they really want.
  $this
    ->_pre_query();
  if ($this->display_handler
    ->uses_exposed()) {
    $this->exposed_widgets = $this
      ->render_exposed_form();
    if (form_set_error() || !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.
  $this
    ->_build('relationship');

  // Build all the filters.
  $this
    ->_build('filter');
  $this->build_sort = TRUE;

  // Arguments can, in fact, cause this whole thing to abort.
  if (!$this
    ->_build_arguments()) {
    $this->build_time = views_microtime() - $start;
    $this
      ->attach_displays();
    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()) {
    $this
      ->_build('field');
  }

  // 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()) {
      $this
        ->_build('sort');
    }

    // allow the plugin to build second sorts as well.
    $this->style_plugin
      ->build_sort_post();
  }

  // Allow display handler to affect the query:
  $this->display_handler
    ->query();

  // Allow style handler to affect the query:
  $this->style_plugin
    ->query();
  if (variable_get('views_sql_signature', FALSE)) {
    $this->query
      ->add_field(NULL, "'" . $this->name . ':' . $this->current_display . "'", 'view_name');
  }

  // Let modules modify the query just prior to finalizing it.
  foreach (module_implements('views_query_alter') as $module) {
    $function = $module . '_views_query_alter';
    $function($this, $this->query);
  }
  $this->build_info['query'] = $this->query
    ->query();
  $this->build_info['count_query'] = $this->query
    ->query(TRUE);
  $this->build_info['query_args'] = $this->query
    ->get_where_args();
  $this->built = TRUE;
  $this->build_time = views_microtime() - $start;

  // Attach displays
  $this
    ->attach_displays();

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