4

I am creating a module for Drupal 8. One of the requirements is that you first add a new database in settings.php. On the admin page, I want to check if the user did this step and show a warning if he didn't. I tried this:

global $databases;

But doing a var_dump shows nothing.

How do I access the databases variable so I can check if the database is setup?

1 Answer 1

10

global $databases has been removed in favor of the preexisting static connection info management methods on the Database class.

Drupal 7

global $databases;
// Check whether we have a default connection.
$has_connection = !empty($databases['default']['default']);

Drupal 8

use Drupal\Core\Database\Database;
$has_connection = (bool) Database::getConnectionInfo('default');

More links

https://www.drupal.org/node/2176621

https://www.drupal.org/node/1951216

1
  • do not force to bool.$has_connection = Database::getConnectionInfo('default'); dpm($has_connection);
    – skaught
    Dec 9, 2019 at 15:27

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.