SiteConfigureForm.php 10.7 KB
Newer Older
1 2 3 4 5 6 7 8 9
<?php

/**
 * @file
 * Contains \Drupal\Core\Installer\Form\SiteConfigureForm.
 */

namespace Drupal\Core\Installer\Form;

10
use Drupal\Core\Extension\ModuleInstallerInterface;
11
use Drupal\Core\Form\ConfigFormBase;
12
use Drupal\Core\Form\FormStateInterface;
13 14 15 16 17 18 19 20
use Drupal\Core\Locale\CountryManagerInterface;
use Drupal\Core\State\StateInterface;
use Drupal\user\UserStorageInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;

/**
 * Provides the site configuration form.
 */
21
class SiteConfigureForm extends ConfigFormBase {
22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37

  /**
   * The user storage.
   *
   * @var \Drupal\user\UserStorageInterface
   */
  protected $userStorage;

  /**
   * The state service.
   *
   * @var \Drupal\Core\State\StateInterface
   */
  protected $state;

  /**
38
   * The module installer.
39
   *
40
   * @var \Drupal\Core\Extension\ModuleInstallerInterface
41
   */
42
  protected $moduleInstaller;
43 44 45 46 47 48 49 50

  /**
   * The country manager.
   *
   * @var \Drupal\Core\Locale\CountryManagerInterface
   */
  protected $countryManager;

51 52 53 54 55 56 57
  /**
   * The app root.
   *
   * @var string
   */
  protected $root;

58 59 60
  /**
   * Constructs a new SiteConfigureForm.
   *
61 62
   * @param string $root
   *   The app root.
63 64 65 66
   * @param \Drupal\user\UserStorageInterface $user_storage
   *   The user storage.
   * @param \Drupal\Core\State\StateInterface $state
   *   The state service.
67 68
   * @param \Drupal\Core\Extension\ModuleInstallerInterface $module_installer
   *   The module installer.
69 70 71
   * @param \Drupal\Core\Locale\CountryManagerInterface $country_manager
   *   The country manager.
   */
72
  public function __construct($root, UserStorageInterface $user_storage, StateInterface $state, ModuleInstallerInterface $module_installer, CountryManagerInterface $country_manager) {
73
    $this->root = $root;
74 75
    $this->userStorage = $user_storage;
    $this->state = $state;
76
    $this->moduleInstaller = $module_installer;
77 78 79 80 81 82 83 84
    $this->countryManager = $country_manager;
  }

  /**
   * {@inheritdoc}
   */
  public static function create(ContainerInterface $container) {
    return new static(
85
      $container->get('app.root'),
86 87
      $container->get('entity.manager')->getStorage('user'),
      $container->get('state'),
88
      $container->get('module_installer'),
89 90 91 92 93 94 95 96 97 98 99
      $container->get('country_manager')
    );
  }

