You are here

public function ModulesListForm::buildForm in Modules weight 8

Same name and namespace in other branches
  1. 8.2 src/Form/ModulesListForm.php \Drupal\modules_weight\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

src/Form/ModulesListForm.php, line 76

Class

ModulesListForm
Builds the form to configure the Modules weight.

Namespace

Drupal\modules_weight\Form

Code

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

  // The table header.
  $header = [
    $this
      ->t('Name'),
    [
      'data' => $this
        ->t('Description'),
      // Hidding the description on narrow width devices.
      'class' => [
        RESPONSIVE_PRIORITY_MEDIUM,
      ],
    ],
    $this
      ->t('Weight'),
    [
      'data' => $this
        ->t('Package'),
      // Hidding the description on narrow width devices.
      'class' => [
        RESPONSIVE_PRIORITY_MEDIUM,
      ],
    ],
  ];

  // The table.
  $form['modules'] = [
    '#type' => 'table',
    '#header' => $header,
    '#sticky' => TRUE,
  ];

  // Getting the config to know if we should show or not the core modules.
  $show_core_modules = $this->configFactory
    ->get('modules_weight.settings')
    ->get('show_system_modules');

  // Getting the module list.
  $modules = $this->modulesWeight
    ->getModulesList($show_core_modules);

  // Iterate over each module.
  foreach ($modules as $filename => $module) {

    // The rows info.
    // Module name.
    $form['modules'][$filename]['name'] = [
      '#markup' => $module['name'],
    ];

    // Module description.
    $form['modules'][$filename]['description'] = [
      '#markup' => $module['description'],
    ];

    // Module weight.
    $form['modules'][$filename]['weight'] = [
      '#type' => 'weight',
      '#default_value' => $module['weight'],
      '#delta' => FormElement::getMaxDelta($module['weight']),
    ];

    // Module package.
    $form['modules'][$filename]['package'] = [
      '#markup' => $module['package'],
    ];
  }

  // The form button.
  $form['submit'] = [
    '#type' => 'submit',
    '#value' => $this
      ->t('Save Changes'),
  ];
  return $form;
}