Now updated for Drupal 10!
This CodeLet enables you to create the specified number of Drupal sub-sites. It requires the path to the MySQL executable on your server. The sub-sites will be initialized with data from an SQL file.
// auto_site.routing.yml
auto_site.admin_settings:
path: '/admin/settings/auto-site'
defaults:
_form: '\Drupal\auto_site\Form\AutoSiteAdminForm'
_title: 'Auto site generator'
requirements:
_permission: 'administer auto site generator'
auto_site.admin_generate:
path: '/admin/settings/auto-site/generate'
defaults:
_form: '\Drupal\auto_site\Form\AutoSiteGenerateForm'
_title: 'Generate sites'
requirements:
_permission: 'administer auto site generator'
use Drupal\Core\Form\FormBase;
use Drupal\Core\Form\FormStateInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Drupal\Core\StringTranslation\StringTranslationTrait;
/**
* Implements hook_help().
*/
function auto_site_help($route_name, RouteMatchInterface $route_match) {
switch ($route_name) {
case 'auto_site.admin_settings':
return t('Configure paths for MySQL, pattern file, and the path to the directory to which the exported db will be stored.');
}
}
/**
* Implements hook_permissions().
*/
function auto_site_permissions() {
return [
'administer auto site generator' => [
'title' => t('Administer auto site generator'),
],
];
}
?>
<?php
namespace Drupal\auto_site\Form;
use Drupal\Core\Form\ConfigFormBase;
use Drupal\Core\Form\FormStateInterface;
class AutoSiteAdminForm extends ConfigFormBase {
use StringTranslationTrait;
/**
* {@inheritdoc}
*/
protected function getEditableConfigNames() {
return ['auto_site.settings'];
}
/**
* {@inheritdoc}
*/
public function getFormId() {
return 'auto_site_admin_form';
}
/**
* Builds the form.
*
* {@inheritdoc}
*/
public function buildForm(array $form, FormStateInterface $form_state) {
$form['mysql_path'] = [
'#type' => 'textfield',
'#title' => $this->t('MySQL Path'),
'#default_value' => $this->config('auto_site.settings')->get('mysql_path'),
];
$form['pattern_file'] = [
'#type' => 'textfield',
'#title' => $this->t('Pattern File'),
'#default_value' => $this->config('auto_site.settings')->get('pattern_file'),
];
$form['export_directory'] = [
'#type' => 'textfield',
'#title' => $this->t('Export Directory Path'),
'#default_value' => $this->config('auto_site.settings')->get('export_directory'),
];
return parent::buildForm($form, $form_state);
}
public function submitForm(array &$form, FormStateInterface $form_state) {
$config = $this->config('your_module_name.settings');
$site_pattern = $config->get('auto_site_pattern');
$site_count = $config->get('auto_site_count');
$domain = $config->get('auto_domain');
if ($site_pattern) {
$db_pattern = $config->get('auto_db_pattern');
$mysql_path = $config->get('auto_mysql_path');
$upload_path = $config->get('auto_upload_path');
$db_path = $form_state->getValue('auto_mysql_db');
$db_user = $form_state->getValue('auto_mysql_username');
$db_password = $form_state->getValue('auto_mysql_password');
$db_host = $form_state->getValue('auto_mysql_host');
$source = DRUPAL_ROOT . '/sites/default/default.settings.php';
$db = $db_pattern . '.' . $domain;
$db_user:$db_password@$db_host/$db;
try {
// Copy default settings to new site directory.
$this->fileSystem->copy($source, "$upload_path/$db/settings.php");
// Append database configuration to the settings file.
file_put_contents("$upload_path/$db/settings.php", "\$databases['default']['default'] = [
'driver' => 'mysql',
'database' => '$db',
'username' => '$db_user',
'password' => '$db_password',
'host' => '$db_host',
];", FILE_APPEND);
// Run additional scripts or commands if necessary.
shell_exec("$mysql_path -u$db_user -p$db_password -h$db_host $db");
$this->messenger()->addMessage(t('Site generated successfully.'));
} catch (\Exception $e) {
$this->messenger()->addError(t('Failed to generate site: @message', ['@message' => $e->getMessage()]));
}
}
}
name: 'Auto Site Generator'
type: module
description: 'Automatically generate sub-sites with pre-populated identical databases.'
core_version_requirement: ^10
package: Custom
dependencies:
- drupal:system (^8.8 || ^9 || ^10)
# Add other module dependencies if needed, for example:
# - drupal:views
# Optional property to provide a link for the project page or repository
project_link: 'https://your-project-link.com'
# Optional property for maintainers or authors
maintainers:
- name: 'DrupalD'
email: 'drupald@example.com'