-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathlanguage_access.module
34 lines (30 loc) · 948 Bytes
/
language_access.module
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
<?php
use Drupal\Core\Access\AccessResult;
use Drupal\Core\Session\AccountInterface;
use Drupal\node\NodeInterface;
/**
* Implements hook_language_switch_links_alter()
*/
function language_access_language_switch_links_alter(&$links, $type, $path) {
$user = \Drupal::currentUser();
$languages = Drupal::languageManager()->getLanguages();
foreach ($languages as $language) {
$id = $language->getId();
if (!$user->hasPermission('access language ' . $id)) {
if (isset($links[$id])) unset($links[$id]);
}
}
}
/**
* Implements hook_node_access().
*/
function language_access_node_access(NodeInterface $node, $op, AccountInterface $account) {
if ($op == 'view') {
/** @var Drupal\Core\Language\Language $language */
$language = $node->language();
if (!$account->hasPermission('access language ' . $language->getId())) {
return AccessResult::forbidden();
}
}
return AccessResult::neutral();
}