You are here

class GroupRequestController 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/Controller/GroupRequestController.php \Drupal\social_group_request\Controller\GroupRequestController
  2. 10.3.x modules/social_features/social_group/modules/social_group_request/src/Controller/GroupRequestController.php \Drupal\social_group_request\Controller\GroupRequestController
  3. 10.0.x modules/social_features/social_group/modules/social_group_request/src/Controller/GroupRequestController.php \Drupal\social_group_request\Controller\GroupRequestController
  4. 10.1.x modules/social_features/social_group/modules/social_group_request/src/Controller/GroupRequestController.php \Drupal\social_group_request\Controller\GroupRequestController

Returns responses for Group request routes.

Hierarchy

Expanded class hierarchy of GroupRequestController

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

File

modules/social_features/social_group/modules/social_group_request/src/Controller/GroupRequestController.php, line 27

Namespace

Drupal\social_group_request\Controller
View source
class GroupRequestController extends ControllerBase {

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

  /**
   * GroupRequestController constructor.
   *
   * @param \Drupal\Core\Form\FormBuilderInterface $form_builder
   *   The form builder.
   * @param \Drupal\Core\Entity\EntityFormBuilderInterface $entity_form_builder
   *   The entity form builder.
   * @param \Drupal\Core\Messenger\MessengerInterface $messenger
   *   The messenger.
   * @param \Drupal\Core\Cache\CacheTagsInvalidatorInterface $cache_tags_invalidator
   *   The cache tags invalidator.
   * @param \Drupal\Core\StringTranslation\TranslationInterface $string_translation
   *   The string translation.
   * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
   *   The entity type manager.
   * @param \Drupal\Core\Session\AccountInterface $current_user
   *   The current user.
   */
  public function __construct(FormBuilderInterface $form_builder, EntityFormBuilderInterface $entity_form_builder, MessengerInterface $messenger, CacheTagsInvalidatorInterface $cache_tags_invalidator, TranslationInterface $string_translation, EntityTypeManagerInterface $entity_type_manager, AccountInterface $current_user) {
    $this->formBuilder = $form_builder;
    $this->entityFormBuilder = $entity_form_builder;
    $this
      ->setMessenger($messenger);
    $this->cacheTagsInvalidator = $cache_tags_invalidator;
    $this
      ->setStringTranslation($string_translation);
    $this->entityTypeManager = $entity_type_manager;
    $this->currentUser = $current_user;
  }

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

  /**
   * Return the title for approve request confirmation page.
   */
  public function getTitleApproveRequest(GroupInterface $group, GroupContentInterface $group_content) {
    return $this
      ->t('Approve membership request for the group @group_title', [
      '@group_title' => $group
        ->label(),
    ]);
  }

  /**
   * Return the title for reject request confirmation page.
   */
  public function getTitleRejectRequest(GroupInterface $group, GroupContentInterface $group_content) {
    return $this
      ->t('Reject membership request for the group @group_title', [
      '@group_title' => $group
        ->label(),
    ]);
  }

  /**
   * Builds the form to create new membership on membership request approve.
   */
  public function approveRequest(GroupInterface $group, GroupContentInterface $group_content) {

    /** @var \Drupal\group\Plugin\GroupContentEnablerInterface $plugin */
    $plugin = $group
      ->getGroupType()
      ->getContentPlugin('group_membership');

    // Pre-populate a group membership from Membership request.
    $group_content = $this
      ->entityTypeManager()
      ->getStorage('group_content')
      ->create([
      'type' => $plugin
        ->getContentTypeConfigId(),
      'gid' => $group
        ->id(),
      'entity_id' => $group_content
        ->getEntity()
        ->id(),
    ]);
    $this->cacheTagsInvalidator
      ->invalidateTags([
      'request-membership:' . $group
        ->id(),
    ]);
    return $this
      ->entityFormBuilder()
      ->getForm($group_content, 'add');
  }

  /**
   * Callback to request membership.
   */
  public function requestMembership(GroupInterface $group) {
    $response = new AjaxResponse();
    $contentTypeConfigId = $group
      ->getGroupType()
      ->getContentPlugin('group_membership_request')
      ->getContentTypeConfigId();
    $request = $this
      ->entityTypeManager()
      ->getStorage('group_content')
      ->getQuery()
      ->condition('type', $contentTypeConfigId)
      ->condition('gid', $group
      ->id())
      ->condition('entity_id', $this
      ->currentUser()
      ->id())
      ->condition('grequest_status', GroupMembershipRequest::REQUEST_PENDING)
      ->count()
      ->execute();
    if ($request == 0) {
      $request_form = $this
        ->formBuilder()
        ->getForm(GroupRequestMembershipRequestForm::class, $group);
      $response
        ->addCommand(new OpenModalDialogCommand($this
        ->t('Request to join'), $request_form, [
        'width' => '582px',
        'dialogClass' => 'social_group-popup',
      ]));
    }
    return $response;
  }

