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

Commit

Permalink
Streamline this into a single step
Browse files Browse the repository at this point in the history
  • Loading branch information
phenaproxima committed Aug 2, 2024
1 parent be8d44b commit d8924c2
Show file tree
Hide file tree
Showing 3 changed files with 90 additions and 106 deletions.
60 changes: 0 additions & 60 deletions installer/src/Form/RecipeAddOnForm.php

This file was deleted.

74 changes: 74 additions & 0 deletions installer/src/Form/RecipesForm.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
<?php

namespace Drupal\starshot_installer\Form;

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

/**
* Provides the recipe add-on selection form.
*/
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),
];
}

}
62 changes: 16 additions & 46 deletions installer/starshot_installer.profile
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ 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;

/**
Expand Down Expand Up @@ -36,30 +37,20 @@ function starshot_installer_install_tasks_alter(array &$tasks, $install_state):
$tasks = $tasks_before + $additions + $tasks_after;
};

// Ensure 'addons' key exists within parameters.
if (!isset($install_state['parameters']['addons'])) {
$install_state['parameters']['addons'] = [];
}

$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_add_on_recipes' => [
'starshot_installer_choose_recipes' => [
// Present hard-coded add-on recipes.
// @todo Update this after https://www.drupal.org/i/3450629 is fixed.
'display_name' => t('Choose add-ons'),
'display_name' => t('Choose template & add-ons'),
'type' => 'form',
'run' => !empty($install_state['parameters']['addons']) ? INSTALL_TASK_SKIP : INSTALL_TASK_RUN_IF_REACHED,
'function' => 'Drupal\starshot_installer\Form\RecipeAddOnForm',
'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 @@ -134,47 +125,26 @@ 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;
}
// Apply addon recipes.
foreach ($install_state['parameters']['addons'] as $addon) {
if (!empty($addon)) {
$recipe = Recipe::createFromDirectory(Drupal::root() . $addon);
Drupal::classResolver(InputCollector::class)->prepare($recipe);
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

0 comments on commit d8924c2

Please sign in to comment.