Updated to Drupal 8.5. Core Media not yet in use.
[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\Form\FormBase;
6 use Drupal\Core\Form\FormStateInterface;
7
8 /**
9  * Provides the profile selection form.
10  *
11  * @internal
12  */
13 class SelectProfileForm extends FormBase {
14
15   /**
16    * {@inheritdoc}
17    */
18   public function getFormId() {
19     return 'install_select_profile_form';
20   }
21
22   /**
23    * {@inheritdoc}
24    */
25   public function buildForm(array $form, FormStateInterface $form_state, $install_state = NULL) {
26     $form['#title'] = $this->t('Select an installation profile');
27
28     $profiles = [];
29     $names = [];
30     foreach ($install_state['profiles'] as $profile) {
31       /** @var $profile \Drupal\Core\Extension\Extension */
32       $details = install_profile_info($profile->getName());
33       // Don't show hidden profiles. This is used by to hide the testing profile,
34       // which only exists to speed up test runs.
35       if ($details['hidden'] === TRUE && !drupal_valid_test_ua()) {
36         continue;
37       }
38       $profiles[$profile->getName()] = $details;
39
40       // Determine the name of the profile; default to file name if defined name
41       // is unspecified.
42       $name = isset($details['name']) ? $details['name'] : $profile->getName();
43       $names[$profile->getName()] = $name;
44     }
45
46     // Display radio buttons alphabetically by human-readable name, but always
47     // put the core profiles first (if they are present in the filesystem).
48     natcasesort($names);
49     if (isset($names['minimal'])) {
50       // If the expert ("Minimal") core profile is present, put it in front of
51       // any non-core profiles rather than including it with them alphabetically,
52       // since the other profiles might be intended to group together in a
53       // particular way.
54       $names = ['minimal' => $names['minimal']] + $names;
55     }
56     if (isset($names['standard'])) {
57       // If the default ("Standard") core profile is present, put it at the very
58       // top of the list. This profile will have its radio button pre-selected,
59       // so we want it to always appear at the top.
60       $names = ['standard' => $names['standard']] + $names;
61     }
62
63     // The profile name and description are extracted for translation from the
64     // .info file, so we can use $this->t() on them even though they are dynamic
65     // data at this point.
66     $form['profile'] = [
67       '#type' => 'radios',
68       '#title' => $this->t('Select an installation profile'),
69       '#title_display' => 'invisible',
70       '#options' => array_map([$this, 't'], $names),
71       '#default_value' => 'standard',
72     ];
73     foreach (array_keys($names) as $profile_name) {
74       $form['profile'][$profile_name]['#description'] = isset($profiles[$profile_name]['description']) ? $this->t($profiles[$profile_name]['description']) : '';
75       // @todo Remove hardcoding of 'demo_umami' profile for a generic warning
76       // system in https://www.drupal.org/project/drupal/issues/2822414.
77       if ($profile_name === 'demo_umami') {
78         $this->addUmamiWarning($form);
79       }
80     }
81     $form['actions'] = ['#type' => 'actions'];
82     $form['actions']['submit'] = [
83       '#type' => 'submit',
84       '#value' => $this->t('Save and continue'),
85       '#button_type' => 'primary',
86     ];
87     return $form;
88   }
89
90   /**
91    * {@inheritdoc}
92    */
93   public function submitForm(array &$form, FormStateInterface $form_state) {
94     global $install_state;
95     $install_state['parameters']['profile'] = $form_state->getValue('profile');
96   }
97
98   /**
99    * Show profile warning if 'demo_umami' profile is selected.
100    */
101   protected function addUmamiWarning(array &$form) {
102     // Warning to show when this profile is selected.
103     $description = $form['profile']['demo_umami']['#description'];
104     // Re-defines radio #description to show warning when selected.
105     $form['profile']['demo_umami']['#description'] = [
106       'warning' => [
107         '#type' => 'item',
108         '#markup' => $this->t('This profile is intended for demonstration purposes only.'),
109         '#wrapper_attributes' => [
110           'class' => ['messages', 'messages--warning'],
111         ],
112         '#states' => [
113           'visible' => [
114             ':input[name="profile"]' => ['value' => 'demo_umami'],
115           ],
116         ],
117       ],
118       'description' => ['#markup' => $description],
119     ];
120   }
121
122 }