Extra Block Types (EBT) - New Layout Builder experience❗

Extra Block Types (EBT) - styled, customizable block types: Slideshows, Tabs, Cards, Accordions and many others. Built-in settings for background, DOM Box, javascript plugins. Experience the future of layout building today.

Demo EBT modules Download EBT modules

❗Extra Paragraph Types (EPT) - New Paragraphs experience

Extra Paragraph Types (EPT) - analogical paragraph based set of modules.

Demo EPT modules Download EPT modules

Scroll

Creating an object for database connection

13/04/2025, by Ivan

Interaction with the database should be done through the database connection object. There are several scenarios that require attention:

1. In procedural code, i.e., *.module, *.inc, or script files:
The best way to instantiate the database connection object is via the Service Container.

Example:

$database = \Drupal::database();
// Or
$database = \Drupal::service('database');

This will return a database connection object configured to connect to the default primary database, as defined in the database configuration in the settings.php file.

2. For historical and technical reasons, the type returned by \Drupal::database() is \Drupal\Core\Database\Connection, and is therefore sometimes referred to as $connection.
 
3. In OOP code:

  • In some cases, the database connection object may already be available as a member of the current class; for example, many plugins and services have $this->database (or $this->connection) as the database connection object.
  • If possible, use DI (dependency injection) to use the @database service or $container->get('database'); to inject the database connection.
  • If that is not possible (such as in a static method of a class), use \Drupal::database().
  • If services are not yet available, \Drupal\Core\Database\Database::getConnection() can be used to get the database connection.
  • In unit tests, we don’t have a loaded kernel or a bootstrapped container. Unit tests typically should not access the database. A unit test that requires the database service should be converted into a kernel test.
  • In core classes and functional tests, we have $this->container->get('database'). Some test authors may find that the container referenced by the test class may be out of sync with the current container during a request in a functional test. In that case, the test author can call $this->rebuildContainer() and then access $this->container->get('database') again.

Using a different database connection

If your site uses multiple databases, to run a query against a database other than the default one, use Database::getConnection(). For example:

$connection = \Drupal\Core\Database\Database::getConnection('default', 'other_database');

This will provide a connection to the database defined in settings.php as:

$databases['other_database']['default']

Drupal’s online documentation is © 2000-2020 by the individual contributors and can be used in accordance with the Creative Commons License, Attribution-ShareAlike 2.0. PHP code is distributed under the GNU General Public License.