SelectProfileForm.php 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. <?php
  2. namespace Drupal\Core\Installer\Form;
  3. use Drupal\Core\Config\FileStorage;
  4. use Drupal\Core\Form\FormBase;
  5. use Drupal\Core\Form\FormStateInterface;
  6. use Drupal\Core\Site\Settings;
  7. /**
  8. * Provides the profile selection form.
  9. *
  10. * @internal
  11. */
  12. class SelectProfileForm extends FormBase {
  13. /**
  14. * The key used in the profile list for the install from config option.
  15. *
  16. * This key must not be a valid profile extension name.
  17. */
  18. const CONFIG_INSTALL_PROFILE_KEY = '::existing_config::';
  19. /**
  20. * {@inheritdoc}
  21. */
  22. public function getFormId() {
  23. return 'install_select_profile_form';
  24. }
  25. /**
  26. * {@inheritdoc}
  27. */
  28. public function buildForm(array $form, FormStateInterface $form_state, $install_state = NULL) {
  29. $form['#title'] = $this->t('Select an installation profile');
  30. $profiles = [];
  31. $names = [];
  32. foreach ($install_state['profiles'] as $profile) {
  33. /** @var $profile \Drupal\Core\Extension\Extension */
  34. $details = install_profile_info($profile->getName());
  35. // Don't show hidden profiles. This is used by to hide the testing profile,
  36. // which only exists to speed up test runs.
  37. if ($details['hidden'] === TRUE && !drupal_valid_test_ua()) {
  38. continue;
  39. }
  40. $profiles[$profile->getName()] = $details;
  41. // Determine the name of the profile; default to file name if defined name
  42. // is unspecified.
  43. $name = isset($details['name']) ? $details['name'] : $profile->getName();
  44. $names[$profile->getName()] = $name;
  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. // The profile name and description are extracted for translation from the
  63. // .info file, so we can use $this->t() on them even though they are dynamic
  64. // data at this point.
  65. $form['profile'] = [
  66. '#type' => 'radios',
  67. '#title' => $this->t('Select an installation profile'),
  68. '#title_display' => 'invisible',
  69. '#options' => array_map([$this, 't'], $names),
  70. '#default_value' => 'standard',
  71. ];
  72. foreach (array_keys($names) as $profile_name) {
  73. $form['profile'][$profile_name]['#description'] = isset($profiles[$profile_name]['description']) ? $this->t($profiles[$profile_name]['description']) : '';
  74. // @todo Remove hardcoding of 'demo_umami' profile for a generic warning
  75. // system in https://www.drupal.org/project/drupal/issues/2822414.
  76. if ($profile_name === 'demo_umami') {
  77. $this->addUmamiWarning($form);
  78. }
  79. }
  80. $config_sync_directory = Settings::get('config_sync_directory');
  81. if (!empty($config_sync_directory)) {
  82. $sync = new FileStorage($config_sync_directory);
  83. $extensions = $sync->read('core.extension');
  84. $site = $sync->read('system.site');
  85. if (isset($site['name']) && isset($extensions['profile']) && in_array($extensions['profile'], array_keys($names), TRUE)) {
  86. // Ensure the profile can be installed from configuration. Install
  87. // profile's which implement hook_INSTALL() are not supported.
  88. // @todo https://www.drupal.org/project/drupal/issues/2982052 Remove
  89. // this restriction.
  90. module_load_install($extensions['profile']);
  91. if (!function_exists($extensions['profile'] . '_install')) {
  92. $form['profile']['#options'][static::CONFIG_INSTALL_PROFILE_KEY] = $this->t('Use existing configuration');
  93. $form['profile'][static::CONFIG_INSTALL_PROFILE_KEY]['#description'] = [
  94. 'description' => [
  95. '#markup' => $this->t('Install %name using existing configuration.', ['%name' => $site['name']]),
  96. ],
  97. 'info' => [
  98. '#type' => 'item',
  99. '#markup' => $this->t('The configuration from the directory %sync_directory will be used.', ['%sync_directory' => $config_sync_directory]),
  100. '#wrapper_attributes' => [
  101. 'class' => ['messages', 'messages--status'],
  102. ],
  103. '#states' => [
  104. 'visible' => [
  105. ':input[name="profile"]' => ['value' => static::CONFIG_INSTALL_PROFILE_KEY],
  106. ],
  107. ],
  108. ],
  109. ];
  110. }
  111. }
  112. }
  113. $form['actions'] = ['#type' => 'actions'];
  114. $form['actions']['submit'] = [
  115. '#type' => 'submit',
  116. '#value' => $this->t('Save and continue'),
  117. '#button_type' => 'primary',
  118. ];
  119. return $form;
  120. }
  121. /**
  122. * {@inheritdoc}
  123. */
  124. public function submitForm(array &$form, FormStateInterface $form_state) {
  125. global $install_state;
  126. $profile = $form_state->getValue('profile');
  127. if ($profile === static::CONFIG_INSTALL_PROFILE_KEY) {
  128. $sync = new FileStorage(Settings::get('config_sync_directory'));
  129. $profile = $sync->read('core.extension')['profile'];
  130. $install_state['parameters']['existing_config'] = TRUE;
  131. }
  132. $install_state['parameters']['profile'] = $profile;
  133. }
  134. /**
  135. * Show profile warning if 'demo_umami' profile is selected.
  136. */
  137. protected function addUmamiWarning(array &$form) {
  138. // Warning to show when this profile is selected.
  139. $description = $form['profile']['demo_umami']['#description'];
  140. // Re-defines radio #description to show warning when selected.
  141. $form['profile']['demo_umami']['#description'] = [
  142. 'warning' => [
  143. '#type' => 'item',
  144. '#markup' => $this->t('This profile is intended for demonstration purposes only.'),
  145. '#wrapper_attributes' => [
  146. 'class' => ['messages', 'messages--warning'],
  147. ],
  148. '#states' => [
  149. 'visible' => [
  150. ':input[name="profile"]' => ['value' => 'demo_umami'],
  151. ],
  152. ],
  153. ],
  154. 'description' => ['#markup' => $description],
  155. ];
  156. }
  157. }