Pull merge.
[yaffs-website] / web / core / lib / Drupal / Core / Installer / Form / SelectProfileForm.php
1 <?php
2
3 namespace Drupal\Core\Installer\Form;
4
5 use Drupal\Core\Config\FileStorage;
6 use Drupal\Core\Form\FormBase;
7 use Drupal\Core\Form\FormStateInterface;
8
9 /**
10  * Provides the profile selection form.
11  *
12  * @internal
13  */
14 class SelectProfileForm extends FormBase {
15
16   /**
17    * The key used in the profile list for the install from config option.
18    *
19    * This key must not be a valid profile extension name.
20    */
21   const CONFIG_INSTALL_PROFILE_KEY = '::existing_config::';
22
23   /**
24    * {@inheritdoc}
25    */
26   public function getFormId() {
27     return 'install_select_profile_form';
28   }
29
30   /**
31    * {@inheritdoc}
32    */
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');
36
37     $profiles = [];
38     $names = [];
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()) {
45         continue;
46       }
47       $profiles[$profile->getName()] = $details;
48
49       // Determine the name of the profile; default to file name if defined name
50       // is unspecified.
51       $name = isset($details['name']) ? $details['name'] : $profile->getName();
52       $names[$profile->getName()] = $name;
53     }
54
55     // Display radio buttons alphabetically by human-readable name, but always
56     // put the core profiles first (if they are present in the filesystem).
57     natcasesort($names);
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
62       // particular way.
63       $names = ['minimal' => $names['minimal']] + $names;
64     }
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;
70     }
71
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.
75     $form['profile'] = [
76       '#type' => 'radios',
77       '#title' => $this->t('Select an installation profile'),
78       '#title_display' => 'invisible',
79       '#options' => array_map([$this, 't'], $names),
80       '#default_value' => 'standard',
81     ];
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);
88       }
89     }
90
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
99         //   this restriction.
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'] = [
104             'description' => [
105               '#markup' => $this->t('Install %name using existing configuration.', ['%name' => $site['name']]),
106             ],
107             'info' => [
108               '#type' => 'item',
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'],
112               ],
113               '#states' => [
114                 'visible' => [
115                   ':input[name="profile"]' => ['value' => static::CONFIG_INSTALL_PROFILE_KEY],
116                 ],
117               ],
118             ],
119           ];
120         }
121       }
122     }
123
124     $form['actions'] = ['#type' => 'actions'];
125     $form['actions']['submit'] = [
126       '#type' => 'submit',
127       '#value' => $this->t('Save and continue'),
128       '#button_type' => 'primary',
129     ];
130     return $form;
131   }
132
133   /**
134    * {@inheritdoc}
135    */
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;
143     }
144     $install_state['parameters']['profile'] = $profile;
145   }
146
147   /**
148    * Show profile warning if 'demo_umami' profile is selected.
149    */
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'] = [
155       'warning' => [
156         '#type' => 'item',
157         '#markup' => $this->t('This profile is intended for demonstration purposes only.'),
158         '#wrapper_attributes' => [
159           'class' => ['messages', 'messages--warning'],
160         ],
161         '#states' => [
162           'visible' => [
163             ':input[name="profile"]' => ['value' => 'demo_umami'],
164           ],
165         ],
166       ],
167       'description' => ['#markup' => $description],
168     ];
169   }
170
171 }