You are here

public function DevelReinstall::buildForm in Devel 8

Same name and namespace in other branches
  1. 8.3 src/Form/DevelReinstall.php \Drupal\devel\Form\DevelReinstall::buildForm()
  2. 8.2 src/Form/DevelReinstall.php \Drupal\devel\Form\DevelReinstall::buildForm()
  3. 4.x src/Form/DevelReinstall.php \Drupal\devel\Form\DevelReinstall::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

src/Form/DevelReinstall.php, line 51

Class

DevelReinstall
Display a dropdown of installed modules with the option to reinstall them.

Namespace

Drupal\devel\Form

Code

public function buildForm(array $form, FormStateInterface $form_state) {

  // Get a list of all available modules.
  $modules = system_rebuild_module_data();
  $uninstallable = array_filter($modules, function ($module) use ($modules) {
    return empty($modules[$module
      ->getName()]->info['required']) && drupal_get_installed_schema_version($module
      ->getName()) > SCHEMA_UNINSTALLED && $module
      ->getName() !== 'devel';
  });
  $form['filters'] = array(
    '#type' => 'container',
    '#attributes' => array(
      'class' => array(
        'table-filter',
        'js-show',
      ),
    ),
  );
  $form['filters']['text'] = array(
    '#type' => 'search',
    '#title' => $this
      ->t('Search'),
    '#size' => 30,
    '#placeholder' => $this
      ->t('Enter module name'),
    '#attributes' => array(
      'class' => array(
        'table-filter-text',
      ),
      'data-table' => '#devel-reinstall-form',
      'autocomplete' => 'off',
      'title' => $this
        ->t('Enter a part of the module name or description to filter by.'),
    ),
  );

  // Only build the rest of the form if there are any modules available to
  // uninstall;
  if (empty($uninstallable)) {
    return $form;
  }
  $header = array(
    'name' => $this
      ->t('Name'),
    'description' => $this
      ->t('Description'),
  );
  $rows = array();
  foreach ($uninstallable as $module) {
    $name = $module->info['name'] ?: $module
      ->getName();
    $rows[$module
      ->getName()] = array(
      'name' => array(
        'data' => array(
          '#type' => 'inline_template',
          '#template' => '<label class="module-name table-filter-text-source">{{ module_name }}</label>',
          '#context' => array(
            'module_name' => $name,
          ),
        ),
      ),
      'description' => array(
        'data' => $module->info['description'],
        'class' => array(
          'description',
        ),
      ),
    );
  }
  $form['reinstall'] = array(
    '#type' => 'tableselect',
    '#header' => $header,
    '#options' => $rows,
    '#js_select' => FALSE,
    '#empty' => $this
      ->t('No modules are available to uninstall.'),
  );
  $form['#attached']['library'][] = 'system/drupal.system.modules';
  $form['actions'] = [
    '#type' => 'actions',
  ];
  $form['actions']['submit'] = [
    '#type' => 'submit',
    '#value' => $this
      ->t('Reinstall'),
  ];
  return $form;
}