View source
<?php
namespace Drupal\apigee_edge_teams\Entity\Form;
use Apigee\Edge\Exception\ApiException;
use Drupal\apigee_edge\Entity\Form\EdgeEntityFormInterface;
use Drupal\apigee_edge\Entity\Form\FieldableEdgeEntityForm;
use Drupal\apigee_edge_teams\Entity\TeamRoleInterface;
use Drupal\apigee_edge_teams\TeamMembershipManagerInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Logger\LoggerChannelInterface;
use Drupal\Core\Session\AccountProxyInterface;
use Drupal\Core\Utility\Error;
use Symfony\Component\DependencyInjection\ContainerInterface;
class TeamForm extends FieldableEdgeEntityForm implements EdgeEntityFormInterface {
const ADMIN_EMAIL_ATTRIBUTE = 'ADMIN_EMAIL';
protected $teamMembershipManager;
protected $currentUser;
protected $logger;
public function __construct(EntityTypeManagerInterface $entity_type_manager, TeamMembershipManagerInterface $team_membership_manager, AccountProxyInterface $current_user, LoggerChannelInterface $logger) {
$this->entityTypeManager = $entity_type_manager;
$this->teamMembershipManager = $team_membership_manager;
$this->currentUser = $current_user;
$this->logger = $logger;
}
public static function create(ContainerInterface $container) {
return new static($container
->get('entity_type.manager'), $container
->get('apigee_edge_teams.team_membership_manager'), $container
->get('current_user'), $container
->get('logger.channel.apigee_edge_teams'));
}
public function buildEntity(array $form, FormStateInterface $form_state) {
$team = parent::buildEntity($form, $form_state);
$team
->setAttribute(static::ADMIN_EMAIL_ATTRIBUTE, $this->currentUser
->getEmail());
return $team;
}
public function form(array $form, FormStateInterface $form_state) {
$form = parent::form($form, $form_state);
$team = $this->entity;
$form['name'] = [
'#title' => $this
->t('Internal name'),
'#type' => 'machine_name',
'#machine_name' => [
'source' => [
'displayName',
'widget',
0,
'value',
],
'label' => $this
->t('Internal name'),
'exists' => [
$this,
'exists',
],
],
'#disabled' => !$team
->isNew(),
'#default_value' => $team
->id(),
];
return $form;
}
public function exists(string $name, array $element, FormStateInterface $form_state) : bool {
if ($name === '') {
return FALSE;
}
$query = $this->entityTypeManager
->getStorage('team')
->getQuery()
->condition('name', $name);
return (bool) $query
->count()
->execute();
}
protected function actions(array $form, FormStateInterface $form_state) {
$team = $this->entity;
$label = mb_strtolower($this->entityTypeManager
->getDefinition('team')
->getSingularLabel());
$actions = parent::actions($form, $form_state);
if ($team
->isNew()) {
$actions['submit']['#value'] = $this
->t('Add @team', [
'@team' => $label,
]);
}
else {
$actions['submit']['#value'] = $this
->t('Save @team', [
'@team' => $label,
]);
}
return $actions;
}
public function save(array $form, FormStateInterface $form_state) {
$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 {
$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;
}
}