You are here

public function SocialGroupRequestMembershipNotification::build in Open Social 10.1.x

Same name and namespace in other branches
  1. 8.9 modules/social_features/social_group/modules/social_group_request/src/Plugin/Block/SocialGroupRequestMembershipNotification.php \Drupal\social_group_request\Plugin\Block\SocialGroupRequestMembershipNotification::build()
  2. 10.3.x modules/social_features/social_group/modules/social_group_request/src/Plugin/Block/SocialGroupRequestMembershipNotification.php \Drupal\social_group_request\Plugin\Block\SocialGroupRequestMembershipNotification::build()
  3. 10.0.x modules/social_features/social_group/modules/social_group_request/src/Plugin/Block/SocialGroupRequestMembershipNotification.php \Drupal\social_group_request\Plugin\Block\SocialGroupRequestMembershipNotification::build()
  4. 10.2.x modules/social_features/social_group/modules/social_group_request/src/Plugin/Block/SocialGroupRequestMembershipNotification.php \Drupal\social_group_request\Plugin\Block\SocialGroupRequestMembershipNotification::build()

Builds and returns the renderable array for this block plugin.

If a block should not be rendered because it has no content, then this method must also ensure to return no content: it must then only return an empty array, or an empty array with #cache set (with cacheability metadata indicating the circumstances for it being empty).

Return value

array A renderable array representing the content of the block.

Overrides BlockPluginInterface::build

See also

\Drupal\block\BlockViewBuilder

File

modules/social_features/social_group/modules/social_group_request/src/Plugin/Block/SocialGroupRequestMembershipNotification.php, line 117

Class

SocialGroupRequestMembershipNotification
Provides a 'Membership requests notification' block.

Namespace

Drupal\social_group_request\Plugin\Block

Code

public function build() {
  if (!$this->group
    ->getGroupType()
    ->hasContentPlugin('group_membership_request')) {
    return [];
  }
  $group_types = [
    'flexible_group',
  ];
  $this->moduleHandler
    ->alter('social_group_request', $group_types);
  if (in_array($this->group
    ->getGroupType()
    ->id(), $group_types)) {
    $join_methods = $this->group
      ->get('field_group_allowed_join_method')
      ->getValue();
    $request_option = in_array('request', array_column($join_methods, 'value'), FALSE);
    if (!$request_option) {
      return [];
    }
  }
  else {
    $allow_request = $this->group
      ->get('allow_request');
    if ($allow_request
      ->isEmpty() || $allow_request->value == 0) {
      return [];
    }
  }
  $contentTypeConfigId = $this->group
    ->getGroupType()
    ->getContentPlugin('group_membership_request')
    ->getContentTypeConfigId();
  $requests = $this->entityTypeManager
    ->getStorage('group_content')
    ->getQuery()
    ->condition('type', $contentTypeConfigId)
    ->condition('gid', $this->group
    ->id())
    ->condition('grequest_status', GroupMembershipRequest::REQUEST_PENDING)
    ->count()
    ->execute();
  if (!$requests) {
    return [];
  }
  return [
    '#type' => 'html_tag',
    '#tag' => 'div',
    '#value' => $this
      ->t('There @link to join this group.', [
      '@link' => Link::fromTextAndUrl($this->translation
        ->formatPlural($requests, 'is (1) new request', 'are (@count) new requests'), Url::fromRoute('view.group_pending_members.membership_requests', [
        'arg_0' => $this->group
          ->id(),
      ]))
        ->toString(),
    ]),
    '#attributes' => [
      'class' => [
        'alert',
        'alert-warning',
      ],
    ],
  ];
}