preExecute example

$this->config('views.settings')->set('display_extenders', ['display_extender_test'])->save();
    $this->assertCount(1, Views::getEnabledDisplayExtenders(), 'Make sure that there is only one enabled display extender.');

    $view = Views::getView('test_view');
    $view->initDisplay();

    $this->assertCount(1, $view->display_handler->getExtenders(), 'Make sure that only one extender is initialized.');

    $display_extender = $view->display_handler->getExtenders()['display_extender_test'];
    $this->assertInstanceOf(DisplayExtenderTestData::class$display_extender);

    $view->preExecute();
    $this->assertTrue($display_extender->testState['preExecute'], 'Make sure the display extender was able to react on preExecute.');
    $view->execute();
    $this->assertTrue($display_extender->testState['query'], 'Make sure the display extender was able to react on query.');
  }

  /** * Tests display extenders validation. */
  public function testDisplayExtendersValidate() {
    $this->config('views.settings')->set('display_extenders', ['display_extender_test_3'])->save();

    

  protected function executeView(ViewExecutable $view$args = []) {
    // A view does not really work outside of a request scope, due to many     // dependencies like the current user.     $view->setDisplay();
    $view->preExecute($args);
    $view->execute();
  }

  /** * Returns the schema definition. * * @internal */
  protected function schemaDefinition() {
    return ViewTestData::schemaDefinition();
  }

  

  public function preExecute() {
    $this->view->setAjaxEnabled($this->ajaxEnabled());
    if ($this->isMoreEnabled() && !$this->useMoreAlways()) {
      $this->view->get_total_rows = TRUE;
    }
    $this->view->initHandlers();
    if ($this->usesExposed()) {
      /** @var \Drupal\views\Plugin\views\exposed_form\ExposedFormPluginInterface $exposed_form */
      $exposed_form = $this->getPlugin('exposed_form');
      $exposed_form->preExecute();
    }

    foreach ($this->extenders as $extender) {
      $extender->preExecute();
    }
  }

  /** * {@inheritdoc} */
  public function calculateCacheMetadata() {
    


  /** * Executes the UPSERT operation. * * @return int * An integer indicating the number of rows affected by the operation. Do * not rely on this value as a precise indication of the actual rows * affected: different database engines return different values. */
  public function execute() {
    if (!$this->preExecute()) {
      return NULL;
    }

    $max_placeholder = 0;
    $values = [];
    foreach ($this->insertValues as $insert_values) {
      foreach ($insert_values as $value) {
        $values[':db_insert_placeholder_' . $max_placeholder++] = $value;
      }
    }

    
    $controller = $this->container->get('entity_type.manager')->getStorage('view');
    $views = $controller->loadMultiple();

    foreach ($views as $name => $view_storage) {
      $view = $view_storage->getExecutable();
      $view->initDisplay();
      foreach ($view->storage->get('display') as $display_id => $display) {
        $view->setDisplay($display_id);

        // Add any args if needed.         if (array_key_exists($name$this->viewArgMap)) {
          $view->preExecute($this->viewArgMap[$name]);
        }

        $view->execute();

        $tokens = ['@name' => $name, '@display_id' => $display_id];
        $this->assertTrue($view->executed, new FormattableMarkup('@name:@display_id has been executed.', $tokens));

        $this->assertNotEmpty($view->result);
        $view->destroy();
      }
    }
  }
/** * {@inheritdoc} */
  public function __construct(Connection $connection, string $table, array $options = []) {
    // @todo Remove the __construct in Drupal 11.     // @see https://www.drupal.org/project/drupal/issues/3256524     parent::__construct($connection$table$options);
    unset($this->queryOptions['return']);
  }

  public function execute() {
    if (!$this->preExecute()) {
      return NULL;
    }

    // If we're selecting from a SelectQuery, finish building the query and     // pass it back, as any remaining options are irrelevant.     if (empty($this->fromQuery)) {
      $max_placeholder = 0;
      $values = [];
      foreach ($this->insertValues as $insert_values) {
        foreach ($insert_values as $value) {
          $values[':db_insert_placeholder_' . $max_placeholder++] = $value;
        }
foreach ($this->alterTags as $tag => $value) {
        $hooks[] = 'query_' . $tag;
      }
      \Drupal::moduleHandler()->alter($hooks$query);
    }

    $this->prepared = TRUE;

    // Now also prepare any sub-queries.     foreach ($this->tables as $table) {
      if ($table['table'] instanceof SelectInterface) {
        $table['table']->preExecute();
      }
    }

    foreach ($this->union as $union) {
      $union['query']->preExecute();
    }

    return $this->prepared;
  }

  /** * {@inheritdoc} */

  public function generateResultsKey() {
    if (!isset($this->resultsKey)) {
      $build_info = $this->view->build_info;

      foreach (['query', 'count_query'] as $index) {
        // If the default query back-end is used generate SQL query strings from         // the query objects.         if ($build_info[$index] instanceof SelectInterface) {
          $query = clone $build_info[$index];
          $query->preExecute();
          $build_info[$index] = [
            'query' => (string) $query,
            'arguments' => $query->getArguments(),
          ];
        }
      }

      $key_data = [
        'build_info' => $build_info,
      ];
      // @todo https://www.drupal.org/node/2433591 might solve it to not require
/** * Executes a view. * * @param \Drupal\views\ViewExecutable $view * The view object. * @param array $args * (optional) An array of the view arguments to use for the view. */
  protected function executeView($view, array $args = []) {
    $view->setDisplay();
    $view->preExecute($args);
    $view->execute();
  }

  /** * Returns the schema definition. * * @internal */
  protected function schemaDefinition() {
    return ViewTestData::schemaDefinition();
  }

  
public function __construct(Connection $connection, string $table, array $options = []) {
    // @todo Remove the __construct in Drupal 11.     // @see https://www.drupal.org/project/drupal/issues/3256524     parent::__construct($connection$table$options);
    unset($this->queryOptions['return']);
  }

  /** * {@inheritdoc} */
  public function execute() {
    if (!$this->preExecute()) {
      return NULL;
    }

    $stmt = $this->connection->prepareStatement((string) $this$this->queryOptions, TRUE);

    // Fetch the list of blobs and sequences used on that table.     $table_information = $this->connection->schema()->queryTableInformation($this->table);

    $max_placeholder = 0;
    $blobs = [];
    $blob_count = 0;
    


  /** * {@inheritdoc} */
  public function preExecute(SelectInterface $query = NULL) {
    // If no query object is passed in, use $this.     if (!isset($query)) {
      $query = $this;
    }

    return $this->query->preExecute($query);
  }

  /** * {@inheritdoc} */
  public function execute() {
    // By calling preExecute() here, we force it to preprocess the extender     // object rather than just the base query object. That means     // hook_query_alter() gets access to the extended object.     if (!$this->preExecute($this)) {
      return NULL;
    }

              }
              $view->addHandler('default', $type$base_table$field$options);
            }
          }
        }
      }

      // Go through each step individually to see whether some parts are       // failing.       $view->build();
      $view->preExecute();
      $view->execute();
      $view->render();

      // Make sure all handlers extend the HandlerBase.       foreach ($object_types as $type) {
        if (isset($view->{$type})) {
          foreach ($view->{$type} as $handler) {
            $this->assertInstanceOf(HandlerBase::class$handler);
          }
        }
      }
    }

  public function execute() {
    // If validation fails, simply return NULL. Note that validation routines     // in preExecute() may throw exceptions instead.     if (!$this->preExecute()) {
      return NULL;
    }

    // If we're selecting from a SelectQuery, finish building the query and     // pass it back, as any remaining options are irrelevant.     if (!empty($this->fromQuery)) {
      $sql = (string) $this;
      // The SelectQuery may contain arguments, load and pass them through.       return $this->connection->query($sql$this->fromQuery->getArguments()$this->queryOptions);
    }

    

  public function preExecute(SelectInterface $query = NULL) {
    if (!$this->executedPrepare) {
      $this->prepareAndNormalize();
    }

    if (!$this->normalize) {
      return FALSE;
    }

    return parent::preExecute($query);
  }

  /** * Adds a custom score expression to the search query. * * Score expressions are used to order search results. If no calls to * addScore() have taken place, a default keyword relevance score will be * used. However, if at least one call to addScore() has taken place, the * keyword relevance score is not automatically added. * * Note that you must use this method to add ordering to your searches, and * not call orderBy() directly, when using the SearchQuery extender. This is * because of the two-pass system the SearchQuery class uses to normalize * scores. * * @param string $score * The score expression, which should evaluate to a number between 0 and 1. * The string 'i.relevance' in a score expression will be replaced by a * measure of keyword relevance between 0 and 1. * @param array $arguments * Query arguments needed to provide values to the score expression. * @param float $multiply * If set, the score is multiplied with this value. However, all scores * with multipliers are then divided by the total of all multipliers, so * that overall, the normalization is maintained. * * @return $this */

  public function executeDisplay($display_id = NULL, $args = []) {
    if (empty($this->current_display) || $this->current_display != $this->chooseDisplay($display_id)) {
      if (!$this->setDisplay($display_id)) {
        return NULL;
      }
    }

    $this->preExecute($args);

    // Execute the view     $output = $this->display_handler->execute();

    $this->postExecute();
    return $output;
  }

  /** * Previews the given display, with the given arguments. * * To be called externally, probably by an AJAX handler of some flavor. * Can also be called when views are embedded, as this guarantees * normalized output. * * This function does not do any access checks on the view. It is the * responsibility of the caller to check $view->access() or implement other * access logic. To render the view normally with access checks, use * views_embed_view() instead. * * @return array|null * A renderable array containing the view output or NULL if the display ID * of the view to be executed doesn't exist. */
Home | Imprint | This part of the site doesn't use cookies.