This repository was archived by the owner on Sep 25, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathtide_api.module
49 lines (44 loc) · 1.52 KB
/
tide_api.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
<?php
/**
* @file
* Tide API module functionality.
*/
use Drupal\Core\Access\AccessResult;
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Entity\EntityTypeInterface;
use Drupal\Core\Session\AccountInterface;
use Drupal\jsonapi\Routing\Routes;
/**
* Implements hook_entity_access().
*/
function tide_api_entity_access(EntityInterface $entity, $operation, AccountInterface $account) {
if ($entity->getEntityTypeId() == 'menu' || $entity->getEntityTypeId() == 'menu_link_content') {
if ($operation == 'view') {
if (Routes::isJsonApiRequest(\Drupal::request()->attributes->all())) {
return AccessResult::allowed()->addCacheableDependency($entity);
}
}
}
return AccessResult::neutral();
}
/**
* Implements hook_jsonapi_entity_filter_access().
*/
function tide_api_jsonapi_entity_filter_access(EntityTypeInterface $entity_type, AccountInterface $account) {
if (in_array($entity_type->id(), ['node', 'paragraph'])) {
return [
JSONAPI_FILTER_AMONG_PUBLISHED => AccessResult::allowedIfHasPermission($account, 'access content')->cachePerPermissions(),
];
}
elseif ($entity_type->id() == 'media') {
return [
JSONAPI_FILTER_AMONG_PUBLISHED => AccessResult::allowedIfHasPermission($account, 'view media')->cachePerPermissions(),
];
}
elseif (in_array($entity_type->id(), ['menu', 'menu_link_content'])) {
return [
JSONAPI_FILTER_AMONG_ALL => AccessResult::allowed()->cachePerPermissions(),
];
}
return [JSONAPI_FILTER_AMONG_ALL => AccessResult::neutral()];
}