View source
<?php
namespace Drupal\config_installer\Form;
use Drupal\Core\Extension\ModuleHandlerInterface;
use Drupal\Core\Form\FormBase;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Site\Settings;
use Drupal\Core\State\StateInterface;
use Drupal\user\UserStorageInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
class SiteConfigureForm extends FormBase {
protected $userStorage;
protected $state;
protected $root;
protected $sitePath;
protected $moduleHandler;
public function __construct($root, $site_path, UserStorageInterface $user_storage, StateInterface $state, ModuleHandlerInterface $module_handler) {
$this->root = $root;
$this->sitePath = $site_path;
$this->userStorage = $user_storage;
$this->state = $state;
$this->moduleHandler = $module_handler;
}
public static function create(ContainerInterface $container) {
return new static($container
->get('app.root'), $container
->get('site.path'), $container
->get('entity.manager')
->getStorage('user'), $container
->get('state'), $container
->get('module_handler'));
}
public function getFormId() {
return 'config_installer_site_configure_form';
}
public function buildForm(array $form, FormStateInterface $form_state) {
$form['#title'] = $this
->t('Configure site');
$settings_file = $this->sitePath . '/settings.php';
$post_params = $this
->getRequest()->request
->all();
if (empty($post_params)) {
$original_profile_name = _config_installer_get_original_install_profile();
$need_to_write_settings = $original_profile_name !== Settings::get('install_profile');
if (version_compare(\Drupal::VERSION, '8.3', '>=') && !is_writable(\Drupal::service('site.path') . '/settings.php')) {
\Drupal::service('kernel')
->invalidateContainer();
$need_to_write_settings = FALSE;
}
if ($need_to_write_settings) {
$settings['settings']['install_profile'] = (object) [
'value' => $original_profile_name,
'required' => TRUE,
];
drupal_rewrite_settings($settings);
}
if (!drupal_verify_install_file($this->root . '/' . $settings_file, FILE_EXIST | FILE_READABLE | FILE_NOT_WRITABLE) || !drupal_verify_install_file($this->root . '/' . $this->sitePath, FILE_NOT_WRITABLE, 'dir')) {
drupal_set_message(t('All necessary changes to %dir and %file have been made, so you should remove write permissions to them now in order to avoid security risks. If you are unsure how to do so, consult the <a href="@handbook_url">online handbook</a>.', [
'%dir' => $this->sitePath,
'%file' => $settings_file,
'@handbook_url' => 'http://drupal.org/server-permissions',
]), 'warning');
}
}
$form['#attached']['library'][] = 'system/drupal.system';
$form['admin_account'] = [
'#type' => 'fieldgroup',
'#title' => $this
->t('Site maintenance account'),
];
$form['admin_account']['account']['name'] = [
'#type' => 'textfield',
'#title' => $this
->t('Username'),
'#maxlength' => USERNAME_MAX_LENGTH,
'#description' => $this
->t('Spaces are allowed; punctuation is not allowed except for periods, hyphens, and underscores.'),
'#required' => TRUE,
'#attributes' => [
'class' => [
'username',
],
],
];
$form['admin_account']['account']['pass'] = [
'#type' => 'password_confirm',
'#required' => TRUE,
'#size' => 25,
];
$form['admin_account']['account']['#tree'] = TRUE;
$form['admin_account']['account']['mail'] = [
'#type' => 'email',
'#title' => $this
->t('Email address'),
'#required' => TRUE,
];
if (function_exists('drush_get_option')) {
$form['admin_account']['account']['name']['#default_value'] = drush_get_option('account-name', 'admin');
$form['admin_account']['account']['pass']['#type'] = 'textfield';
if (class_exists('\\Drush\\Utils\\StringUtils')) {
$default_password = drush_get_option('account-pass', \Drush\Utils\StringUtils::generatePassword());
}
elseif (function_exists('drush_generate_password')) {
$default_password = drush_get_option('account-pass', drush_generate_password());
}
$form['admin_account']['account']['pass']['#default_value'] = $default_password;
$form['admin_account']['account']['mail']['#default_value'] = drush_get_option('account-mail', 'admin@example.com');
}
$form['actions'] = [
'#type' => 'actions',
];
$form['actions']['submit'] = [
'#type' => 'submit',
'#value' => $this
->t('Save and continue'),
'#weight' => 15,
'#button_type' => 'primary',
];
return $form;
}
public function validateForm(array &$form, FormStateInterface $form_state) {
if ($error = user_validate_name($form_state
->getValue([
'account',
'name',
]))) {
$form_state
->setErrorByName('account][name', $error);
}
}
public function submitForm(array &$form, FormStateInterface $form_state) {
$account_values = $form_state
->getValue('account');
$account = $this->userStorage
->load(1);
$account->init = $account->mail = $account_values['mail'];
$account->roles = $account
->getRoles();
$account
->activate();
$account->timezone = $form_state
->getValue('date_default_timezone');
$account->pass = $account_values['pass'];
$account->name = $account_values['name'];
$account
->save();
$this->state
->set('install_time', $_SERVER['REQUEST_TIME']);
if (version_compare(\Drupal::VERSION, '8.3', '>=')) {
\Drupal::service('kernel')
->invalidateContainer();
}
}
}