social_sharing.module in Open Social 8.2
Same filename and directory in other branches
- 8.9 modules/social_features/social_sharing/social_sharing.module
- 8 modules/social_features/social_sharing/social_sharing.module
- 8.3 modules/social_features/social_sharing/social_sharing.module
- 8.4 modules/social_features/social_sharing/social_sharing.module
- 8.5 modules/social_features/social_sharing/social_sharing.module
- 8.6 modules/social_features/social_sharing/social_sharing.module
- 8.7 modules/social_features/social_sharing/social_sharing.module
- 8.8 modules/social_features/social_sharing/social_sharing.module
- 10.3.x modules/social_features/social_sharing/social_sharing.module
- 10.0.x modules/social_features/social_sharing/social_sharing.module
- 10.1.x modules/social_features/social_sharing/social_sharing.module
- 10.2.x modules/social_features/social_sharing/social_sharing.module
The Social Sharing module.
File
modules/social_features/social_sharing/social_sharing.moduleView source
<?php
/**
* @file
* The Social Sharing module.
*/
use Drupal\node\Entity\Node;
use Drupal\block\Entity\Block;
use Drupal\Core\Session\AccountInterface;
use Drupal\Core\Access\AccessResult;
/**
* Implements hook_block_access().
*/
function social_sharing_block_access(Block $block, $operation, AccountInterface $account) {
if ($operation == 'view' && $block
->getPluginId() == 'addtoany_block') {
// Exclude Social Sharing block form Add and Edit node pages.
$route_name = \Drupal::routeMatch()
->getRouteName();
$excluded_routes = [
'node.add',
'entity.node.edit_form',
];
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();
}
Functions
Name | Description |
---|---|
social_sharing_block_access | Implements hook_block_access(). |