Hello all and thank you for any help in advance,

I'm attempting to automate the population of database settings on installation via an install profile in drupal 8. It works however immediately after selecting the profile on install I receive a database connection error. Hard refresh (F5), fixes it and the installation completes successfully. How do I stop attempts to connect to the database before the details have been defined? I have a similar working profile for 7. Here is my code in the profilename.profile:

function standard_install_tasks_alter(&$tasks, $install_state) {
// Hide the database configuration step.
$tasks['install_settings_form']['display'] = FALSE;
$tasks['install_settings_form']['function'] = 'standard_edited_install_settings_defaults';
$tasks['install_settings_form']['type'] = 'normal';
}

function standard_edited_install_settings_defaults(&$install_state){
// Get variables in.
$vars = array(
'DRUPAL_DB_HOST',
'DRUPAL_DB_PORT',
'DRUPAL_DB_USER',
'DRUPAL_DB_NAME',
'DRUPAL_DB_PASSWORD',
'DRUPAL_DB_DRIVER',
'DRUPAL_DB_PREFIX'
);
foreach ($vars as $var) {
if (!isset($_ENV[$var]) && getenv($var)) {
$_ENV[$var] = getenv($var);
}
}
// DB details array.
$database = array (
'driver' => $_ENV['DRUPAL_DB_DRIVER'],
'database' => $_ENV['DRUPAL_DB_NAME'],
'username' => $_ENV['DRUPAL_DB_USER'],
'password' => $_ENV['DRUPAL_DB_PASSWORD'],
'host' => $_ENV['DRUPAL_DB_HOST'],
'port' => $_ENV['DRUPAL_DB_PORT'],
'prefix' => isset($_ENV['DRUPAL_DB_PREFIX']) ? $_ENV['DRUPAL_DB_PREFIX'] : null,
);

global $install_state;

// Update global settings array and save.
$settings = array();
$settings['databases']['default']['default'] = (object) array(
'value' => $database,
'required' => TRUE,
);
$settings['settings']['hash_salt'] = (object) array(
'value' => Crypt::randomBytesBase64(55),
'required' => TRUE,
);

drupal_rewrite_settings($settings);

// Add the config directories to settings.php.
drupal_install_config_directories();

// Indicate that the settings file has been verified, and check the database
// for the last completed task, now that we have a valid connection. This
// last step is important since we want to trigger an error if the new
// database already has Drupal installed.
$install_state['settings_verified'] = TRUE;
$install_state['completed_task'] = install_verify_completed_task();
return NULL;
}

Comments

Viliar’s picture

I made this a bit hackish workaround:

---
1. save database settings and hash in site/default/settings.php
<?php

$databases['default']['default'] = array (
'database' => 'drupal',
'username' => 'xxx',
'password' => 'xxx',
'host' => 'localhost',
'port' => '3306',
'driver' => 'mysql',
'prefix' => '',
'collation' => 'utf8mb4_general_ci',
);
$settings['hash_salt'] = 'xxxxx';

2. add to core/profiles/$profile/$profile.profile the following code (change standard to appropriate profile name):

function standard_install_tasks_alter(&$tasks, $install_state) {
$tasks['install_settings_form']['display'] = FALSE;
$tasks['install_settings_form']['function'] = 'standard_install_settings_defaults';
$tasks['install_settings_form']['type'] = 'normal';
}

function standard_install_settings_defaults(&$install_state) {
return NULL;
}

3. and finally use this patch from https://www.drupal.org/node/728702 for correct redirection to install.php:
https://www.drupal.org/files/issues/728702-163.patch

---

I hope it would be useful for someone who puzzles over this task as did I.