Skip to content
This repository has been archived by the owner on Aug 8, 2024. It is now read-only.

Provide an form during installation for users to choose their site template and any add-ons #161

Merged
merged 4 commits into from
Aug 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
77 changes: 77 additions & 0 deletions installer/src/Form/RecipesForm.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
<?php

namespace Drupal\starshot_installer\Form;

use Drupal\Core\Form\FormBase;
use Drupal\Core\Form\FormStateInterface;

/**
* Provides a form to choose the site template and optional add-on recipes.
*
* @todo Present this as a mini project browser once
* https://www.drupal.org/i/3450629 is fixed.
*/
final class RecipesForm extends FormBase {

/**
* {@inheritdoc}
*/
public function getFormId(): string {
return 'starshot_installer_recipes_form';
}

/**
* {@inheritdoc}
*/
public function buildForm(array $form, FormStateInterface $form_state): array {
$form['#title'] = $this->t('Choose template & add-ons');

$form['template'] = [
'#type' => 'radios',
'#title' => $this->t('Choose your site template'),
'#options' => [
'starshot' => $this->t('Starshot'),
],
'#required' => TRUE,
'#default_value' => 'starshot',
];

$options = [
'starshot_multilingual' => $this->t('Multilingual support'),
'starshot_accessibility_tools' => $this->t('Accessibility tools'),
];

$form['add_ons'] = [
'#type' => 'checkboxes',
'#title' => $this->t('Optional add-ons'),
'#options' => $options,
'#default_value' => [],
];

$form['actions'] = [
'submit' => [
'#type' => 'submit',
'#value' => $this->t('Save and continue'),
'#button_type' => 'primary',
],
'#type' => 'actions',
];
return $form;
}

/**
* {@inheritdoc}
*/
public function submitForm(array &$form, FormStateInterface $form_state): void {
global $install_state;

$add_ons = $form_state->getValue('add_ons', []);
$add_ons = array_filter($add_ons);

$install_state['parameters']['recipes'] = [
$form_state->getValue('template'),
...array_values($add_ons),
];
}

}
53 changes: 19 additions & 34 deletions installer/starshot_installer.profile
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,14 @@ use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Recipe\InputCollector;
use Drupal\Core\Recipe\Recipe;
use Drupal\Core\Recipe\RecipeRunner;
use Drupal\starshot_installer\Form\RecipesForm;
use Symfony\Component\Process\ExecutableFinder;

/**
* Implements hook_install_tasks().
*/
function starshot_installer_install_tasks(): array {
return [
// 'starshot_installer_choose_add_on_recipes' => [
// We don't currently have the ability to present add-on recipes, so for
// now this task doesn't do anything and is hidden from users.
// @todo Fill this in after https://www.drupal.org/i/3450629 is fixed.
// 'display_name' => t('Choose add-ons'),
// ],
'starshot_installer_uninstall_myself' => [
// As a final task, this profile should uninstall itself.
],
Expand All @@ -29,7 +24,7 @@ function starshot_installer_install_tasks(): array {
/**
* Implements hook_install_tasks_alter().
*/
function starshot_installer_install_tasks_alter(array &$tasks): void {
function starshot_installer_install_tasks_alter(array &$tasks, array $install_state): void {
$insert_before = function (string $key, array $additions) use (&$tasks): void {
$key = array_search($key, array_keys($tasks), TRUE);
if ($key === FALSE) {
Expand All @@ -42,16 +37,17 @@ function starshot_installer_install_tasks_alter(array &$tasks): void {
$tasks = $tasks_before + $additions + $tasks_after;
};
$insert_before('install_settings_form', [
'starshot_installer_choose_template' => [
// Because the choice of template is currently hard-coded, this should
// not be presented to the user.
// 'display_name' => t('Choose template'),
'starshot_installer_choose_recipes' => [
'display_name' => t('Choose template & add-ons'),
'type' => 'form',
'run' => array_key_exists('recipes', $install_state['parameters']) ? INSTALL_TASK_SKIP : INSTALL_TASK_RUN_IF_REACHED,
'function' => RecipesForm::class,
],
]);

// Wrap the install_profile_modules() function, which returns a batch job, and
// add all the necessary operations to apply the chosen template recipe.
$tasks['install_profile_modules']['function'] = 'starshot_installer_apply_template';
$tasks['install_profile_modules']['function'] = 'starshot_installer_apply_recipes';
}

/**
Expand Down Expand Up @@ -126,38 +122,27 @@ function _starshot_installer_install_configure_form_submit(array &$form, FormSta
}

/**
* Presents the user which a choice of which template should set up the site.
*
* @param array $install_state
* An array of information about the current installation state.
*
* @see starshot_installer_apply_template()
*/
function starshot_installer_choose_template(array &$install_state): void {
// For now, hard-code the choice to the main Starshot recipe. When more
// choices are available, this should present a form whose submit handler
// should set the `template` install parameter for
// starshot_installer_apply_template() to act upon.
$install_state['parameters']['template'] = Drupal::root() . '/recipes/starshot';
}

/**
* Runs a batch job that applies the template recipe.
* Runs a batch job that applies the template and add-on recipes.
*
* @param array $install_state
* An array of information about the current installation state.
*
* @return array
* The batch job definition.
*/
function starshot_installer_apply_template(array &$install_state): array {
function starshot_installer_apply_recipes(array &$install_state): array {
$batch = install_profile_modules($install_state);

$recipe = Recipe::createFromDirectory($install_state['parameters']['template']);
Drupal::classResolver(InputCollector::class)->prepare($recipe);
$input_collector = Drupal::classResolver(InputCollector::class);
$cookbook_path = Drupal::root() . '/recipes';

foreach (RecipeRunner::toBatchOperations($recipe) as $operation) {
$batch['operations'][] = $operation;
foreach ($install_state['parameters']['recipes'] as $recipe) {
$recipe = Recipe::createFromDirectory($cookbook_path . '/' . $recipe);
$input_collector->prepare($recipe);

foreach (RecipeRunner::toBatchOperations($recipe) as $operation) {
$batch['operations'][] = $operation;
}
}
return $batch;
}
Expand Down
1 change: 1 addition & 0 deletions recipes/starshot_multilingual/recipe.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ install:
- content_translation
- language
- node
- user
config:
actions:
core.entity_form_display.node.*.default:
Expand Down