You are here

class GroupRequestMembershipRequestForm in Open Social 10.2.x

Same name and namespace in other branches
  1. 8.9 modules/social_features/social_group/modules/social_group_request/src/Form/GroupRequestMembershipRequestForm.php \Drupal\social_group_request\Form\GroupRequestMembershipRequestForm
  2. 10.3.x modules/social_features/social_group/modules/social_group_request/src/Form/GroupRequestMembershipRequestForm.php \Drupal\social_group_request\Form\GroupRequestMembershipRequestForm
  3. 10.0.x modules/social_features/social_group/modules/social_group_request/src/Form/GroupRequestMembershipRequestForm.php \Drupal\social_group_request\Form\GroupRequestMembershipRequestForm
  4. 10.1.x modules/social_features/social_group/modules/social_group_request/src/Form/GroupRequestMembershipRequestForm.php \Drupal\social_group_request\Form\GroupRequestMembershipRequestForm

Provides a form to request group membership.

Hierarchy

Expanded class hierarchy of GroupRequestMembershipRequestForm

1 file declares its use of GroupRequestMembershipRequestForm
GroupRequestController.php in modules/social_features/social_group/modules/social_group_request/src/Controller/GroupRequestController.php

File

modules/social_features/social_group/modules/social_group_request/src/Form/GroupRequestMembershipRequestForm.php, line 18

Namespace

Drupal\social_group_request\Form
View source
class GroupRequestMembershipRequestForm extends FormBase {

  /**
   * Group entity.
   *
   * @var \Drupal\group\Entity\GroupInterface
   */
  protected $group;

  /**
   * Group membership request.
   *
   * @var \Drupal\group\Entity\GroupContentInterface
   */
  protected $groupContent;

  /**
   * The cache tags invalidator.
   *
   * @var \Drupal\Core\Cache\CacheTagsInvalidatorInterface
   */
  protected $cacheTagsInvalidator;

  /**
   * The current user.
   *
   * @var \Drupal\Core\Session\AccountInterface
   */
  protected $currentUser;

  /**
   * Entity type manger.
   *
   * @var \Drupal\Core\Entity\EntityTypeManagerInterface
   */
  protected $entityTypeManager;

  /**
   * GroupRequestMembershipRejectForm constructor.
   *
   * @param \Drupal\Core\Cache\CacheTagsInvalidatorInterface $cache_tags_invalidator
   *   The cache tags invalidator.
   * @param \Drupal\Core\Session\AccountInterface $current_user
   *   The current user.
   * @param \Drupal\Core\StringTranslation\TranslationInterface $string_translation
   *   The string translation.
   * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
   *   The entity type manager.
   */
  public function __construct(CacheTagsInvalidatorInterface $cache_tags_invalidator, AccountInterface $current_user, TranslationInterface $string_translation, EntityTypeManagerInterface $entity_type_manager) {
    $this->cacheTagsInvalidator = $cache_tags_invalidator;
    $this->currentUser = $current_user;
    $this
      ->setStringTranslation($string_translation);
    $this->entityTypeManager = $entity_type_manager;
  }

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

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

  /**
   * {@inheritdoc}
   */
  public function buildForm(array $form, FormStateInterface $form_state, GroupInterface $group = NULL) {
    $this->group = $group;
    $form['description'] = [
      '#type' => 'html_tag',
      '#tag' => 'p',
      '#value' => $this
        ->t('You can leave a message in your request. Only when your request is approved, you will receive a notification via email and notification center.'),
    ];
    $form['message'] = [
      '#type' => 'textarea',
      '#title' => $this
        ->t('Message'),
    ];
    $form['actions']['submit'] = [
      '#type' => 'submit',
      '#value' => $this
        ->t('Send request'),
      '#button_type' => 'primary',
    ];
    return $form;
  }

  /**
   * {@inheritdoc}
   */
  public function submitForm(array &$form, FormStateInterface $form_state) {
    $content_type_config_id = $this->group
      ->getGroupType()
      ->getContentPlugin('group_membership_request')
      ->getContentTypeConfigId();
    $group_content = $this->entityTypeManager
      ->getStorage('group_content')
      ->create([
      'type' => $content_type_config_id,
      'gid' => $this->group
        ->id(),
      'entity_id' => $this
        ->currentUser()
        ->id(),
      'grequest_status' => GroupMembershipRequest::REQUEST_PENDING,
      'field_grequest_message' => $form_state
        ->getValue('message'),
    ]);
    $result = $group_content
      ->save();
    if ($result) {
      $this
        ->messenger()
        ->addMessage($this
        ->t('Your request has been sent successfully'));
    }
    else {
      $this
        ->messenger()
        ->addError($this
        ->t('Error when creating a request to join'));
    }
    $this->cacheTagsInvalidator
      ->invalidateTags([
      'request-membership:' . $this->group
        ->id(),
    ]);
    return $form_state
      ->setRedirect('social_group.stream', [
      'group' => $this->group
        ->id(),
    ]);
  }

}

Members

Namesort descending Modifiers Type Description Overrides
DependencySerializationTrait::$_entityStorages protected property
DependencySerializationTrait::$_serviceIds protected property
DependencySerializationTrait::__sleep public function 2
DependencySerializationTrait::__wakeup public function 2
FormBase::$configFactory protected property The config factory. 3
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. 3
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.
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.
FormBase::validateForm public function Form validation handler. Overrides FormInterface::validateForm 72
GroupRequestMembershipRequestForm::$cacheTagsInvalidator protected property The cache tags invalidator.
GroupRequestMembershipRequestForm::$currentUser protected property The current user.
GroupRequestMembershipRequestForm::$entityTypeManager protected property Entity type manger.
GroupRequestMembershipRequestForm::$group protected property Group entity.
GroupRequestMembershipRequestForm::$groupContent protected property Group membership request.
GroupRequestMembershipRequestForm::buildForm public function Form constructor. Overrides FormInterface::buildForm
GroupRequestMembershipRequestForm::create public static function Instantiates a new instance of this class. Overrides FormBase::create
GroupRequestMembershipRequestForm::getFormId public function Returns a unique string identifying the form. Overrides FormInterface::getFormId
GroupRequestMembershipRequestForm::submitForm public function Form submission handler. Overrides FormInterface::submitForm
GroupRequestMembershipRequestForm::__construct public function GroupRequestMembershipRejectForm constructor.
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. 27
MessengerTrait::messenger public function Gets the messenger. 27
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.
StringTranslationTrait::$stringTranslation protected property The string translation service. 4
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.