Add an install task for selecting+installing farmOS modules.

This commit is contained in:
Michael Stenta 2020-08-15 11:39:00 -04:00
parent 7e3c042ddb
commit e1dfe45cdb
3 changed files with 168 additions and 0 deletions

66
farm.install Normal file
View File

@ -0,0 +1,66 @@
<?php
/**
* @file
* Install, update and uninstall functions for the farmOS installation profile.
*/
/**
* Implements hook_install_tasks().
*/
function farm_install_tasks(&$install_state) {
// Add tasks for enabling farmOS modules.
$tasks = [
'\Drupal\farm\Form\FarmModulesForm' => [
'display_name' => t('Install modules'),
'type' => 'form',
],
'farm_install_modules' => [
'type' => 'batch',
],
];
return $tasks;
}
/**
* Installs farmOS modules via a batch process.
*
* @param $install_state
* An array of information about the current installation state.
*
* @return array
* The batch definition.
*/
function farm_install_modules(&$install_state) {
// Load the list of modules that should be installed. This state is set by
// the \Drupal\farm\Form\FarmModulesForm submit method.
$modules = \Drupal::state()->get('farm.install_modules') ?: [];
// Load a list of all available modules, so that we can display their names.
$files = \Drupal::service('extension.list.module')->getList();
// Assemble the batch operation for installing modules.
$operations = [];
foreach ($modules as $module => $weight) {
$operations[] = ['_farm_install_module_batch', [$module, $files[$module]->info['name']]];
}
$batch = [
'operations' => $operations,
'title' => t('Installing @drupal modules', ['@drupal' => drupal_install_profile_distribution_name()]),
'error_message' => t('The installation has encountered an error.'),
];
return $batch;
}
/**
* Implements callback_batch_operation().
*
* Performs batch installation of farmOS modules.
*/
function _farm_install_module_batch($module, $module_name, &$context) {
\Drupal::service('module_installer')->install([$module], TRUE);
$context['results'][] = $module;
$context['message'] = t('Installed %module module.', ['%module' => $module_name]);
}

34
farm.profile Normal file
View File

@ -0,0 +1,34 @@
<?php
/**
* @file
* farmOS installation profile.
*/
/**
* Define farmOS modules that can be installed.
*
* @return array
* Returns an array with two sub-arrays: 'default' and 'optional'. Default
* modules will be selected for installation by default, and optional modules
* will require the user to select them for installation.
*/
function farm_modules() {
return [
'default' => [
'farm_activity' => t('Activity logs'),
'farm_observation' => t('Observation logs'),
'farm_input' => t('Input logs'),
'farm_harvest' => t('Harvest logs'),
],
'optional' => [
'farm_seeding' => t('Seeding logs'),
'farm_transplanting' => t('Transplanting logs'),
'farm_lab_test' => t('Lab test logs'),
'farm_maintenance' => t('Maintenance logs'),
'farm_medical' => t('Medical logs'),
'farm_purchase' => t('Purchase logs'),
'farm_sale' => t('Sale logs'),
],
];
}

View File

@ -0,0 +1,68 @@
<?php
namespace Drupal\farm\Form;
use Drupal\Core\Form\FormBase;
use Drupal\Core\Form\FormStateInterface;
/**
* farmOS modules form.
*
* @ingroup farm
*/
class FarmModulesForm extends FormBase {
/**
* {@inheritdoc}
*/
public function getFormId() {
return 'farm_modules_form';
}
/**
* {@inheritdoc}
*/
public function buildForm(array $form, FormStateInterface $form_state) {
// Set the form title.
$form['#title'] = $this->t('Enable modules');
// Load the list of available modules.
$modules = farm_modules();
// Allow user to choose which high-level farm modules to install.
$module_options = array_merge($modules['default'], $modules['optional']);
// Default modules will be selected by default.
$module_defaults = array_keys($modules['default']);
// Module checkboxes.
$form['modules'] = [
'#title' => t('farmOS Modules'),
'#title_display' => 'invisible',
'#type' => 'checkboxes',
'#description' => t('Select the farmOS modules that you would like installed by default.'),
'#options' => $module_options,
'#default_value' => $module_defaults,
];
// Submit button.
$form['actions'] = ['#type' => 'actions'];
$form['actions']['submit'] = [
'#type' => 'submit',
'#value' => $this->t('Save and continue'),
'#button_type' => 'primary',
];
return $form;
}
/**
* {@inheritdoc}
*/
public function submitForm(array &$form, FormStateInterface $form_state) {
$modules = array_filter($form_state->getValue('modules'));
\Drupal::state()->set('farm.install_modules', $modules);
}
}