You are here

trait ViewsSqlQueryGetConnectionTrait in Range 8

Gets the database connection to use for the view.

This compatibility layer is required in order to support overriding of the database query condition drivers. This change has been committed to both D8 and D9 in https://www.drupal.org/node/3113403. Then, the missing getConnection() method was added to the \Drupal\views\Plugin\views\query\Sql in https://www.drupal.org/node/3139353. Finally, creating an instance of the class Drupal\Core\Database\Query\Condition with the new keyword has been deprecated in https://www.drupal.org/node/3130655.

However, later two changes have not been committed to D8 making it impossible to have zero deprecations and support both D8 and D9. This trait fixed that.

Hierarchy

2 files declare their use of ViewsSqlQueryGetConnectionTrait
Range.php in src/Plugin/views/filter/Range.php
Range.php in src/Plugin/views/argument/Range.php

File

src/D8Compatibility/ViewsSqlQueryGetConnectionTrait.php, line 22

Namespace

Drupal\range\D8Compatibility
View source
trait ViewsSqlQueryGetConnectionTrait {

  /**
   * Gets the database connection to use for the view.
   *
   * The returned database connection does not have to be the default database
   * connection. It can also be to another database connection when the view is
   * to an external database or a replica database.
   *
   * @return \Drupal\Core\Database\Connection
   *   The database connection to be used for the query.
   */
  protected function getDatabaseConnection(Sql $query) {

    // Call the getConnection() method directly if exists (D9.1+).
    if (method_exists($query, 'getConnection')) {
      return $query
        ->getConnection();
    }
    else {

      // Set the replica target if the replica option is set for the view.
      $target = empty($query->options['replica']) ? 'default' : 'replica';

      // Use an external database when the view configured to.
      $key = $query->view->base_database ?? 'default';
      return Database::getConnection($target, $key);
    }
  }

}

Members

Namesort descending Modifiers Type Description Overrides
ViewsSqlQueryGetConnectionTrait::getDatabaseConnection protected function Gets the database connection to use for the view.