diff --git a/src/Plugin/Group/RelationHandlerDefault/OperationProvider.php b/src/Plugin/Group/RelationHandlerDefault/OperationProvider.php index 6d9c66d2d1e373fc36a0103bd5d7a723ad8ae749..5de37c9b9250ccaa1176937f37a58f81eb1067b1 100644 --- a/src/Plugin/Group/RelationHandlerDefault/OperationProvider.php +++ b/src/Plugin/Group/RelationHandlerDefault/OperationProvider.php @@ -7,6 +7,7 @@ use Drupal\Core\Extension\ModuleHandlerInterface; use Drupal\Core\Session\AccountProxyInterface; use Drupal\Core\StringTranslation\TranslationInterface; use Drupal\Core\Url; +use Drupal\field_ui\Hook\FieldUiHooks; use Drupal\group\Entity\GroupInterface; use Drupal\group\Entity\GroupTypeInterface; use Drupal\group\Plugin\Group\Relation\GroupRelationTypeManagerInterface; @@ -71,11 +72,19 @@ class OperationProvider implements OperationProviderInterface { ]; } - // This could be in its own decorator, but then it would live in a module - // of its own purely for field_ui support. So let's keep it here. + // Use a safer alternative to field_ui_entity_operation() if ($this->moduleHandler->moduleExists('field_ui')) { $relationship_type = $this->entityTypeManager()->getStorage('group_relationship_type')->load($relationship_type_id); - $operations += field_ui_entity_operation($relationship_type); + if ($relationship_type) { + // Try using the right method for adding the Field UI entity operations. + $operations += match (TRUE) { + // Drupal version >= 11.1.x. + method_exists(FieldUiHooks::class, 'entityOperation') => (new FieldUiHooks())->entityOperation($relationship_type), + // Drupal version < 11.1.x. + function_exists('field_ui_entity_operation') => field_ui_entity_operation($relationship_type), + default => [], + }; + } } } elseif ($ui_allowed) { diff --git a/tests/src/Functional/EntityOperationsTest.php b/tests/src/Functional/EntityOperationsTest.php index ce73834a95d4d7eaaae19240f9e45af3e62fe047..6028ee7b155b94a20c6734e4a2e44e641a06aa50 100644 --- a/tests/src/Functional/EntityOperationsTest.php +++ b/tests/src/Functional/EntityOperationsTest.php @@ -2,6 +2,7 @@ namespace Drupal\Tests\group\Functional; +use Drupal\group\Entity\GroupRole; use Drupal\group\PermissionScopeInterface; use Drupal\user\RoleInterface; @@ -37,28 +38,41 @@ class EntityOperationsTest extends GroupBrowserTestBase { * @dataProvider provideEntityOperationScenarios */ public function testEntityOperations($visible, $invisible, $permissions = [], $modules = []) { - $group = $this->createGroup(['type' => $this->createGroupType()->id()]); + $group_type = $this->createGroupType(); + $group = $this->createGroup(['type' => $group_type->id()]); + // Assign permissions if provided. if (!empty($permissions)) { - $this->createGroupRole([ + $role = GroupRole::create([ 'group_type' => $group->bundle(), - 'scope' => PermissionScopeInterface::INSIDER_ID, - 'global_role' => RoleInterface::AUTHENTICATED_ID, + 'id' => 'custom-role', + 'label' => 'Custom Role', 'permissions' => $permissions, ]); + $role->save(); } + // Install required modules. if (!empty($modules)) { - $this->container->get('module_installer')->install($modules, TRUE); + $module_handler = \Drupal::service('module_handler'); + $module_installer = \Drupal::service('module_installer'); + foreach ($modules as $module) { + if (!$module_handler->moduleExists($module)) { + $module_installer->install([$module], TRUE); + } + } } - $this->drupalGet('admin/group'); + // Navigate to the group administration page. + $this->drupalGet('/admin/structure/group'); + // Verify visible links. foreach ($visible as $path => $label) { $this->assertSession()->linkExists($label); $this->assertSession()->linkByHrefExists($path); } + // Verify invisible links. foreach ($invisible as $path => $label) { $this->assertSession()->linkNotExists($label); $this->assertSession()->linkByHrefNotExists($path); @@ -72,21 +86,21 @@ class EntityOperationsTest extends GroupBrowserTestBase { $scenarios['withoutAccess'] = [ [], [ - 'group/1/edit' => 'Edit', - 'group/1/members' => 'Members', - 'group/1/delete' => 'Delete', - 'group/1/revisions' => 'Revisions', + '/group/1/edit' => 'Edit', + '/group/1/members' => 'Members', + '/group/1/delete' => 'Delete', + '/group/1/revisions' => 'Revisions', ], ]; $scenarios['withAccess'] = [ [ - 'group/1/edit' => 'Edit', - 'group/1/delete' => 'Delete', - 'group/1/revisions' => 'Revisions', + '/group/1/edit' => 'Edit', + '/group/1/delete' => 'Delete', + '/group/1/revisions' => 'Revisions', ], [ - 'group/1/members' => 'Members', + '/group/1/members' => 'Members', ], [ 'view group', @@ -99,10 +113,10 @@ class EntityOperationsTest extends GroupBrowserTestBase { $scenarios['withAccessAndViews'] = [ [ - 'group/1/edit' => 'Edit', - 'group/1/members' => 'Members', - 'group/1/delete' => 'Delete', - 'group/1/revisions' => 'Revisions', + '/group/1/edit' => 'Edit', + '/group/1/members' => 'Members', + '/group/1/delete' => 'Delete', + '/group/1/revisions' => 'Revisions', ], [], [ @@ -117,5 +131,5 @@ class EntityOperationsTest extends GroupBrowserTestBase { return $scenarios; } - + }