Skip to content

Instantly share code, notes, and snippets.

@tenken
Created March 29, 2012 22:17
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tenken/2244304 to your computer and use it in GitHub Desktop.
Save tenken/2244304 to your computer and use it in GitHub Desktop.
A Sample short profile that auto-fills the DB form, its a little ugly.
; dpg ::: http://drupal.org/user/1059226
; Example Short Profile -- hides database configuration page.
;
; This bears mentioning more than once. See http://drupal.org/node/1153646
name = Short Profile
description = A short profile, it removes DB settings page.
core = 7.x
dependencies[] = block
dependencies[] = color
dependencies[] = comment
dependencies[] = contextual
dependencies[] = dashboard
dependencies[] = help
dependencies[] = image
dependencies[] = list
dependencies[] = menu
dependencies[] = number
dependencies[] = options
dependencies[] = path
dependencies[] = taxonomy
dependencies[] = dblog
dependencies[] = search
dependencies[] = shortcut
dependencies[] = toolbar
dependencies[] = overlay
dependencies[] = field_ui
dependencies[] = file
dependencies[] = rdf
files[] = shortprofile.profile
<?php
function shortprofile_install() {
include_once DRUPAL_ROOT . 'profiles/minimal/minimal.install';
minimal_install();
}
function shortprofile_install_tasks_alter(&$tasks, $install_state) {
// Hide the database configuration step.
$tasks['install_settings_form']['display'] = FALSE;
// See this issue, that we cant just form_alter the database configuration
// page: http://drupal.org/node/1153646
//
// We supply our own database configuration form -- and by doing so also need
// to supply _submit and _validate functions herein. We just copy the
// source code from those functions into ours.
//
// If you use the *hack* in the above issue queue you dont need to do this
// function override of this settings form. You can just do a simple:
// system_form_install_settings_form() in your installation profile and
// supply default values I believe.
//
// But dragons be there! 'Ye be warned. It's not Kosher.
$tasks['install_settings_form']['function'] = 'shortprofile_install_settings_form';
}
/**
* Our custom database form. We force our own local values. Why, who knows.
*/
function shortprofile_install_settings_form($form, &$form_state, &$install_state) {
global $databases;
$profile = $install_state['parameters']['profile'];
$install_locale = $install_state['parameters']['locale'];
drupal_static_reset('conf_path');
$conf_path = './' . conf_path(FALSE);
$settings_file = $conf_path . '/settings.php';
// I added this line with our defaults.
$databases['default']['default'] = array(
'driver' => 'mysql',
'database' => 'drupal7_profiletest',
'username' => 'dev_user',
'password' => 'dev',
'host' => 'localhost',
'port' => '',
'prefix' => '',
);
// The rest of this function is almost the same ....
$database = isset($databases['default']['default']) ? $databases['default']['default'] : array();
drupal_set_title(st('Database configuration'));
$drivers = drupal_get_database_types();
$drivers_keys = array_keys($drivers);
$form['driver'] = array(
'#type' => 'radios',
'#title' => st('Database type'),
'#required' => TRUE,
'#default_value' => !empty($database['driver']) ? $database['driver'] : current($drivers_keys),
'#description' => st('The type of database your @drupal data will be stored in.', array('@drupal' => drupal_install_profile_distribution_name())),
);
if (count($drivers) == 1) {
$form['driver']['#disabled'] = TRUE;
$form['driver']['#description'] .= ' ' . st('Your PHP configuration only supports a single database type, so it has been automatically selected.');
}
// Add driver specific configuration options.
foreach ($drivers as $key => $driver) {
$form['driver']['#options'][$key] = $driver->name();
$form['settings'][$key] = $driver->getFormOptions($database);
$form['settings'][$key]['#prefix'] = '<h2 class="js-hide">' . st('@driver_name settings', array('@driver_name' => $driver->name())) . '</h2>';
$form['settings'][$key]['#type'] = 'container';
$form['settings'][$key]['#tree'] = TRUE;
$form['settings'][$key]['advanced_options']['#parents'] = array($key);
$form['settings'][$key]['#states'] = array(
'visible' => array(
':input[name=driver]' => array('value' => $key),
),
);
}
$form['actions'] = array('#type' => 'actions');
$form['actions']['save'] = array(
'#type' => 'submit',
'#value' => st('Save and continue'),
'#limit_validation_errors' => array(
array('driver'),
array(isset($form_state['input']['driver']) ? $form_state['input']['driver'] : current($drivers_keys)),
),
// I Edited this line, to point ot our custom submit function.
'#submit' => array('shortprofile_install_settings_form_submit'),
);
$form['errors'] = array();
$form['settings_file'] = array(
'#type' => 'value',
'#value' => $settings_file,
);
return $form;
}
/**
* A custom validate function, copied from Core.
*/
function shortprofile_install_settings_form_validate($form, &$form_state) {
$driver = $form_state['values']['driver'];
$database = $form_state['values'][$driver];
// I added this line, setting a default password in the _form() function
// didn't appear to work on the screen.
$database['password'] = 'dev';
$database['driver'] = $driver;
// TODO: remove when PIFR will be updated to use 'db_prefix' instead of
// 'prefix' in the database settings form.
$database['prefix'] = $database['db_prefix'];
unset($database['db_prefix']);
$form_state['storage']['database'] = $database;
$errors = install_database_errors($database, $form_state['values']['settings_file']);
foreach ($errors as $name => $message) {
form_set_error($name, $message);
}
}
/**
* Custom submit function. Copied from Core install_settings_form_submit().
*/
function shortprofile_install_settings_form_submit($form, &$form_state) {
global $install_state;
// Update global settings array and save.
$settings['databases'] = array(
'value' => array('default' => array('default' => $form_state['storage']['database'])),
'required' => TRUE,
);
$settings['drupal_hash_salt'] = array(
'value' => drupal_hash_base64(drupal_random_bytes(55)),
'required' => TRUE,
);
drupal_rewrite_settings($settings);
// 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();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment