SelectProfileForm.php 4.2 KB

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