This repository has been archived by the owner on Sep 3, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
termstatus.module
73 lines (61 loc) · 2.12 KB
/
termstatus.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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
<?php
/**
* @file
* Contains general functions and hooks used for term status.
*/
use Drupal\Core\Access\AccessResult;
use Drupal\Core\Access\AccessResultReasonInterface;
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Session\AccountInterface;
use Drupal\termstatus\Entity\TermWithStatus;
use Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException;
/**
* Helper function to get all vocabularies.
*
* @return \Drupal\Core\Entity\EntityInterface[]
*/
function termstatus_get_vocabularies() {
return \Drupal::entityTypeManager()
->getStorage('taxonomy_vocabulary')
->loadMultiple();
}
/**
* Implements hook_entity_type_build().
*
* @param array $entity_types
*/
function termstatus_entity_type_build(array &$entity_types) {
if (!empty($entity_types['taxonomy_term'])) {
$entity_types['taxonomy_term']->setClass('\Drupal\termstatus\Entity\TermWithStatus');
}
}
/**
* Implements hook_entity_access().
*
* @param \Drupal\Core\Entity\EntityInterface $entity
* @param $operation
* @param \Drupal\Core\Session\AccountInterface $account
*
* @return \Drupal\Core\Access\AccessResult|\Drupal\Core\Access\AccessResultNeutral
*/
function termstatus_entity_access(EntityInterface $entity, $operation, AccountInterface $account) {
if ($entity instanceof TermWithStatus) {
$published = $entity->isPublished();
$vid = $entity->bundle();
if ($published) {
$permission = 'view published terms in ' . $vid;
$access_result = AccessResult::forbiddenIf(!$account->hasPermission($permission));
if ($access_result->isForbidden()) {
throw new AccessDeniedHttpException($access_result instanceof AccessResultReasonInterface ? $access_result->getReason() : NULL);
}
}
else {
$permission = 'view unpublished terms in ' . $vid;
$access_result = AccessResult::forbiddenIf(!$account->hasPermission($permission));
if ($access_result->isForbidden()) {
throw new AccessDeniedHttpException($access_result instanceof AccessResultReasonInterface ? $access_result->getReason() : NULL);
}
}
return AccessResult::neutral();
}
}