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?

9

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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