You are here

public function ConfigPermListForm::submitForm in Custom Permissions 8

Same name and namespace in other branches
  1. 8.2 src/Form/ConfigPermListForm.php \Drupal\config_perms\Form\ConfigPermListForm::submitForm()

Form submission handler.

Parameters

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

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

Overrides FormInterface::submitForm

File

src/Form/ConfigPermListForm.php, line 189

Class

ConfigPermListForm
Class ConfigPermListForm.

Namespace

Drupal\config_perms\Form

Code

public function submitForm(array &$form, FormStateInterface $form_state) {
  $values = $form_state
    ->getValues();
  $perms = CustomPermsEntity::loadMultiple();
  foreach ($values['local'] as $key => $data) {

    // If new permission.
    if ($key == 'new') {
      $entity = CustomPermsEntity::create();
      $entity
        ->set('id', $this
        ->configPermsGenerateMachineName($data['name']));
      $entity
        ->set('label', $data['name']);
      $entity
        ->set('path', $data['path']);
      $entity
        ->set('status', $data['status']);
      $entity
        ->save();
    }
    else {

      // Update || Insert.
      if (!empty($data['name']) && !empty($data['path'])) {
        $entity = $perms[$data['id']];
        $entity
          ->set('label', $data['name']);
        $entity
          ->set('path', $data['path']);
        $entity
          ->set('status', $data['status']);
        $entity
          ->save();
      }
    }
  }
  \Drupal::service('router.builder')
    ->rebuild();
  drupal_set_message($this
    ->t('The permissions have been saved.'));
}