You are here

class RemoveTeamMemberForm in Apigee Edge 8

Remove team members from.

Hierarchy

Expanded class hierarchy of RemoveTeamMemberForm

1 file declares its use of RemoveTeamMemberForm
TeamRouteProvider.php in modules/apigee_edge_teams/src/Entity/TeamRouteProvider.php

File

modules/apigee_edge_teams/src/Form/RemoveTeamMemberForm.php, line 35

Namespace

Drupal\apigee_edge_teams\Form
View source
class RemoveTeamMemberForm extends ConfirmFormBase {

  /**
   * The team from the route.
   *
   * @var \Drupal\apigee_edge_teams\Entity\TeamInterface
   */
  protected $team;

  /**
   * The developer from the route.
   *
   * @var \Drupal\apigee_edge\Entity\DeveloperInterface
   */
  protected $developer;

  /**
   * The team entity type definition.
   *
   * @var \Drupal\Core\Entity\EntityTypeInterface
   */
  protected $teamEntityType;

  /**
   * The team membership manager service.
   *
   * @var \Drupal\apigee_edge_teams\TeamMembershipManagerInterface
   */
  protected $teamMembershipManager;

  /**
   * The user storage.
   *
   * @var \Drupal\user\UserStorageInterface
   */
  protected $userStorage;

