diff --git a/core/modules/jsonapi/tests/src/Functional/TermTest.php b/core/modules/jsonapi/tests/src/Functional/TermTest.php index 8e7bf041c55dd4c195b6965b116459238fcff266..649237d997a78c613501ff80c6d416a5939ecb9c 100644 --- a/core/modules/jsonapi/tests/src/Functional/TermTest.php +++ b/core/modules/jsonapi/tests/src/Functional/TermTest.php @@ -69,7 +69,7 @@ class TermTest extends ResourceTestBase { protected function setUpAuthorization($method) { switch ($method) { case 'GET': - $this->grantPermissionsToTestedRole(['access content']); + $this->grantPermissionsToTestedRole(['access content', 'view vocabulary labels']); break; case 'POST': diff --git a/core/modules/taxonomy/src/VocabularyAccessControlHandler.php b/core/modules/taxonomy/src/VocabularyAccessControlHandler.php index 93fab47e5dc55e309a45cd9344a4891c0ef30750..9fb23124af9526aa37d74c00dd2c68f5d2ce7761 100644 --- a/core/modules/taxonomy/src/VocabularyAccessControlHandler.php +++ b/core/modules/taxonomy/src/VocabularyAccessControlHandler.php @@ -14,11 +14,23 @@ */ class VocabularyAccessControlHandler extends EntityAccessControlHandler { + /** + * {@inheritdoc} + */ + protected $viewLabelOperation = TRUE; + /** * {@inheritdoc} */ protected function checkAccess(EntityInterface $entity, $operation, AccountInterface $account) { switch ($operation) { + case 'view label': + return AccessResult::allowedIfHasPermissions($account, [ + 'view vocabulary labels', + 'access taxonomy overview', + 'administer taxonomy', + ], 'OR'); + case 'access taxonomy overview': case 'view': return AccessResult::allowedIfHasPermissions($account, ['access taxonomy overview', 'administer taxonomy'], 'OR'); diff --git a/core/modules/taxonomy/taxonomy.permissions.yml b/core/modules/taxonomy/taxonomy.permissions.yml index 8ec7ab626c7d9cabb6fd2d3ef0b358b7afda9beb..9ba35754546f9bf4bd589b03f5cf373db910425d 100644 --- a/core/modules/taxonomy/taxonomy.permissions.yml +++ b/core/modules/taxonomy/taxonomy.permissions.yml @@ -14,5 +14,8 @@ delete all taxonomy revisions: view all taxonomy revisions: title: 'View all term revisions' +view vocabulary labels: + title: 'View vocabulary labels' + permission_callbacks: - Drupal\taxonomy\TaxonomyPermissions::permissions diff --git a/core/modules/taxonomy/tests/src/Kernel/Views/TaxonomyFieldVidTest.php b/core/modules/taxonomy/tests/src/Kernel/Views/TaxonomyFieldVidTest.php index fbc253bde201a2d41f638cc4a2416bf12561feac..4bad95a92227b9dc43ebf47ef4d292546ee90d12 100644 --- a/core/modules/taxonomy/tests/src/Kernel/Views/TaxonomyFieldVidTest.php +++ b/core/modules/taxonomy/tests/src/Kernel/Views/TaxonomyFieldVidTest.php @@ -4,6 +4,7 @@ use Drupal\Core\Render\RenderContext; use Drupal\Tests\taxonomy\Traits\TaxonomyTestTrait; +use Drupal\Tests\user\Traits\UserCreationTrait; use Drupal\Tests\views\Kernel\ViewsKernelTestBase; use Drupal\user\Entity\User; use Drupal\views\Tests\ViewTestData; @@ -18,6 +19,7 @@ class TaxonomyFieldVidTest extends ViewsKernelTestBase { use TaxonomyTestTrait; + use UserCreationTrait; /** * Modules to enable. @@ -116,6 +118,34 @@ public function testViewsHandlerVidField() { $this->assertEquals($expected, $actual, 'Displayed vocabulary name should match that loaded from the term.'); $this->assertEquals('bbb', $vocabulary->id(), 'First result should be vocabulary "bbb", due to DESC sorting.'); + + // Test with user without 'view vocabulary labels' permission. + $this->setUpCurrentUser(); + $actual = $renderer->executeInRenderContext(new RenderContext(), function () use ($view) { + return $view->field['vid']->advancedRender($view->result[0]); + }); + $expected = ''; + $this->assertEquals($expected, $actual); + + // Test with user with 'view vocabulary labels' permissions. + $this->setUpCurrentUser([], ['view vocabulary labels']); + $actual = $renderer->executeInRenderContext(new RenderContext(), function () use ($view) { + return $view->field['vid']->advancedRender($view->result[0]); + }); + $expected = $vocabulary->label(); + $this->assertEquals($expected, $actual); + + // Test with user with 'administer taxonomy' and 'access taxonomy overview' + // permissions. Label should be displayed for either permission. + $this->setUpCurrentUser([], [ + 'administer taxonomy', + 'access taxonomy overview', + ]); + $actual = $renderer->executeInRenderContext(new RenderContext(), function () use ($view) { + return $view->field['vid']->advancedRender($view->result[0]); + }); + $expected = $vocabulary->label(); + $this->assertEquals($expected, $actual); } }