You are here

public function TeamForm::save in Apigee Edge 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/apigee_edge_teams/src/Entity/Form/TeamForm.php, line 185

Class

TeamForm
General form handler for the team create/edit forms.

Namespace

Drupal\apigee_edge_teams\Entity\Form

Code

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

  /** @var \Drupal\apigee_edge_teams\Entity\TeamInterface $team */
  $team = $this->entity;
  $was_new = $team
    ->isNew();
  $result = parent::save($form, $form_state);
  if ($was_new) {
    try {
      $this->teamMembershipManager
        ->addMembers($team
        ->id(), [
        $this->currentUser
          ->getEmail(),
      ]);
      try {

        /** @var \Drupal\apigee_edge_teams\Entity\Storage\TeamMemberRoleStorageInterface $team_member_role_storage */
        $team_member_role_storage = $this->entityTypeManager
          ->getStorage('team_member_role');
        $team_member_role_storage
          ->addTeamRoles($this
          ->currentUser(), $team, [
          TeamRoleInterface::TEAM_ADMIN_ROLE,
        ]);
      } catch (\Exception $exception) {
        $admin_role = $this->entityTypeManager
          ->getStorage('team_role')
          ->load(TeamRoleInterface::TEAM_ADMIN_ROLE);
        $context = [
          '%email' => $this->currentUser
            ->getEmail(),
          '%team_name' => $team
            ->label(),
          '%admin_role' => $admin_role
            ->label(),
          '@team' => mb_strtolower($this->entityTypeManager
            ->getDefinition('team')
            ->getSingularLabel()),
          'link' => $team
            ->toLink()
            ->toString(),
        ];
        $this
          ->messenger()
          ->addError($this
          ->t('Failed to grant %admin_role team role in %team_name @team.', $context));
        $context += Error::decodeException($exception);
        $this->logger
          ->error('Failed to add creator of the team (%email) as team administrator to the team. @message %function (line %line of %file). <pre>@backtrace_string</pre>', $context);
      }
    } catch (ApiException $exception) {
      $this
        ->messenger()
        ->addError($this
        ->t('Failed to register team membership in %team_name @team.', [
        '%team_name' => $team
          ->label(),
        '@team' => mb_strtolower($this->entityTypeManager
          ->getDefinition('team')
          ->getSingularLabel()),
      ]));
      $context = [
        '%email' => $this->currentUser
          ->getEmail(),
        'link' => $team
          ->toLink()
          ->toString(),
      ];
      $context += Error::decodeException($exception);
      $this->logger
        ->error('Unable to add creator of the team (%email) as member to the team. @message %function (line %line of %file). <pre>@backtrace_string</pre>', $context);
    }
  }
  $form_state
    ->setRedirectUrl($team
    ->toUrl('collection'));
  return $result;
}