  /**
   * RemoveTeamMemberForm 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.
   */
  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;
  }

  /**
   * {@inheritdoc}
   */
  public static function create(ContainerInterface $container) {
    return new static($container
      ->get('entity_type.manager'), $container
      ->get('apigee_edge_teams.team_membership_manager'));
  }

  /**
   * {@inheritdoc}
   */
  public function getFormId() {
    return 'apigee_edge_teams_remove_team_member_form';
  }

  /**
   * {@inheritdoc}
   */
  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);
  }

  /**
   * {@inheritdoc}
   */
  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()),
    ]);
  }

  /**
   * {@inheritdoc}
   */
  public function getCancelUrl() {
    return $this->team
      ->toUrl('members');
  }

  /**
   * {@inheritdoc}
   */
  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);
  }

  /**
   * {@inheritdoc}
   */
  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));
    }
  }

  /**
   * Returns the label for a developer.
   *
   * @return string
   *   The label for a developer.
   */
  protected function getDeveloperLabel() {
    $developer_label = $this->developer
      ->getEmail();

    // The developer that we would like to remove from the team may not have
    // a Drupal user yet (the two system is out of sync). To resolve this
    // possible problem we always try to display the label of the user first
    // and we fallback to the developer's email if necessary. We only display
    // the email here because this is what a user could see on the team member
    // list as well.
    // @see \Drupal\apigee_edge_teams\Controller\TeamMembersList::buildRow()
    $users = $this->userStorage
      ->loadByProperties([
      'mail' => $this->developer
        ->getEmail(),
    ]);
    if (!empty($users)) {

      /** @var \Drupal\user\UserInterface $user */
      $user = reset($users);
      $developer_label = $user
        ->label();
    }
    return $developer_label;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
ConfirmFormBase::getCancelText public function Returns a caption for the link which cancels the action. Overrides ConfirmFormInterface::getCancelText 1
ConfirmFormBase::getConfirmText public function Returns a caption for the button that confirms the action. Overrides ConfirmFormInterface::getConfirmText 20
ConfirmFormBase::getDescription public function Returns additional text to display as a description. Overrides ConfirmFormInterface::getDescription 11
ConfirmFormBase::getFormName public function Returns the internal name used to refer to the confirmation item. Overrides ConfirmFormInterface::getFormName
DependencySerializationTrait::$_entityStorages protected property An array of entity type IDs keyed by the property name of their storages.
DependencySerializationTrait::$_serviceIds protected property An array of service IDs keyed by property name used for serialization.
DependencySerializationTrait::__sleep public function 1
DependencySerializationTrait::__wakeup public function 2
FormBase::$configFactory protected property The config factory. 1
FormBase::$requestStack protected property The request stack. 1
FormBase::$routeMatch protected property The route match.
FormBase::config protected function Retrieves a configuration object.
FormBase::configFactory protected function Gets the config factory for this form. 1
FormBase::container private function Returns the service container.
FormBase::currentUser protected function Gets the current user.
FormBase::getRequest protected function Gets the request object.
FormBase::getRouteMatch protected function Gets the route match.
FormBase::logger protected function Gets the logger for a specific channel.
FormBase::redirect protected function Returns a redirect response object for the specified route. Overrides UrlGeneratorTrait::redirect
FormBase::resetConfigFactory public function Resets the configuration factory.
FormBase::setConfigFactory public function Sets the config factory for this form.
FormBase::setRequestStack public function Sets the request stack object to use.
LinkGeneratorTrait::$linkGenerator protected property The link generator. 1
LinkGeneratorTrait::getLinkGenerator Deprecated protected function Returns the link generator.
LinkGeneratorTrait::l Deprecated protected function Renders a link to a route given a route name and its parameters.
LinkGeneratorTrait::setLinkGenerator Deprecated public function Sets the link generator service.
LoggerChannelTrait::$loggerFactory protected property The logger channel factory service.
LoggerChannelTrait::getLogger protected function Gets the logger for a specific channel.
LoggerChannelTrait::setLoggerFactory public function Injects the logger channel factory.
MessengerTrait::$messenger protected property The messenger. 29
MessengerTrait::messenger public function Gets the messenger. 29
MessengerTrait::setMessenger public function Sets the messenger.
RedirectDestinationTrait::$redirectDestination protected property The redirect destination service. 1
RedirectDestinationTrait::getDestinationArray protected function Prepares a 'destination' URL query parameter for use with \Drupal\Core\Url.
RedirectDestinationTrait::getRedirectDestination protected function Returns the redirect destination service.
RedirectDestinationTrait::setRedirectDestination public function Sets the redirect destination service.
RemoveTeamMemberForm::$developer protected property The developer from the route.
RemoveTeamMemberForm::$team protected property The team from the route.
RemoveTeamMemberForm::$teamEntityType protected property The team entity type definition.
RemoveTeamMemberForm::$teamMembershipManager protected property The team membership manager service.
RemoveTeamMemberForm::$userStorage protected property The user storage.
RemoveTeamMemberForm::buildForm public function Form constructor. Overrides ConfirmFormBase::buildForm
RemoveTeamMemberForm::create public static function Instantiates a new instance of this class. Overrides FormBase::create
RemoveTeamMemberForm::getCancelUrl public function Returns the route to go to if the user cancels the action. Overrides ConfirmFormInterface::getCancelUrl
RemoveTeamMemberForm::getDeveloperLabel protected function Returns the label for a developer.
RemoveTeamMemberForm::getFormId public function Returns a unique string identifying the form. Overrides FormInterface::getFormId
RemoveTeamMemberForm::getQuestion public function Returns the question to ask the user. Overrides ConfirmFormInterface::getQuestion
RemoveTeamMemberForm::submitForm public function Form submission handler. Overrides FormInterface::submitForm
RemoveTeamMemberForm::validateForm public function Form validation handler. Overrides FormBase::validateForm
RemoveTeamMemberForm::__construct public function RemoveTeamMemberForm constructor.
StringTranslationTrait::$stringTranslation protected property The string translation service. 1
StringTranslationTrait::formatPlural protected function Formats a string containing a count of items.
StringTranslationTrait::getNumberOfPlurals protected function Returns the number of plurals supported by a given language.
StringTranslationTrait::getStringTranslation protected function Gets the string translation service.
StringTranslationTrait::setStringTranslation public function Sets the string translation service to use. 2
StringTranslationTrait::t protected function Translates a string to the current language or to a given language.
UrlGeneratorTrait::$urlGenerator protected property The url generator.
UrlGeneratorTrait::getUrlGenerator Deprecated protected function Returns the URL generator service.
UrlGeneratorTrait::setUrlGenerator Deprecated public function Sets the URL generator service.
UrlGeneratorTrait::url Deprecated protected function Generates a URL or path for a specific route based on the given parameters.