You are here

public function SocialEventInviteAccessHelper::eventFeatureAccess in Open Social 10.3.x

Same name and namespace in other branches
  1. 8.9 modules/social_features/social_event/modules/social_event_invite/src/SocialEventInviteAccessHelper.php \Drupal\social_event_invite\SocialEventInviteAccessHelper::eventFeatureAccess()
  2. 10.0.x modules/social_features/social_event/modules/social_event_invite/src/SocialEventInviteAccessHelper.php \Drupal\social_event_invite\SocialEventInviteAccessHelper::eventFeatureAccess()
  3. 10.1.x modules/social_features/social_event/modules/social_event_invite/src/SocialEventInviteAccessHelper.php \Drupal\social_event_invite\SocialEventInviteAccessHelper::eventFeatureAccess()
  4. 10.2.x modules/social_features/social_event/modules/social_event_invite/src/SocialEventInviteAccessHelper.php \Drupal\social_event_invite\SocialEventInviteAccessHelper::eventFeatureAccess()

Custom access check for the event invite features for event managers.

Return value

\Drupal\Core\Access\AccessResult Returns the access result.

Throws

\Drupal\Component\Plugin\Exception\InvalidPluginDefinitionException

\Drupal\Component\Plugin\Exception\PluginNotFoundException

File

modules/social_features/social_event/modules/social_event_invite/src/SocialEventInviteAccessHelper.php, line 89

Class

SocialEventInviteAccessHelper
Class SocialEventInviteAccessHelper.

Namespace

Drupal\social_event_invite

Code

public function eventFeatureAccess() {
  $config = $this->configFactory
    ->get('social_event_invite.settings');
  $enabled_global = $config
    ->get('invite_enroll');

  // If it's globally disabled, we don't want to show the block.
  if (!$enabled_global) {
    return AccessResult::forbidden();
  }

  // Get the group of this node.
  $node = $this->routeMatch
    ->getRawParameter('node');
  $node = Node::load($node);
  if ($node instanceof NodeInterface) {
    $node = $node
      ->id();
  }
  $gid_from_entity = $this->groupHelperService
    ->getGroupFromEntity([
    'target_type' => 'node',
    'target_id' => $node,
  ]);

  // If we have a group we need to additional checks.
  if ($gid_from_entity !== NULL) {

    /** @var \Drupal\group\Entity\GroupInterface $group */
    $group = $this->entityTypeManager
      ->getStorage('group')
      ->load($gid_from_entity);
    $enabled_for_group = $config
      ->get('invite_group_types');
    $enabled = FALSE;
    if (is_array($enabled_for_group)) {
      foreach ($enabled_for_group as $group_type) {
        if ($group_type === $group
          ->bundle()) {
          $enabled = TRUE;
          break;
        }
      }
    }

    // If it's not enabled for the group this event belongs to,
    // we don't want to show the block.
    if (!$enabled) {
      return AccessResult::forbidden();
    }
  }

  // If the user is not an event owner or organizer don't give access.
  // @todo can be combined with the next check into a service.
  if (!social_event_manager_or_organizer()) {
    return AccessResult::forbidden();
  }

  // If we've got this far we can be sure the user is allowed to see this
  // block.
  // @todo move that function to a service.
  return AccessResult::allowedIf(social_event_manager_or_organizer());
}