You are here

function social_sharing_block_access in Open Social 8.9

Same name and namespace in other branches
  1. 8 modules/social_features/social_sharing/social_sharing.module \social_sharing_block_access()
  2. 8.2 modules/social_features/social_sharing/social_sharing.module \social_sharing_block_access()
  3. 8.3 modules/social_features/social_sharing/social_sharing.module \social_sharing_block_access()
  4. 8.4 modules/social_features/social_sharing/social_sharing.module \social_sharing_block_access()
  5. 8.5 modules/social_features/social_sharing/social_sharing.module \social_sharing_block_access()
  6. 8.6 modules/social_features/social_sharing/social_sharing.module \social_sharing_block_access()
  7. 8.7 modules/social_features/social_sharing/social_sharing.module \social_sharing_block_access()
  8. 8.8 modules/social_features/social_sharing/social_sharing.module \social_sharing_block_access()
  9. 10.3.x modules/social_features/social_sharing/social_sharing.module \social_sharing_block_access()
  10. 10.0.x modules/social_features/social_sharing/social_sharing.module \social_sharing_block_access()
  11. 10.1.x modules/social_features/social_sharing/social_sharing.module \social_sharing_block_access()
  12. 10.2.x modules/social_features/social_sharing/social_sharing.module \social_sharing_block_access()

Implements hook_block_access().

File

modules/social_features/social_sharing/social_sharing.module, line 17
The Social Sharing module.

Code

function social_sharing_block_access(Block $block, $operation, AccountInterface $account) {
  if ($operation == 'view' && $block
    ->getPluginId() == 'shariff_block') {

    // Exclude Social Sharing block form Add and Edit node pages.
    $route_name = \Drupal::routeMatch()
      ->getRouteName();
    $excluded_routes = [
      'node.add',
      'entity.node.edit_form',
    ];

    // Call module handler service.
    $module_handler = \Drupal::service('module_handler');

    // Exclude Social Sharing block form event invite pages.
    if ($module_handler
      ->moduleExists('social_group_invite')) {
      $excluded_routes[] = 'ginvite.invitation.bulk';
      $excluded_routes[] = 'ginvite.invitation.bulk.confirm';
    }

    // Exclude Social Sharing block form group invite pages.
    if ($module_handler
      ->moduleExists('social_event_invite')) {
      $excluded_routes[] = 'social_event_invite.invite_email';
      $excluded_routes[] = 'social_event_invite.invite_user';
      $excluded_routes[] = 'social_event_invite.confirm_invite';
    }
    if (in_array($route_name, $excluded_routes)) {
      return AccessResult::forbidden();
    }
    else {

      // When a share block is assigned to a entity, and the entity field
      // settings are not Public. We prevent the block from being displayed.
      $nid = \Drupal::routeMatch()
        ->getRawParameter('node');

      // We have a node!
      if (!empty($nid) && ($node = Node::load($nid))) {
        $field_definitions = $node
          ->getFieldDefinitions();

        /* @var \Drupal\Core\Field\FieldConfigInterface $field_definition */
        foreach ($field_definitions as $field_name => $field_definition) {

          // Lets fetch all the entity access fields on this current node.
          if ($field_definition
            ->getType() === 'entity_access_field') {

            // Lets get all the values that we have for our
            // entity_access_fields.
            $field_values = $node
              ->get($field_name)
              ->getValue();
            foreach ($field_values as $field_value) {
              if (isset($field_value['value'])) {

                // If we have a value, and if it's not public. We better remove
                // our add to any block. We can't share any non public pages
                // anyway.
                return AccessResult::forbiddenIf($field_value['value'] != 'public')
                  ->addCacheableDependency($block);
              }
            }
          }
        }
      }
    }
  }

  // No opinion for other situations really.
  return AccessResult::neutral();
}