You are here

public function ServerForm::save in Lightweight Directory Access Protocol (LDAP) 8.3

Same name and namespace in other branches
  1. 8.4 ldap_servers/src/Form/ServerForm.php \Drupal\ldap_servers\Form\ServerForm::save()

Form submission handler for the 'save' action.

Normally this method should be overridden to provide specific messages to the user and redirect the form after the entity has been saved.

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

int Either SAVED_NEW or SAVED_UPDATED, depending on the operation performed.

Overrides EntityForm::save

File

ldap_servers/src/Form/ServerForm.php, line 467

Class

ServerForm
Class ServerForm.

Namespace

Drupal\ldap_servers\Form

Code

public function save(array $form, FormStateInterface $form_state) {
  if ($form_state
    ->getValue('bind_method') != 'service_account') {
    $this->entity
      ->set('binddn', NULL);
    $this->entity
      ->set('bindpw', NULL);
  }
  else {
    if ($form_state
      ->getValue('bindpw') != '****') {
      $this->entity
        ->set('bindpw', $form_state
        ->getValue('bindpw'));
    }
    else {

      // Fetch existing password since the placeholder is present.
      $oldConfiguration = Server::load($this->entity
        ->id());
      if ($oldConfiguration && $oldConfiguration
        ->get('bindpw')) {
        $this->entity
          ->set('bindpw', $oldConfiguration
          ->get('bindpw'));
      }
    }
  }
  $fields = [
    'user_attr',
    'account_name_attr',
    'mail_attr',
    'mail_template',
    'picture_attr',
    'unique_persistent_attr',
    'user_dn_expression',
    'grp_memb_attr',
    'grp_object_cat',
    'grp_memb_attr_match_user_attr',
    'grp_user_memb_attr',
    'grp_derive_from_dn_attr',
  ];
  foreach ($fields as $field) {
    $this->entity
      ->set($field, mb_strtolower(trim($this->entity
      ->get($field))));
  }
  $status = $this->entity
    ->save();
  switch ($status) {
    case SAVED_NEW:
      drupal_set_message($this
        ->t('Created the %label Server.', [
        '%label' => $this->entity
          ->label(),
      ]));
      break;
    default:
      drupal_set_message($this
        ->t('Saved the %label Server.', [
        '%label' => $this->entity
          ->label(),
      ]));
  }
  $form_state
    ->setRedirectUrl($this->entity
    ->toUrl('collection'));
}