View source
<?php
namespace Drupal\apigee_edge_teams\Form;
use Drupal\apigee_edge\Entity\DeveloperInterface;
use Drupal\apigee_edge_teams\Entity\TeamInterface;
use Drupal\apigee_edge_teams\TeamMembershipManagerInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Form\ConfirmFormBase;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Utility\Error;
use Symfony\Component\DependencyInjection\ContainerInterface;
class RemoveTeamMemberForm extends ConfirmFormBase {
protected $team;
protected $developer;
protected $teamEntityType;
protected $teamMembershipManager;
protected $userStorage;
public function __construct(EntityTypeManagerInterface $entity_type_manager, TeamMembershipManagerInterface $team_membership_manager) {
$this->teamEntityType = $entity_type_manager
->getDefinition('team');
$this->userStorage = $entity_type_manager
->getStorage('user');
$this->teamMembershipManager = $team_membership_manager;
}
public static function create(ContainerInterface $container) {
return new static($container
->get('entity_type.manager'), $container
->get('apigee_edge_teams.team_membership_manager'));
}
public function getFormId() {
return 'apigee_edge_teams_remove_team_member_form';
}
public function buildForm(array $form, FormStateInterface $form_state, TeamInterface $team = NULL, DeveloperInterface $developer = NULL) {
$this->team = $team;
$this->developer = $developer;
return parent::buildForm($form, $form_state);
}
public function getQuestion() {
return $this
->t('Are you sure that you would like to remove %developer from the @team?', [
'%developer' => $this
->getDeveloperLabel(),
'@team' => mb_strtolower($this->teamEntityType
->getSingularLabel()),
]);
}
public function getCancelUrl() {
return $this->team
->toUrl('members');
}
public function validateForm(array &$form, FormStateInterface $form_state) {
if (!in_array($this->developer
->getEmail(), $this->teamMembershipManager
->getMembers($this->team
->id()))) {
$form_state
->setError($form, $this
->t('%developer developer is not member of the %team_name @team.', [
'%developer' => $this->developer
->label(),
'%team_name' => $this->team
->getDisplayName(),
'@team' => mb_strtolower($this->teamEntityType
->getSingularLabel()),
]));
$form_state
->setRedirectUrl($this
->getCancelUrl());
}
parent::validateForm($form, $form_state);
}
public function submitForm(array &$form, FormStateInterface $form_state) {
$context = [
'%developer' => $this
->getDeveloperLabel(),
'%developer_mail' => $this->developer
->getEmail(),
'@team' => mb_strtolower($this->teamEntityType
->getSingularLabel()),
'%team_id' => $this->team
->id(),
];
$success = FALSE;
try {
$this->teamMembershipManager
->removeMembers($this->team
->id(), [
$this->developer
->getEmail(),
]);
$success = TRUE;
} catch (\Exception $exception) {
$context += Error::decodeException($exception);
$this
->messenger()
->addError($this
->t('Failed to remove %developer developer from the @team. Please try again.', $context));
$this
->logger('apigee_edge_teams')
->error('Failed to remove %developer_mail developer from %team_id @team. @message %function (line %line of %file). <pre>@backtrace_string</pre>', $context);
}
if ($success) {
$this
->messenger()
->addStatus($this
->t('%developer successfully removed from the @team.', $context));
}
}
protected function getDeveloperLabel() {
$developer_label = $this->developer
->getEmail();
$users = $this->userStorage
->loadByProperties([
'mail' => $this->developer
->getEmail(),
]);
if (!empty($users)) {
$user = reset($users);
$developer_label = $user
->label();
}
return $developer_label;
}
}