class TeamForm in Apigee Edge 8
General form handler for the team create/edit forms.
Hierarchy
- class \Drupal\Core\Form\FormBase implements ContainerInjectionInterface, FormInterface uses DependencySerializationTrait, LoggerChannelTrait, MessengerTrait, LinkGeneratorTrait, RedirectDestinationTrait, UrlGeneratorTrait, StringTranslationTrait
- class \Drupal\Core\Entity\EntityForm implements EntityFormInterface
- class \Drupal\apigee_edge\Entity\Form\FieldableEdgeEntityForm implements FieldableEdgeEntityFormInterface
- class \Drupal\apigee_edge_teams\Entity\Form\TeamForm implements EdgeEntityFormInterface
- class \Drupal\apigee_edge\Entity\Form\FieldableEdgeEntityForm implements FieldableEdgeEntityFormInterface
- class \Drupal\Core\Entity\EntityForm implements EntityFormInterface
Expanded class hierarchy of TeamForm
File
- modules/
apigee_edge_teams/ src/ Entity/ Form/ TeamForm.php, line 38
Namespace
Drupal\apigee_edge_teams\Entity\FormView source
class TeamForm extends FieldableEdgeEntityForm implements EdgeEntityFormInterface {
/**
* Admin email attribute name.
*/
const ADMIN_EMAIL_ATTRIBUTE = 'ADMIN_EMAIL';
/**
* The team membership manager service.
*
* @var \Drupal\apigee_edge_teams\TeamMembershipManagerInterface
*/
protected $teamMembershipManager;
/**
* The current user.
*
* @var \Drupal\Core\Session\AccountProxyInterface
*/
protected $currentUser;
/**
* The logger service.
*
* @var \Drupal\Core\Logger\LoggerChannelInterface
*/
protected $logger;
/**
* TeamForm constructor.
*
* @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
* The entity type manager service.
* @param \Drupal\apigee_edge_teams\TeamMembershipManagerInterface $team_membership_manager
* The team membership manager service.
* @param \Drupal\Core\Session\AccountProxyInterface $current_user
* The current user.
* @param \Drupal\Core\Logger\LoggerChannelInterface $logger
* The logger service.
*/
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;
}
/**
* {@inheritdoc}
*/
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'));
}
/**
* {@inheritdoc}
*/
public function buildEntity(array $form, FormStateInterface $form_state) {
/** @var \Drupal\apigee_edge_teams\Entity\TeamInterface $team */
$team = parent::buildEntity($form, $form_state);
// ADMIN_EMAIL_ATTRIBUTE is a required field for monetization.
// We add to any team to make sure team creation works for mint orgs even
// if they do not enable the m10n teams module.
$team
->setAttribute(static::ADMIN_EMAIL_ATTRIBUTE, $this->currentUser
->getEmail());
return $team;
}
/**
* {@inheritdoc}
*/
public function form(array $form, FormStateInterface $form_state) {
$form = parent::form($form, $form_state);
/** @var \Drupal\apigee_edge_teams\Entity\TeamInterface $team */
$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;
}
/**
* Checks if a team already exists with the same name.
*
* @param string $name
* Team name.
* @param array $element
* Form element.
* @param \Drupal\Core\Form\FormStateInterface $form_state
* The current state of the complete form.
*
* @return bool
* TRUE if the team exists, else FALSE.
*/
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();
}
/**
* {@inheritdoc}
*/
protected function actions(array $form, FormStateInterface $form_state) {
/** @var \Drupal\apigee_edge_teams\Entity\TeamInterface $team */
$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;
}
/**
* {@inheritdoc}
*/
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;
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
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 | |
EntityForm:: |
protected | property | The entity type manager. | 3 |
EntityForm:: |
protected | property | The module handler service. | |
EntityForm:: |
protected | property | The name of the current operation. | |
EntityForm:: |
private | property | The entity manager. | |
EntityForm:: |
protected | function | Returns the action form element for the current entity form. | |
EntityForm:: |
public | function | Form element #after_build callback: Updates the entity with submitted data. | |
EntityForm:: |
public | function |
Form constructor. Overrides FormInterface:: |
10 |
EntityForm:: |
public | function |
Returns a string identifying the base form. Overrides BaseFormIdInterface:: |
5 |
EntityForm:: |
public | function |
Gets the form entity. Overrides EntityFormInterface:: |
|
EntityForm:: |
public | function |
Determines which entity will be used by this form from a RouteMatch object. Overrides EntityFormInterface:: |
1 |
EntityForm:: |
public | function |
Returns a unique string identifying the form. Overrides FormInterface:: |
10 |
EntityForm:: |
public | function |
Gets the operation identifying the form. Overrides EntityFormInterface:: |
|
EntityForm:: |
protected | function | Prepares the entity object before the form is built first. | 3 |
EntityForm:: |
protected | function | Invokes the specified prepare hook variant. | |
EntityForm:: |
public | function | Process callback: assigns weights and hides extra fields. | |
EntityForm:: |
public | function |
Sets the form entity. Overrides EntityFormInterface:: |
|
EntityForm:: |
public | function |
Sets the entity manager for this form. Overrides EntityFormInterface:: |
|
EntityForm:: |
public | function |
Sets the entity type manager for this form. Overrides EntityFormInterface:: |
|
EntityForm:: |
public | function |
Sets the module handler for this form. Overrides EntityFormInterface:: |
|
EntityForm:: |
public | function |
Sets the operation for this form. Overrides EntityFormInterface:: |
|
EntityForm:: |
public | function |
This is the default entity object builder function. It is called before any
other submit handler to build the new entity object to be used by the
following submit handlers. At this point of the form workflow the entity is
validated and the form state… Overrides FormInterface:: |
17 |
EntityForm:: |
public | function | ||
EntityForm:: |
public | function | ||
FieldableEdgeEntityForm:: |
protected | property |
The fieldable entity being used by this form. Overrides EntityForm:: |
|
FieldableEdgeEntityForm:: |
protected | function |
Copies top-level form values to entity properties Overrides EntityForm:: |
|
FieldableEdgeEntityForm:: |
protected | function | Flags violations for the current form. | |
FieldableEdgeEntityForm:: |
protected | function | Gets the names of all fields edited in the form. | |
FieldableEdgeEntityForm:: |
public | function |
Gets the form display. Overrides FieldableEdgeEntityFormInterface:: |
|
FieldableEdgeEntityForm:: |
protected | function |
Initialize the form state and the entity before the first form build. Overrides EntityForm:: |
|
FieldableEdgeEntityForm:: |
public | function |
Sets the form display. Overrides FieldableEdgeEntityFormInterface:: |
|
FieldableEdgeEntityForm:: |
public | function |
TODO Add missing return type-hint in 2.x. Overrides FormBase:: |
|
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. | |
TeamForm:: |
protected | property | The current user. | |
TeamForm:: |
protected | property | The logger service. | |
TeamForm:: |
protected | property | The team membership manager service. | |
TeamForm:: |
protected | function |
Returns an array of supported actions for the current entity form. Overrides EntityForm:: |
|
TeamForm:: |
constant | Admin email attribute name. | ||
TeamForm:: |
public | function |
Builds an updated entity object based upon the submitted form values. Overrides EntityForm:: |
|
TeamForm:: |
public static | function |
Instantiates a new instance of this class. Overrides FormBase:: |
|
TeamForm:: |
public | function | Checks if a team already exists with the same name. | |
TeamForm:: |
public | function |
Gets the actual form array to be built. Overrides FieldableEdgeEntityForm:: |
|
TeamForm:: |
public | function |
Form submission handler for the 'save' action. Overrides EntityForm:: |
|
TeamForm:: |
public | function | TeamForm constructor. | |
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. |