function ViewExecutable::execute

Same name in other branches
  1. 9 core/modules/views/src/ViewExecutable.php \Drupal\views\ViewExecutable::execute()
  2. 10 core/modules/views/src/ViewExecutable.php \Drupal\views\ViewExecutable::execute()
  3. 11.x core/modules/views/src/ViewExecutable.php \Drupal\views\ViewExecutable::execute()

Executes the view's query.

Parameters

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

Return value

bool TRUE if the view execution was successful, FALSE otherwise. For example, an argument could stop the process.

2 calls to ViewExecutable::execute()
ViewExecutable::render in core/modules/views/src/ViewExecutable.php
Renders this view for a certain display.
ViewExecutable::__wakeup in core/modules/views/src/ViewExecutable.php
Magic method implementation to unserialize the view executable.

File

core/modules/views/src/ViewExecutable.php, line 1389

Class

ViewExecutable
Represents a view as a whole.

Namespace

Drupal\views

Code

public function execute($display_id = NULL) {
    if (empty($this->built)) {
        if (!$this->build($display_id)) {
            return FALSE;
        }
    }
    if (!empty($this->executed)) {
        return TRUE;
    }
    // Don't allow to use deactivated displays, but display them on the live preview.
    if (!$this->display_handler
        ->isEnabled() && empty($this->live_preview)) {
        $this->build_info['fail'] = TRUE;
        return FALSE;
    }
    // Let modules modify the view just prior to executing it.
    $module_handler = \Drupal::moduleHandler();
    $module_handler->invokeAll('views_pre_execute', [
        $this,
    ]);
    // Check for already-cached results.
    
    /** @var \Drupal\views\Plugin\views\cache\CachePluginBase $cache */
    if (!empty($this->live_preview)) {
        $cache = Views::pluginManager('cache')->createInstance('none');
    }
    else {
        $cache = $this->display_handler
            ->getPlugin('cache');
    }
    if ($cache->cacheGet('results')) {
        if ($this->usePager()) {
            $this->pager->total_items = $this->total_rows;
            $this->pager
                ->updatePageInfo();
        }
    }
    else {
        $this->query
            ->execute($this);
        // Enforce the array key rule as documented in
        // views_plugin_query::execute().
        $this->result = array_values($this->result);
        $this->_postExecute();
        $cache->cacheSet('results');
    }
    // Let modules modify the view just after executing it.
    $module_handler->invokeAll('views_post_execute', [
        $this,
    ]);
    return $this->executed = TRUE;
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.