Same name and namespace in other branches
  1. 8.9.x core/modules/system/src/Form/ModulesListForm.php \Drupal\system\Form\ModulesListForm::buildForm()
  2. 9 core/modules/system/src/Form/ModulesListForm.php \Drupal\system\Form\ModulesListForm::buildForm()

Form constructor.

Parameters

array $form: An associative array containing the structure of the form.

\Drupal\Core\Form\FormStateInterface $form_state: The current state of the form.

Return value

array The form structure.

Overrides FormInterface::buildForm

File

core/modules/system/src/Form/ModulesListForm.php, line 143

Class

ModulesListForm
Provides module installation interface.

Namespace

Drupal\system\Form

Code

public function buildForm(array $form, FormStateInterface $form_state) {
  require_once DRUPAL_ROOT . '/core/includes/install.inc';
  $distribution = drupal_install_profile_distribution_name();

  // Include system.admin.inc so we can use the sort callbacks.
  $this->moduleHandler
    ->loadInclude('system', 'inc', 'system.admin');
  $form['filters'] = [
    '#type' => 'container',
    '#attributes' => [
      'class' => [
        'table-filter',
        'js-show',
      ],
    ],
  ];
  $form['filters']['text'] = [
    '#type' => 'search',
    '#title' => $this
      ->t('Filter modules'),
    '#title_display' => 'invisible',
    '#size' => 30,
    '#placeholder' => $this
      ->t('Filter by name or description'),
    '#description' => $this
      ->t('Enter a part of the module name or description'),
    '#attributes' => [
      'class' => [
        'table-filter-text',
      ],
      'data-table' => '#system-modules',
      'autocomplete' => 'off',
    ],
  ];

  // Sort all modules by their names.
  try {

    // The module list needs to be reset so that it can re-scan and include
    // any new modules that may have been added directly into the filesystem.
    $modules = $this->moduleExtensionList
      ->reset()
      ->getList();

    // Remove obsolete modules.
    $modules = array_filter($modules, function ($module) {
      return !$module
        ->isObsolete();
    });
    uasort($modules, [
      ModuleExtensionList::class,
      'sortByName',
    ]);
  } catch (InfoParserException $e) {
    $this
      ->messenger()
      ->addError($this
      ->t('Modules could not be listed due to an error: %error', [
      '%error' => $e
        ->getMessage(),
    ]));
    $modules = [];
  }

  // Iterate over each of the modules.
  $form['modules']['#tree'] = TRUE;
  $incompatible_installed = FALSE;
  foreach ($modules as $filename => $module) {
    if (empty($module->info['hidden'])) {
      $package = $module->info['package'];
      $form['modules'][$package][$filename] = $this
        ->buildRow($modules, $module, $distribution);
      $form['modules'][$package][$filename]['#parents'] = [
        'modules',
        $filename,
      ];
    }
    if (!$incompatible_installed && $module->status && $module->info['core_incompatible']) {
      $incompatible_installed = TRUE;
      $this
        ->messenger()
        ->addWarning($this
        ->t('There are errors with some installed modules. Visit the <a href=":link">status report page</a> for more information.', [
        ':link' => Url::fromRoute('system.status')
          ->toString(),
      ]));
    }
  }

  // Add a wrapper around every package.
  foreach (Element::children($form['modules']) as $package) {
    $form['modules'][$package] += [
      '#type' => 'details',
      '#title' => $this
        ->t($package),
      '#open' => TRUE,
      '#theme' => 'system_modules_details',
      '#attributes' => [
        'class' => [
          'package-listing',
        ],
      ],
      // Ensure that the "Core" package comes first.
      '#weight' => $package == 'Core' ? -10 : NULL,
    ];
  }

  // If testing modules are shown, collapse the corresponding package by
  // default.
  if (isset($form['modules']['Testing'])) {
    $form['modules']['Testing']['#open'] = FALSE;
  }

  // Lastly, sort all packages by title.
  uasort($form['modules'], [
    '\\Drupal\\Component\\Utility\\SortArray',
    'sortByTitleProperty',
  ]);
  $form['#attached']['library'][] = 'core/drupal.tableresponsive';
  $form['#attached']['library'][] = 'system/drupal.system.modules';
  $form['actions'] = [
    '#type' => 'actions',
  ];
  $form['actions']['submit'] = [
    '#type' => 'submit',
    '#value' => $this
      ->t('Install'),
    '#button_type' => 'primary',
  ];
  return $form;
}