diff --git a/quick_node_clone.module b/quick_node_clone.module index 2a5576b..64c59f9 100644 --- a/quick_node_clone.module +++ b/quick_node_clone.module @@ -10,7 +10,7 @@ use Drupal\Core\Entity\ContentEntityInterface; use Drupal\Core\Entity\EntityInterface; use Drupal\Core\Url; use Drupal\Core\Routing\RouteMatchInterface; -use Drupal\group\Entity\GroupContent; +use Drupal\quick_node_clone\ManageVersionGroup; /** * Implements hook_entity_type_build(). @@ -84,19 +84,38 @@ function _quick_node_clone_has_clone_permission(ContentEntityInterface $node) { $bundle = $node->bundle(); if ($current_user->hasPermission("clone $bundle content")) { if (\Drupal::moduleHandler()->moduleExists('gnode')) { - $group_contents = GroupContent::loadByEntity($node); - foreach ($group_contents as $group_content) { - // We check via createEntityAccess() for "create group entity", not via - // createAccess() for "relate existing entity to group", as we do in - // fact create a new entity. - // @see \Drupal\group\Access\GroupContentCreateAnyEntityAccessCheck::access - // @todo Use group access control handler when we can rely on group 1.0. - $access = $group_content->getContentPlugin() - ->createEntityAccess($group_content->getGroup(), $current_user); - if ($access->isAllowed()) { - return TRUE; + + // Classes and methods for group 1.0 and 2.0 are different. + $class_group_relationship = ManageVersionGroup::groupContentOrGroupRelationhipClass(); + + if (ManageVersionGroup::getVersion() === 2) { + $group_relationships = $class_group_relationship::loadByEntity($node); + $group_relationships_manager = \Drupal::service('group_relation_type.manager'); + foreach ($group_relationships as $group_relationship) { + $plugin_id = $group_relationship->getPlugin()->getPluginId(); + $access_control_handler = $group_relationships_manager->getAccessControlHandler($plugin_id); + $access = $access_control_handler->relationshipCreateAccess($group_relationship->getGroup(), $current_user, TRUE); + if ($access->isAllowed()) { + return TRUE; + } + } + } + else { + $group_contents = $class_group_relationship::loadByEntity($node); + foreach ($group_contents as $group_content) { + // We check via createEntityAccess() for "create group entity", not via + // createAccess() for "relate existing entity to group", as we do in + // fact create a new entity. + // @see \Drupal\group\Access\GroupRelationshipCreateAccessCheck::access + // @todo Use group access control handler when we can rely on group 1.0. + $access = $group_content->getContentPlugin() + ->createEntityAccess($group_content->getGroup(), $current_user); + if ($access->isAllowed()) { + return TRUE; + } } } + } // Only check global access if we there is no group module enabled, or diff --git a/src/Entity/QuickNodeCloneEntityFormBuilder.php b/src/Entity/QuickNodeCloneEntityFormBuilder.php index b917a55..1720ec1 100755 --- a/src/Entity/QuickNodeCloneEntityFormBuilder.php +++ b/src/Entity/QuickNodeCloneEntityFormBuilder.php @@ -11,12 +11,12 @@ use Drupal\Core\Extension\ModuleHandlerInterface; use Drupal\Core\Form\FormBuilderInterface; use Drupal\Core\Form\FormState; use Drupal\Core\Session\AccountInterface; -use Drupal\group\Entity\GroupContent; use Drupal\node\Entity\Node; use Symfony\Component\DependencyInjection\ContainerInterface; use Drupal\Core\TempStore\PrivateTempStoreFactory; use Drupal\Core\StringTranslation\StringTranslationTrait; use Drupal\Core\StringTranslation\TranslationInterface; +use Drupal\quick_node_clone\ManageVersionGroup; /** * Builds entity forms. @@ -135,12 +135,15 @@ class QuickNodeCloneEntityFormBuilder extends EntityFormBuilder { // Get and store groups of original entity, if any. $groups = []; if ($this->moduleHandler->moduleExists('gnode')) { + // Classes and methods for group 1.0 and 2.0 are different. + $class_group_relationship = ManageVersionGroup::groupContentOrGroupRelationhipClass(); /** @var \Drupal\Core\Entity\ContentEntityInterface $original_entity */ - $group_contents = GroupContent::loadByEntity($original_entity); + $group_contents = $class_group_relationship::loadByEntity($original_entity); foreach ($group_contents as $group_content) { $groups[] = $group_content->getGroup(); } } + $form_state_additions['quick_node_clone_groups_storage'] = $groups; // Get default status value of node bundle. diff --git a/src/Form/QuickNodeCloneNodeForm.php b/src/Form/QuickNodeCloneNodeForm.php index bd84d39..7edb981 100644 --- a/src/Form/QuickNodeCloneNodeForm.php +++ b/src/Form/QuickNodeCloneNodeForm.php @@ -4,6 +4,7 @@ namespace Drupal\quick_node_clone\Form; use Drupal\Core\Form\FormStateInterface; use Drupal\node\NodeForm; +use Drupal\quick_node_clone\ManageVersionGroup; /** * Form controller for Quick Node Clone edit forms. @@ -60,11 +61,18 @@ class QuickNodeCloneNodeForm extends NodeForm { $form_state->setValue('nid', $node->id()); $form_state->set('nid', $node->id()); $storage = $form_state->getStorage(); + + // Some classes and methods for group 1.0 and 2.0 are different. + $add_method = 'addContent'; + if (ManageVersionGroup::getVersion() === 2) { + $add_method = 'addRelationship'; + } + foreach ($storage['quick_node_clone_groups_storage'] as $group) { // Add node to all the groups the original was in // (if group and gnode modules aren't installed then nothing should ever // be set in this array anyway) - $group->addContent($node, "group_node:" . $node->bundle()); + $group->$add_method($node, "group_node:" . $node->bundle()); } if ($node->access('view')) { $form_state->setRedirect( diff --git a/src/ManageVersionGroup.php b/src/ManageVersionGroup.php new file mode 100644 index 0000000..73aba49 --- /dev/null +++ b/src/ManageVersionGroup.php @@ -0,0 +1,80 @@ +getAllInstalledInfo()['group'] ?? NULL; + } + return self::$infoGroup; + + } + + /** + * Get version of group module. + * + * @return int|NULL + * Version of group module. + */ + static function getVersion() { + if (!isset(self::$version)) { + self::$version = 1; + $infoGroup = self::getInfoGroup(); + if ($infoGroup === NULL) { + return self::$version; + } + if (isset($infoGroup['version']) && (int)$infoGroup['version'] >= 2) { + self::$version = 2; + } + } + return self::$version; + + } + + /** + * Require group content or group relationship. + * + * @return string + * TRUE if require is successful, FALSE if file not found, NULL if group module is not installed. + */ + static function groupContentOrGroupRelationhipClass() { + $versionClassMap = [ + 1 => '\Drupal\group\Entity\GroupContent', + 2 => '\Drupal\group\Entity\GroupRelationship', + ]; + $version = self::getVersion() ?? 1; + return $versionClassMap[$version]; + + } + + +} \ No newline at end of file