You are here

public function ConnectionRoleForm::save in RedHen CRM 8

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

modules/redhen_connection/src/Form/ConnectionRoleForm.php, line 89

Class

ConnectionRoleForm
Class ConnectionRoleForm.

Namespace

Drupal\redhen_connection\Form

Code

public function save(array $form, FormStateInterface $form_state) {
  $redhen_connection_role = $this->entity;

  // Get connection type entity from the route.
  $connection_type = $this
    ->getEntityFromRouteMatch($this
    ->getRouteMatch(), 'redhen_connection_type');

  // Set connection type property based on the route param.
  $redhen_connection_role
    ->set('connection_type', $connection_type
    ->id());

  // Load up plugin definitions.
  $connection_plugin_manager = \Drupal::service('plugin.manager.connection_permission');
  $plugin_definitions = $connection_plugin_manager
    ->getDefinitions();
  $permissions = [];

  // Build array of permissions.
  foreach ($plugin_definitions as $plugin_id => $plugin_definition) {
    $plugin_instance = $connection_plugin_manager
      ->createInstance($plugin_id);
    $permission_key = $plugin_instance
      ->getPermissionKey();
    $permissions[$permission_key] = is_array($form_state
      ->getValue($permission_key)) ? array_filter(array_values($form_state
      ->getValue($permission_key))) : NULL;
  }
  $redhen_connection_role
    ->set('permissions', $permissions);
  $status = $redhen_connection_role
    ->save();
  $messenger = \Drupal::messenger();
  switch ($status) {
    case SAVED_NEW:
      $messenger
        ->addMessage($this
        ->t('Created the %label Connection Role.', [
        '%label' => $redhen_connection_role
          ->label(),
      ]));
      break;
    default:
      $messenger
        ->addMessage($this
        ->t('Saved the %label Connection Role.', [
        '%label' => $redhen_connection_role
          ->label(),
      ]));
  }
  $form_state
    ->setRedirectUrl($redhen_connection_role
    ->toUrl('collection'));
}