Skip to content

Commit 6ebeed0

Browse files
committed
When language service is not being called make sure to use the route object
1 parent 6fa5181 commit 6ebeed0

File tree

1 file changed

+15
-4
lines changed

1 file changed

+15
-4
lines changed

modules/custom/social_language/src/Access.php

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
use Drupal\Core\Routing\Access\AccessInterface;
88
use Drupal\Core\Routing\RouteMatchInterface;
99
use Drupal\Core\Session\AccountInterface;
10+
use Symfony\Component\Routing\Route;
1011

1112
/**
1213
* Class Access.
@@ -49,12 +50,22 @@ public function __construct(LanguageManagerInterface $language_manager) {
4950
*/
5051
public function access(RouteMatchInterface $route_match, AccountInterface $account, $permission = NULL) {
5152
if (count($this->languageManager->getLanguages()) > 1) {
52-
if (!empty($permission)) {
53-
return AccessResult::allowedIfHasPermission($account, $permission);
53+
// If the permission is null the service was not called,
54+
// and we should get it from the route object.
55+
if (is_null($permission)) {
56+
// Get route object.
57+
$route = $route_match->getRouteObject();
58+
assert($route instanceof Route);
59+
60+
// Get defined permission on _social_language_access.
61+
$permission = $route->getRequirements()['_social_language_access'];
5462
}
55-
else {
56-
return AccessResult::allowed();
63+
64+
if (is_string($permission)) {
65+
return AccessResult::allowedIfHasPermission($account, $permission);
5766
}
67+
68+
return AccessResult::neutral();
5869
}
5970

6071
return AccessResult::forbidden();

0 commit comments

Comments
 (0)