You are here

public function ViewExecutable::render in Views (for Drupal 7) 8.3

Render this view for a certain display.

Note: You should better use just the preview function if you want to render a view.

Parameters

string $display_id: The machine name of the display, which should be rendered.

Return value

(string|NULL) Return the output of the rendered view or NULL if something failed in the process.

File

lib/Drupal/views/ViewExecutable.php, line 1228
Definition of Drupal\views\ViewExecutable.

Class

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

Namespace

Drupal\views

Code

public function render($display_id = NULL) {
  $this
    ->execute($display_id);

  // Check to see if the build failed.
  if (!empty($this->build_info['fail'])) {
    return;
  }
  if (!empty($this->build_info['denied'])) {
    return;
  }
  drupal_theme_initialize();
  $config = config('views.settings');

  // Set the response so other parts can alter it.
  $this->response = new Response('', 200);
  $start = microtime(TRUE);
  if (!empty($this->live_preview) && $config
    ->get('ui.show.additional_queries')) {
    $this
      ->startQueryCapture();
  }
  $exposed_form = $this->display_handler
    ->getPlugin('exposed_form');
  $exposed_form
    ->pre_render($this->result);

  // Check for already-cached output.
  if (!empty($this->live_preview)) {
    $cache = FALSE;
  }
  else {
    $cache = $this->display_handler
      ->getPlugin('cache');
  }
  if ($cache && $cache
    ->cache_get('output')) {
  }
  else {
    if ($cache) {
      $cache
        ->cache_start();
    }

    // Run pre_render for the pager as it might change the result.
    if (!empty($this->pager)) {
      $this->pager
        ->pre_render($this->result);
    }

    // Initialize the style plugin.
    $this
      ->initStyle();

    // Give field handlers the opportunity to perform additional queries
    // using the entire resultset prior to rendering.
    if ($this->style_plugin
      ->usesFields()) {
      foreach ($this->field as $id => $handler) {
        if (!empty($this->field[$id])) {
          $this->field[$id]
            ->pre_render($this->result);
        }
      }
    }
    $this->style_plugin
      ->pre_render($this->result);

    // Let modules modify the view just prior to rendering it.
    foreach (module_implements('views_pre_render') as $module) {
      $function = $module . '_views_pre_render';
      $function($this);
    }

    // Let the themes play too, because pre render is a very themey thing.
    foreach ($GLOBALS['base_theme_info'] as $base) {
      $function = $base->name . '_views_pre_render';
      if (function_exists($function)) {
        $function($this);
      }
    }
    $function = $GLOBALS['theme'] . '_views_pre_render';
    if (function_exists($function)) {
      $function($this);
    }
    $this->display_handler->output = $this->display_handler
      ->render();
    if ($cache) {
      $cache
        ->cache_set('output');
    }
  }
  $exposed_form
    ->post_render($this->display_handler->output);
  if ($cache) {
    $cache
      ->post_render($this->display_handler->output);
  }

  // Let modules modify the view output after it is rendered.
  foreach (module_implements('views_post_render') as $module) {
    $function = $module . '_views_post_render';
    $function($this, $this->display_handler->output, $cache);
  }

  // Let the themes play too, because post render is a very themey thing.
  foreach ($GLOBALS['base_theme_info'] as $base) {
    $function = $base->name . '_views_post_render';
    if (function_exists($function)) {
      $function($this);
    }
  }
  $function = $GLOBALS['theme'] . '_views_post_render';
  if (function_exists($function)) {
    $function($this, $this->display_handler->output, $cache);
  }
  if (!empty($this->live_preview) && $config
    ->get('ui.show.additional_queries')) {
    $this
      ->endQueryCapture();
  }
  $this->render_time = microtime(TRUE) - $start;
  return $this->display_handler->output;
}