  /**
   * Callback to request membership for anonymous.
   */
  public function anonymousRequestMembership(GroupInterface $group) {
    $request_form = $this
      ->formBuilder()
      ->getForm(GroupRequestMembershipRequestAnonymousForm::class, $group);
    $response = new AjaxResponse();
    $response
      ->addCommand(new OpenModalDialogCommand($this
      ->t('Request to join'), $request_form, [
      'width' => '337px',
      'dialogClass' => 'social_group-popup social_group-popup--anonymous',
    ]));
    return $response;
  }

  /**
   * Callback to cancel the request of membership.
   */
  public function cancelRequest(GroupInterface $group) {
    $content_type_config_id = $group
      ->getGroupType()
      ->getContentPlugin('group_membership_request')
      ->getContentTypeConfigId();
    $requests = $this
      ->entityTypeManager()
      ->getStorage('group_content')
      ->loadByProperties([
      'type' => $content_type_config_id,
      'gid' => $group
        ->id(),
      'entity_id' => $this
        ->currentUser()
        ->id(),
      'grequest_status' => GroupMembershipRequest::REQUEST_PENDING,
    ]);
    foreach ($requests as $request) {
      $request
        ->delete();
    }
    $this
      ->messenger()
      ->addMessage($this
      ->t('Membership has been successfully denied.'));
    $this->cacheTagsInvalidator
      ->invalidateTags([
      'request-membership:' . $group
        ->id(),
    ]);
    return $this
      ->redirect('social_group.stream', [
      'group' => $group
        ->id(),
    ]);
  }

  /**
   * Checks access for a specific route request to see if user can see requests.
   *
   * @param \Drupal\Core\Session\AccountInterface $account
   *   Run access checks for this account.
   *
   * @return \Drupal\Core\Access\AccessResultInterface
   *   The access result.
   */
  public function routeAccess(AccountInterface $account) {

    // @todo refactor this when Group entity query access lands.
    $has_administer_users = $account
      ->hasPermission('administer members');
    if ($has_administer_users) {
      return AccessResult::allowed();
    }
    $group = _social_group_get_current_group();
    if (!$group instanceof Group) {
      $group_id = \Drupal::routeMatch()
        ->getParameter('group');

      // Views upcasting is lame.
      if (!isset($group_id)) {
        $group_id = \Drupal::routeMatch()
          ->getParameter('arg_0');
      }
      $group = Group::load($group_id);
    }
    $is_group_page = isset($group);
    $is_group_manager = $group
      ->hasPermission('administer members', $account);
    return AccessResult::allowedIf($is_group_page && $is_group_manager);
  }

}

Members

Namesort descending Modifiers Type Description Overrides
ControllerBase::$configFactory protected property The configuration factory.
ControllerBase::$currentUser protected property The current user service. 1
ControllerBase::$entityFormBuilder protected property The entity form builder.
ControllerBase::$entityTypeManager protected property The entity type manager.
ControllerBase::$formBuilder protected property The form builder. 2
ControllerBase::$keyValue protected property The key-value storage. 1
ControllerBase::$languageManager protected property The language manager. 1
ControllerBase::$moduleHandler protected property The module handler. 2
ControllerBase::$stateService protected property The state service.
ControllerBase::cache protected function Returns the requested cache bin.
ControllerBase::config protected function Retrieves a configuration object.
ControllerBase::container private function Returns the service container.
ControllerBase::currentUser protected function Returns the current user. 1
ControllerBase::entityFormBuilder protected function Retrieves the entity form builder.
ControllerBase::entityTypeManager protected function Retrieves the entity type manager.
ControllerBase::formBuilder protected function Returns the form builder service. 2
ControllerBase::keyValue protected function Returns a key/value storage collection. 1
ControllerBase::languageManager protected function Returns the language manager service. 1
ControllerBase::moduleHandler protected function Returns the module handler. 2
ControllerBase::redirect protected function Returns a redirect response object for the specified route.
ControllerBase::state protected function Returns the state storage service.
GroupRequestController::$cacheTagsInvalidator protected property The cache tags invalidator.
GroupRequestController::anonymousRequestMembership public function Callback to request membership for anonymous.
GroupRequestController::approveRequest public function Builds the form to create new membership on membership request approve.
GroupRequestController::cancelRequest public function Callback to cancel the request of membership.
GroupRequestController::create public static function Instantiates a new instance of this class. Overrides ControllerBase::create
GroupRequestController::getTitleApproveRequest public function Return the title for approve request confirmation page.
GroupRequestController::getTitleRejectRequest public function Return the title for reject request confirmation page.
GroupRequestController::requestMembership public function Callback to request membership.
GroupRequestController::routeAccess public function Checks access for a specific route request to see if user can see requests.
GroupRequestController::__construct public function GroupRequestController 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.