diff --git a/modules/gnode/gnode.module b/modules/gnode/gnode.module index 0efb6b1..ec79abc 100644 --- a/modules/gnode/gnode.module +++ b/modules/gnode/gnode.module @@ -6,7 +6,10 @@ */ use Drupal\Core\Access\AccessResult; +use Drupal\Core\Entity\EntityInterface; use Drupal\Core\Session\AccountInterface; +use Drupal\group\Entity\Group; +use Drupal\group\Entity\GroupContent; use Drupal\group\Entity\GroupContentType; use Drupal\node\NodeInterface; use Drupal\node\NodeTypeInterface; @@ -299,3 +302,34 @@ function gnode_node_access_records(NodeInterface $node) { return $records; } + +/** + * Implements hook_entity_operation_alter(). + */ +function gnode_entity_operation_alter(array &$operations, EntityInterface $entity) { + if (isset($operations['translate']) && $entity instanceof NodeInterface) { + $can_translate = TRUE; + $group_content_array = GroupContent::loadByEntity($entity); + $group_ids = []; + foreach ($group_content_array as $group_content) { + $group_ids[] = $group_content->gid->target_id; + } + if (!empty($group_ids)) { + $can_translate = FALSE; + $account = Drupal::currentUser(); + $groups = Group::loadMultiple($group_ids); + $plugin_id = 'group_node:' . $entity->bundle(); + /** @var \Drupal\group\Entity\Group[] $groups */ + foreach ($groups as $group) { + if ($group->hasPermission("translate $plugin_id entity", $account)) { + $can_translate = TRUE; + break; + } + } + } + if (!$can_translate) { + unset($operations['translate']); + } + + } +} diff --git a/modules/gnode/gnode.services.yml b/modules/gnode/gnode.services.yml new file mode 100644 index 0000000..1e22fe7 --- /dev/null +++ b/modules/gnode/gnode.services.yml @@ -0,0 +1,10 @@ +services: + gnode.translation_overview_access: + class: Drupal\gnode\Access\GroupContentTranslationAccess + tags: + - { name: access_check, applies_to: _access_content_translation_overview } + + gnode.translation_manage_access: + class: Drupal\gnode\Access\GroupContentTranslationAccess + tags: + - { name: access_check, applies_to: _access_content_translation_manage } diff --git a/modules/gnode/src/Access/GroupContentTranslationAccess.php b/modules/gnode/src/Access/GroupContentTranslationAccess.php new file mode 100644 index 0000000..9f87857 --- /dev/null +++ b/modules/gnode/src/Access/GroupContentTranslationAccess.php @@ -0,0 +1,59 @@ +getParameter($entity_type_id); + if ($entity_type_id === 'node' && $node) { + // Check if it this is group content. + $group_content_array = GroupContent::loadByEntity($node); + $group_ids = []; + foreach ($group_content_array as $group_content) { + $group_ids[] = $group_content->gid->target_id; + } + if (!empty($group_ids)) { + // This is group content. Check for the group permission to translate. + $groups = Group::loadMultiple($group_ids); + $plugin_id = 'group_node:' . $node->bundle(); + /** @var \Drupal\group\Entity\Group[] $groups */ + foreach ($groups as $group) { + if ($group->hasPermission("translate $plugin_id entity", $account) && $node->access('update', $account)) { + return AccessResult::allowed(); + } + } + + return AccessResult::forbidden('You are not allowed to translate group content'); + } + } + + // There is no entity available or the entity is not part of any group. + return AccessResult::allowed(); + } + +} diff --git a/modules/gnode/src/Plugin/GroupContentEnabler/GroupNode.php b/modules/gnode/src/Plugin/GroupContentEnabler/GroupNode.php index 033f953..5e3e2b8 100644 --- a/modules/gnode/src/Plugin/GroupContentEnabler/GroupNode.php +++ b/modules/gnode/src/Plugin/GroupContentEnabler/GroupNode.php @@ -67,6 +67,9 @@ class GroupNode extends GroupContentEnablerBase { $permissions["view unpublished $plugin_id entity"] = [ 'title' => str_replace('View ', 'View unpublished ', $original['title']), ] + $original; + $permissions["translate $plugin_id entity"] = [ + 'title' => str_replace('View ', 'Translate ', $original['title']), + ] + $original; return $permissions; }