class AddTeamMembersForm in Apigee Edge 8
Add team members form.
Hierarchy
- class \Drupal\Core\Form\FormBase implements ContainerInjectionInterface, FormInterface uses DependencySerializationTrait, LoggerChannelTrait, MessengerTrait, LinkGeneratorTrait, RedirectDestinationTrait, UrlGeneratorTrait, StringTranslationTrait
- class \Drupal\apigee_edge_teams\Form\TeamMembersFormBase
- class \Drupal\apigee_edge_teams\Form\AddTeamMembersForm
- class \Drupal\apigee_edge_teams\Form\TeamMembersFormBase
Expanded class hierarchy of AddTeamMembersForm
1 file declares its use of AddTeamMembersForm
- TeamRouteProvider.php in modules/
apigee_edge_teams/ src/ Entity/ TeamRouteProvider.php
File
- modules/
apigee_edge_teams/ src/ Form/ AddTeamMembersForm.php, line 34
Namespace
Drupal\apigee_edge_teams\FormView source
class AddTeamMembersForm extends TeamMembersFormBase {
/**
* The team membership manager service.
*
* @var \Drupal\apigee_edge_teams\TeamMembershipManagerInterface
*/
protected $teamMembershipManager;
/**
* The user storage.
*
* @var \Drupal\user\UserStorageInterface
*/
protected $userStorage;
/**
* The team invitation storage.
*
* @var \Drupal\apigee_edge_teams\Entity\Storage\TeamInvitationStorageInterface
*/
protected $teamInvitationStorage;
/**
* AddTeamMemberForms constructor.
*
* @param \Drupal\apigee_edge_teams\TeamMembershipManagerInterface $team_membership_manager
* The team membership manager service.
* @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
* The entity type manager.
*/
public function __construct(TeamMembershipManagerInterface $team_membership_manager, EntityTypeManagerInterface $entity_type_manager) {
parent::__construct($entity_type_manager);
$this->teamMembershipManager = $team_membership_manager;
$this->userStorage = $entity_type_manager
->getStorage('user');
$this->teamInvitationStorage = $entity_type_manager
->getStorage('team_invitation');
}
/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container) {
return new static($container
->get('apigee_edge_teams.team_membership_manager'), $container
->get('entity_type.manager'));
}
/**
* {@inheritdoc}
*/
public function getFormId() {
return 'apigee_edge_teams_add_team_member_form';
}
/**
* {@inheritdoc}
*/
public function buildForm(array $form, FormStateInterface $form_state, TeamInterface $team = NULL) {
$this->team = $team;
$role_options = $this
->getRoleOptions();
$form['developers'] = [
'#title' => $this
->t('Developers'),
'#description' => $this
->t('Enter the email of one or more developers to invite them to the @team, separated by comma.', [
'@team' => mb_strtolower($this->team
->getEntityType()
->getSingularLabel()),
]),
'#type' => 'textarea',
'#required' => TRUE,
];
$form['team_roles'] = [
'#type' => 'checkboxes',
'#title' => $this
->t('Roles'),
'#options' => $role_options,
'#multiple' => TRUE,
'#required' => FALSE,
];
// Special handling for the inevitable team member role.
$form['team_roles'][TeamRoleInterface::TEAM_MEMBER_ROLE] = [
'#default_value' => TRUE,
'#disabled' => TRUE,
];
$form['team_roles']['description'] = [
'#markup' => $this
->t('Assign one or more roles to <em>all developers</em> that you selected in %team_label @team.', [
'%team_label' => $this->team
->label(),
'@team' => mb_strtolower($this->team
->getEntityType()
->getSingularLabel()),
]),
];
$form['actions'] = [
'#type' => 'actions',
'submit' => [
'#type' => 'submit',
'#value' => $this
->t('Invite members'),
'#button_type' => 'primary',
],
'cancel' => [
'#type' => 'link',
'#title' => $this
->t('Cancel'),
'#attributes' => [
'class' => [
'button',
],
],
'#url' => $this->team
->toUrl('members'),
],
];
return $form;
}
/**
* Return an array of user UIDs given a list of emails.
*
* @param string $emails
* The emails, comma separated.
*
* @return array
* An array containing a first array of user accounts, and a second array of
* emails that have no account on the system.
*
* @deprecated in apigee_edge_teams:8.x-1.12 and is removed from apigee_edge_teams:2.x. No replacement.
*
* @see https://github.com/apigee/apigee-edge-drupal/pull/474
*/
protected function getAccountsFromEmails(string $emails) : array {
$developerEmails = [];
$notFound = [];
$emails = array_map('trim', explode(',', $emails));
foreach ($emails as $email) {
if ($account = user_load_by_mail($email)) {
$developerEmails[$email] = $account;
}
else {
$notFound[] = $email;
}
}
return [
$developerEmails,
$notFound,
];
}
/**
* {@inheritdoc}
*/
public function validateForm(array &$form, FormStateInterface $form_state) {
$emails = array_map('trim', explode(',', $form_state
->getValue('developers', '')));
$members = $this->teamMembershipManager
->getMembers($this->team
->id());
$already_members = array_unique(array_intersect($emails, $members));
// Validate existing members.
if (count($already_members)) {
$form_state
->setErrorByName('developers', $this
->formatPlural(count($already_members), 'The following developer is already a member of the @team: %developers.', 'The following developers are already members of the @team: %developers.', [
'%developers' => implode(', ', $already_members),
'@team' => mb_strtolower($this->team
->getEntityType()
->getSingularLabel()),
]));
}
// Validate pending invitations.
$invites = array_diff($emails, $members);
$has_invitation = [];
foreach ($invites as $invite) {
$pending_invitations = array_filter($this->teamInvitationStorage
->loadByRecipient($invite, $this->team
->id()), function (TeamInvitationInterface $team_invitation) {
return $team_invitation
->isPending();
});
if (count($pending_invitations)) {
$has_invitation[] = $invite;
}
}
$has_invitation = array_unique($has_invitation);
if (count($has_invitation)) {
$form_state
->setErrorByName('developers', $this
->formatPlural(count($has_invitation), 'The following developer has already been invited to the @team: %developers.', 'The following developers have already been invited to the @team: %developers.', [
'%developers' => implode(', ', $has_invitation),
'@team' => mb_strtolower($this->team
->getEntityType()
->getSingularLabel()),
]));
}
parent::validateForm($form, $form_state);
}
/**
* {@inheritdoc}
*/
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'));
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
AddTeamMembersForm:: |
protected | property | The team invitation storage. | |
AddTeamMembersForm:: |
protected | property | The team membership manager service. | |
AddTeamMembersForm:: |
protected | property | The user storage. | |
AddTeamMembersForm:: |
public | function |
Form constructor. Overrides FormInterface:: |
|
AddTeamMembersForm:: |
public static | function |
Instantiates a new instance of this class. Overrides TeamMembersFormBase:: |
|
AddTeamMembersForm:: |
protected | function | Return an array of user UIDs given a list of emails. | |
AddTeamMembersForm:: |
public | function |
Returns a unique string identifying the form. Overrides FormInterface:: |
|
AddTeamMembersForm:: |
public | function |
Form submission handler. Overrides FormInterface:: |
|
AddTeamMembersForm:: |
public | function |
Form validation handler. Overrides FormBase:: |
|
AddTeamMembersForm:: |
public | function |
AddTeamMemberForms constructor. Overrides TeamMembersFormBase:: |
|
DependencySerializationTrait:: |
protected | property | An array of entity type IDs keyed by the property name of their storages. | |
DependencySerializationTrait:: |
protected | property | An array of service IDs keyed by property name used for serialization. | |
DependencySerializationTrait:: |
public | function | 1 | |
DependencySerializationTrait:: |
public | function | 2 | |
FormBase:: |
protected | property | The config factory. | 1 |
FormBase:: |
protected | property | The request stack. | 1 |
FormBase:: |
protected | property | The route match. | |
FormBase:: |
protected | function | Retrieves a configuration object. | |
FormBase:: |
protected | function | Gets the config factory for this form. | 1 |
FormBase:: |
private | function | Returns the service container. | |
FormBase:: |
protected | function | Gets the current user. | |
FormBase:: |
protected | function | Gets the request object. | |
FormBase:: |
protected | function | Gets the route match. | |
FormBase:: |
protected | function | Gets the logger for a specific channel. | |
FormBase:: |
protected | function |
Returns a redirect response object for the specified route. Overrides UrlGeneratorTrait:: |
|
FormBase:: |
public | function | Resets the configuration factory. | |
FormBase:: |
public | function | Sets the config factory for this form. | |
FormBase:: |
public | function | Sets the request stack object to use. | |
LinkGeneratorTrait:: |
protected | property | The link generator. | 1 |
LinkGeneratorTrait:: |
protected | function | Returns the link generator. | |
LinkGeneratorTrait:: |
protected | function | Renders a link to a route given a route name and its parameters. | |
LinkGeneratorTrait:: |
public | function | Sets the link generator service. | |
LoggerChannelTrait:: |
protected | property | The logger channel factory service. | |
LoggerChannelTrait:: |
protected | function | Gets the logger for a specific channel. | |
LoggerChannelTrait:: |
public | function | Injects the logger channel factory. | |
MessengerTrait:: |
protected | property | The messenger. | 29 |
MessengerTrait:: |
public | function | Gets the messenger. | 29 |
MessengerTrait:: |
public | function | Sets the messenger. | |
RedirectDestinationTrait:: |
protected | property | The redirect destination service. | 1 |
RedirectDestinationTrait:: |
protected | function | Prepares a 'destination' URL query parameter for use with \Drupal\Core\Url. | |
RedirectDestinationTrait:: |
protected | function | Returns the redirect destination service. | |
RedirectDestinationTrait:: |
public | function | Sets the redirect destination service. | |
StringTranslationTrait:: |
protected | property | The string translation service. | 1 |
StringTranslationTrait:: |
protected | function | Formats a string containing a count of items. | |
StringTranslationTrait:: |
protected | function | Returns the number of plurals supported by a given language. | |
StringTranslationTrait:: |
protected | function | Gets the string translation service. | |
StringTranslationTrait:: |
public | function | Sets the string translation service to use. | 2 |
StringTranslationTrait:: |
protected | function | Translates a string to the current language or to a given language. | |
TeamMembersFormBase:: |
protected | property | The team from the route. | |
TeamMembersFormBase:: |
protected | property | Team member role storage. | |
TeamMembersFormBase:: |
protected | property | Team role storage. | |
TeamMembersFormBase:: |
protected | function | Helper function to filter the value of a team roles checkboxes element. | |
TeamMembersFormBase:: |
protected | function | Returns an array of team role options keyed by team role id. | |
UrlGeneratorTrait:: |
protected | property | The url generator. | |
UrlGeneratorTrait:: |
protected | function | Returns the URL generator service. | |
UrlGeneratorTrait:: |
public | function | Sets the URL generator service. | |
UrlGeneratorTrait:: |
protected | function | Generates a URL or path for a specific route based on the given parameters. |