function ModulesUninstallForm::buildForm

Same name and namespace in other branches
  1. 9 core/modules/system/src/Form/ModulesUninstallForm.php \Drupal\system\Form\ModulesUninstallForm::buildForm()
  2. 8.9.x core/modules/system/src/Form/ModulesUninstallForm.php \Drupal\system\Form\ModulesUninstallForm::buildForm()
  3. 11.x core/modules/system/src/Form/ModulesUninstallForm.php \Drupal\system\Form\ModulesUninstallForm::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/ModulesUninstallForm.php, line 104

Class

ModulesUninstallForm
Provides a form for uninstalling modules.

Namespace

Drupal\system\Form

Code

public function buildForm(array $form, FormStateInterface $form_state) {
  // Make sure the install API is available.
  include_once DRUPAL_ROOT . '/core/includes/install.inc';
  // Get a list of all available modules that can be uninstalled.
  $uninstallable = array_filter($this->moduleExtensionList
    ->getList(), function ($module) {
    return empty($module->info['required']) && $module->status;
  });
  // 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-uninstall',
      'autocomplete' => 'off',
    ],
  ];
  $form['modules'] = [];
  // Only build the rest of the form if there are any modules available to
  // uninstall;
  if (empty($uninstallable)) {
    return $form;
  }
  // Deprecated and obsolete modules should appear at the top of the
  // uninstallation list.
  $unstable_lifecycle = array_flip([
    ExtensionLifecycle::DEPRECATED,
    ExtensionLifecycle::OBSOLETE,
  ]);
  // Sort all modules by their lifecycle identifier and name.
  uasort($uninstallable, function ($a, $b) use ($unstable_lifecycle) {
    $lifecycle_a = isset($unstable_lifecycle[$a->info[ExtensionLifecycle::LIFECYCLE_IDENTIFIER]]) ? -1 : 1;
    $lifecycle_b = isset($unstable_lifecycle[$b->info[ExtensionLifecycle::LIFECYCLE_IDENTIFIER]]) ? -1 : 1;
    if ($lifecycle_a === $lifecycle_b) {
      return ModuleExtensionList::sortByName($a, $b);
    }
    return $lifecycle_a <=> $lifecycle_b;
  });
  $validation_reasons = $this->moduleInstaller
    ->validateUninstall(array_keys($uninstallable));
  $form['uninstall'] = [
    '#tree' => TRUE,
  ];
  foreach ($uninstallable as $module_key => $module) {
    $name = $module->info['name'] ?: $module->getName();
    $form['modules'][$module->getName()]['#module_name'] = $name;
    $form['modules'][$module->getName()]['name']['#markup'] = $name;
    $form['modules'][$module->getName()]['description']['#markup'] = $this->t($module->info['description']);
    $lifecycle = $module->info[ExtensionLifecycle::LIFECYCLE_IDENTIFIER];
    if ($lifecycle !== ExtensionLifecycle::STABLE && !empty($module->info[ExtensionLifecycle::LIFECYCLE_LINK_IDENTIFIER])) {
      $form['modules'][$module->getName()]['name']['#markup'] .= ' ' . Link::fromTextAndUrl('(' . $this->t('@lifecycle', [
        '@lifecycle' => ucfirst($lifecycle),
      ]) . ')', Url::fromUri($module->info[ExtensionLifecycle::LIFECYCLE_LINK_IDENTIFIER], [
        'attributes' => [
          'class' => 'module-link--non-stable',
          'aria-label' => $this->t('View information on the @lifecycle status of the module @module', [
            '@lifecycle' => ucfirst($lifecycle),
            '@module' => $module->info['name'],
          ]),
        ],
      ]))
        ->toString();
    }
    $form['uninstall'][$module->getName()] = [
      '#type' => 'checkbox',
      '#title' => $this->t('Uninstall @module module', [
        '@module' => $name,
      ]),
      '#title_display' => 'invisible',
    ];
    // If a validator returns reasons not to uninstall a module,
    // list the reasons and disable the check box.
    if (isset($validation_reasons[$module_key])) {
      $form['modules'][$module->getName()]['#validation_reasons'] = $validation_reasons[$module_key];
      $form['uninstall'][$module->getName()]['#disabled'] = TRUE;
    }
    // All modules which depend on this one must be uninstalled first, before
    // we can allow this module to be uninstalled.
    foreach (array_keys($module->required_by) as $dependent) {
      if ($this->updateRegistry
        ->getInstalledVersion($dependent) !== $this->updateRegistry::SCHEMA_UNINSTALLED) {
        $form['modules'][$module->getName()]['#required_by'][] = $dependent;
        $form['uninstall'][$module->getName()]['#disabled'] = TRUE;
      }
    }
  }
  $form['#attached']['library'][] = 'system/drupal.system.modules';
  $form['actions'] = [
    '#type' => 'actions',
  ];
  $form['actions']['submit'] = [
    '#type' => 'submit',
    '#value' => $this->t('Uninstall'),
  ];
  return $form;
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.