3 namespace Drupal\Core\Installer\Form;
5 use Drupal\Core\Config\FileStorage;
6 use Drupal\Core\Form\FormBase;
7 use Drupal\Core\Form\FormStateInterface;
10 * Provides the profile selection form.
14 class SelectProfileForm extends FormBase {
17 * The key used in the profile list for the install from config option.
19 * This key must not be a valid profile extension name.
21 const CONFIG_INSTALL_PROFILE_KEY = '::existing_config::';
26 public function getFormId() {
27 return 'install_select_profile_form';
33 public function buildForm(array $form, FormStateInterface $form_state, $install_state = NULL) {
34 global $config_directories;
35 $form['#title'] = $this->t('Select an installation profile');
39 foreach ($install_state['profiles'] as $profile) {
40 /** @var $profile \Drupal\Core\Extension\Extension */
41 $details = install_profile_info($profile->getName());
42 // Don't show hidden profiles. This is used by to hide the testing profile,
43 // which only exists to speed up test runs.
44 if ($details['hidden'] === TRUE && !drupal_valid_test_ua()) {
47 $profiles[$profile->getName()] = $details;
49 // Determine the name of the profile; default to file name if defined name
51 $name = isset($details['name']) ? $details['name'] : $profile->getName();
52 $names[$profile->getName()] = $name;
55 // Display radio buttons alphabetically by human-readable name, but always
56 // put the core profiles first (if they are present in the filesystem).
58 if (isset($names['minimal'])) {
59 // If the expert ("Minimal") core profile is present, put it in front of
60 // any non-core profiles rather than including it with them alphabetically,
61 // since the other profiles might be intended to group together in a
63 $names = ['minimal' => $names['minimal']] + $names;
65 if (isset($names['standard'])) {
66 // If the default ("Standard") core profile is present, put it at the very
67 // top of the list. This profile will have its radio button pre-selected,
68 // so we want it to always appear at the top.
69 $names = ['standard' => $names['standard']] + $names;
72 // The profile name and description are extracted for translation from the
73 // .info file, so we can use $this->t() on them even though they are dynamic
74 // data at this point.
77 '#title' => $this->t('Select an installation profile'),
78 '#title_display' => 'invisible',
79 '#options' => array_map([$this, 't'], $names),
80 '#default_value' => 'standard',
82 foreach (array_keys($names) as $profile_name) {
83 $form['profile'][$profile_name]['#description'] = isset($profiles[$profile_name]['description']) ? $this->t($profiles[$profile_name]['description']) : '';
84 // @todo Remove hardcoding of 'demo_umami' profile for a generic warning
85 // system in https://www.drupal.org/project/drupal/issues/2822414.
86 if ($profile_name === 'demo_umami') {
87 $this->addUmamiWarning($form);
91 if (!empty($config_directories[CONFIG_SYNC_DIRECTORY])) {
92 $sync = new FileStorage($config_directories[CONFIG_SYNC_DIRECTORY]);
93 $extensions = $sync->read('core.extension');
94 $site = $sync->read('system.site');
95 if (isset($site['name']) && isset($extensions['profile']) && in_array($extensions['profile'], array_keys($names), TRUE)) {
96 // Ensure the the profile can be installed from configuration. Install
97 // profile's which implement hook_INSTALL() are not supported.
98 // @todo https://www.drupal.org/project/drupal/issues/2982052 Remove
100 module_load_install($extensions['profile']);
101 if (!function_exists($extensions['profile'] . '_install')) {
102 $form['profile']['#options'][static::CONFIG_INSTALL_PROFILE_KEY] = $this->t('Use existing configuration');
103 $form['profile'][static::CONFIG_INSTALL_PROFILE_KEY]['#description'] = [
105 '#markup' => $this->t('Install %name using existing configuration.', ['%name' => $site['name']]),
109 '#markup' => $this->t('The configuration from the directory %sync_directory will be used.', ['%sync_directory' => $config_directories[CONFIG_SYNC_DIRECTORY]]),
110 '#wrapper_attributes' => [
111 'class' => ['messages', 'messages--status'],
115 ':input[name="profile"]' => ['value' => static::CONFIG_INSTALL_PROFILE_KEY],
124 $form['actions'] = ['#type' => 'actions'];
125 $form['actions']['submit'] = [
127 '#value' => $this->t('Save and continue'),
128 '#button_type' => 'primary',
136 public function submitForm(array &$form, FormStateInterface $form_state) {
137 global $install_state, $config_directories;
138 $profile = $form_state->getValue('profile');
139 if ($profile === static::CONFIG_INSTALL_PROFILE_KEY) {
140 $sync = new FileStorage($config_directories[CONFIG_SYNC_DIRECTORY]);
141 $profile = $sync->read('core.extension')['profile'];
142 $install_state['parameters']['existing_config'] = TRUE;
144 $install_state['parameters']['profile'] = $profile;
148 * Show profile warning if 'demo_umami' profile is selected.
150 protected function addUmamiWarning(array &$form) {
151 // Warning to show when this profile is selected.
152 $description = $form['profile']['demo_umami']['#description'];
153 // Re-defines radio #description to show warning when selected.
154 $form['profile']['demo_umami']['#description'] = [
157 '#markup' => $this->t('This profile is intended for demonstration purposes only.'),
158 '#wrapper_attributes' => [
159 'class' => ['messages', 'messages--warning'],
163 ':input[name="profile"]' => ['value' => 'demo_umami'],
167 'description' => ['#markup' => $description],