You are here

public function AddTeamMembersForm::submitForm in Apigee Edge 8

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

modules/apigee_edge_teams/src/Form/AddTeamMembersForm.php, line 220

Class

AddTeamMembersForm
Add team members form.

Namespace

Drupal\apigee_edge_teams\Form

Code

public function submitForm(array &$form, FormStateInterface $form_state) {
  $emails = array_map('trim', explode(',', $form_state
    ->getValue('developers', '')));
  $selected_roles = $this
    ->filterSelectedRoles($form_state
    ->getValue('team_roles', []));

  // Add default member role.
  $selected_roles = [
    TeamRoleInterface::TEAM_MEMBER_ROLE => TeamRoleInterface::TEAM_MEMBER_ROLE,
  ] + $selected_roles;

  // Create an invitation for each email.
  foreach ($emails as $email) {
    $this->teamInvitationStorage
      ->create([
      'team' => [
        'target_id' => $this->team
          ->id(),
      ],
      'team_roles' => array_values(array_map(function (string $role) {
        return [
          'target_id' => $role,
        ];
      }, $selected_roles)),
      'recipient' => $email,
    ])
      ->save();
  }
  $context = [
    '@developers' => implode(', ', $emails),
    '@team' => $this->team
      ->label(),
    '@team_label' => mb_strtolower($this->team
      ->getEntityType()
      ->getSingularLabel()),
  ];
  $this
    ->messenger()
    ->addStatus($this
    ->formatPlural(count($emails), $this
    ->t('The following developer has been invited to the @team @team_label: @developers.', $context), $this
    ->t('The following developers have been invited to the @team @team_label: @developers.', $context)));
  $form_state
    ->setRedirectUrl($this->team
    ->toUrl('members'));
}