You are here

function farm_install_modules in farmOS 2.x

Installs farmOS modules via a batch process.

Parameters

array $install_state: An array of information about the current installation state.

Return value

array The batch definition.

File

./farm.install, line 66
Install, update and uninstall functions for the farmOS installation profile.

Code

function farm_install_modules(array &$install_state) {

  // Load the list of farmOS core modules.
  $all = farm_modules();

  // Load the list of modules that should be installed.
  // If provided, use the modules defined in farm.modules profile argument.
  // We assume this is an array of module machine names, unless it is simply a
  // string, which is interpreted as a shortcut for installing a set of modules.
  // Available shortcuts are:
  // - "all" (installs all modules)
  // - "default" (installs default and base modules)
  // - "base" (installs base modules only)
  if (!empty($install_state['forms']['farm']['modules'])) {
    $modules_arg = $install_state['forms']['farm']['modules'];
    $all = farm_modules();
    if ($modules_arg === 'all') {
      $modules = array_merge(array_keys($all['base']), array_keys($all['default']), array_keys($all['optional']));
    }
    elseif ($modules_arg === 'default') {
      $modules = array_merge(array_keys($all['base']), array_keys($all['default']));
    }
    elseif ($modules_arg === 'base') {
      $modules = array_keys($all['base']);
    }
    else {
      $modules = Json::decode($modules_arg);
      if (!is_array($modules)) {
        $modules = [];
      }
    }
  }
  else {
    $modules = \Drupal::state()
      ->get('farm.install_modules') ?: [];
    $modules = array_merge(array_keys($all['base']), $modules);
  }

  // If this is running in the context of a functional test, do not install any
  // additional modules. This is a temporary hack.
  // @see Drupal\Tests\farm_test\Functional\FarmBrowserTestBase::setUp()
  // @todo https://www.drupal.org/project/farm/issues/3183739
  if (!empty($GLOBALS['farm_test'])) {
    $modules = [];
  }

  // Load a list of all available modules, so that we can display their names.
  $module_handler = \Drupal::service('module_handler');

  // Assemble the batch operation for installing modules.
  $operations = [];
  foreach ($modules as $module) {
    $operations[] = [
      '_farm_install_module_batch',
      [
        $module,
        $module_handler
          ->getName($module),
      ],
    ];
  }
  $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;
}