  /**
   * {@inheritdoc}
   */
  public function getFormId() {
    return 'install_configure_form';
  }

100 101 102 103 104 105 106 107 108 109 110
  /**
   * {@inheritdoc}
   */
  protected function getEditableConfigNames() {
    return [
      'system.date',
      'system.site',
      'update.settings',
    ];
  }

111 112 113
  /**
   * {@inheritdoc}
   */
114
  public function buildForm(array $form, FormStateInterface $form_state) {
115 116 117 118 119 120 121 122 123 124 125 126 127
    $form['#title'] = $this->t('Configure site');

    // Warn about settings.php permissions risk
    $settings_dir = conf_path();
    $settings_file = $settings_dir . '/settings.php';
    // Check that $_POST is empty so we only show this message when the form is
    // first displayed, not on the next page after it is submitted. (We do not
    // want to repeat it multiple times because it is a general warning that is
    // not related to the rest of the installation process; it would also be
    // especially out of place on the last page of the installer, where it would
    // distract from the message that the Drupal installation has completed
    // successfully.)
    $post_params = $this->getRequest()->request->all();
128
    if (empty($post_params) && (!drupal_verify_install_file($this->root . '/' . $settings_file, FILE_EXIST|FILE_READABLE|FILE_NOT_WRITABLE) || !drupal_verify_install_file($this->root . '/' . $settings_dir, FILE_NOT_WRITABLE, 'dir'))) {
129 130 131 132 133 134 135 136
      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>.', array('%dir' => $settings_dir, '%file' => $settings_file, '@handbook_url' => 'http://drupal.org/server-permissions')), 'warning');
    }

    $form['#attached']['library'][] = 'system/drupal.system';
    // Add JavaScript time zone detection.
    $form['#attached']['library'][] = 'core/drupal.timezone';
    // We add these strings as settings because JavaScript translation does not
    // work during installation.
137
    $form['#attached']['drupalSettings']['copyFieldValue']['edit-site-mail'] = ['edit-account-mail'];
138

139 140 141 142 143 144 145 146 147
    // Cache a fully-built schema. This is necessary for any invocation of
    // index.php because: (1) setting cache table entries requires schema
    // information, (2) that occurs during bootstrap before any module are
    // loaded, so (3) if there is no cached schema, drupal_get_schema() will
    // try to generate one but with no loaded modules will return nothing.
    //
    // @todo Move this to the 'install_finished' task?
    drupal_get_schema(NULL, TRUE);

148 149 150 151 152 153 154 155 156 157 158 159
    $form['site_information'] = array(
      '#type' => 'fieldgroup',
      '#title' => $this->t('Site information'),
    );
    $form['site_information']['site_name'] = array(
      '#type' => 'textfield',
      '#title' => $this->t('Site name'),
      '#required' => TRUE,
      '#weight' => -20,
    );
    $form['site_information']['site_mail'] = array(
      '#type' => 'email',
160
      '#title' => $this->t('Site email address'),
161
      '#default_value' => ini_get('sendmail_from'),
162
      '#description' => $this->t("Automated emails, such as registration information, will be sent from this address. Use an address ending in your site's domain to help prevent these emails from being flagged as spam."),
163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183
      '#required' => TRUE,
      '#weight' => -15,
    );

    $form['admin_account'] = array(
      '#type' => 'fieldgroup',
      '#title' => $this->t('Site maintenance account'),
    );
    $form['admin_account']['account']['name'] = array(
      '#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' => array('class' => array('username')),
    );
    $form['admin_account']['account']['pass'] = array(
      '#type' => 'password_confirm',
      '#required' => TRUE,
      '#size' => 25,
    );
184 185 186 187 188 189
    $form['admin_account']['account']['#tree'] = TRUE;
    $form['admin_account']['account']['mail'] = array(
      '#type' => 'email',
      '#title' => $this->t('Email address'),
      '#required' => TRUE,
    );
190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207

    $form['regional_settings'] = array(
      '#type' => 'fieldgroup',
      '#title' => $this->t('Regional settings'),
    );
    $countries = $this->countryManager->getList();
    $form['regional_settings']['site_default_country'] = array(
      '#type' => 'select',
      '#title' => $this->t('Default country'),
      '#empty_value' => '',
      '#default_value' => $this->config('system.date')->get('country.default'),
      '#options' => $countries,
      '#description' => $this->t('Select the default country for the site.'),
      '#weight' => 0,
    );
    $form['regional_settings']['date_default_timezone'] = array(
      '#type' => 'select',
      '#title' => $this->t('Default time zone'),
208 209
      // Use system timezone if set, but avoid throwing a warning in PHP >=5.4
      '#default_value' => @date_default_timezone_get(),
210 211 212 213 214 215 216 217 218 219 220 221 222 223 224
      '#options' => system_time_zones(),
      '#description' => $this->t('By default, dates in this site will be displayed in the chosen time zone.'),
      '#weight' => 5,
      '#attributes' => array('class' => array('timezone-detect')),
    );

    $form['update_notifications'] = array(
      '#type' => 'fieldgroup',
      '#title' => $this->t('Update notifications'),
    );
    $form['update_notifications']['update_status_module'] = array(
      '#type' => 'checkboxes',
      '#title' => $this->t('Update notifications'),
      '#options' => array(
        1 => $this->t('Check for updates automatically'),
225
        2 => $this->t('Receive email notifications'),
226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252
      ),
      '#default_value' => array(1, 2),
      '#description' => $this->t('The system will notify you when updates and important security releases are available for installed components. Anonymous information about your site is sent to <a href="@drupal">Drupal.org</a>.', array('@drupal' => 'http://drupal.org')),
      '#weight' => 15,
    );
    $form['update_notifications']['update_status_module'][2] = array(
      '#states' => array(
        'visible' => array(
          'input[name="update_status_module[1]"]' => array('checked' => TRUE),
        ),
      ),
    );

    $form['actions'] = array('#type' => 'actions');
    $form['actions']['submit'] = array(
      '#type' => 'submit',
      '#value' => $this->t('Save and continue'),
      '#weight' => 15,
      '#button_type' => 'primary',
    );

    return $form;
  }

  /**
   * {@inheritdoc}
   */
253
  public function validateForm(array &$form, FormStateInterface $form_state) {
254
    if ($error = user_validate_name($form_state->getValue(array('account', 'name')))) {
255
      $form_state->setErrorByName('account][name', $error);
256 257 258 259 260 261
    }
  }

  /**
   * {@inheritdoc}
   */
262
  public function submitForm(array &$form, FormStateInterface $form_state) {
263
    $this->config('system.site')
264 265 266
      ->set('name', (string) $form_state->getValue('site_name'))
      ->set('mail', (string) $form_state->getValue('site_mail'))
      ->save(TRUE);
267 268

    $this->config('system.date')
269 270 271
      ->set('timezone.default', (string) $form_state->getValue('date_default_timezone'))
      ->set('country.default', (string) $form_state->getValue('site_default_country'))
      ->save(TRUE);
272

273 274
    $account_values = $form_state->getValue('account');

275
    // Enable update.module if this option was selected.
276 277
    $update_status_module = $form_state->getValue('update_status_module');
    if ($update_status_module[1]) {
278
      $this->moduleInstaller->install(array('file', 'update'), FALSE);
279 280 281

      // Add the site maintenance account's email address to the list of
      // addresses to be notified when updates are available, if selected.
282
      if ($update_status_module[2]) {
283 284
        // Reset the configuration factory so it is updated with the new module.
        $this->resetConfigFactory();
285
        $this->config('update.settings')->set('notification.emails', array($account_values['mail']))->save(TRUE);
286 287 288 289 290
      }
    }

    // We precreated user 1 with placeholder values. Let's save the real values.
    $account = $this->userStorage->load(1);
291
    $account->init = $account->mail = $account_values['mail'];
292 293
    $account->roles = $account->getRoles();
    $account->activate();
294 295 296
    $account->timezone = $form_state->getValue('date_default_timezone');
    $account->pass = $account_values['pass'];
    $account->name = $account_values['name'];
297 298 299 300 301 302 303
    $account->save();

    // Record when this install ran.
    $this->state->set('install_time', $_SERVER['REQUEST_TIME']);
  }

}