-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathdruxt.module
136 lines (119 loc) · 3.7 KB
/
druxt.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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
<?php
/**
* @file
* Drupal module hooks and shared functionality.
*/
use Drupal\Core\Access\AccessResult;
use Drupal\Core\Entity\Entity\EntityViewDisplay;
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Entity\EntityTypeInterface;
use Drupal\Core\Session\AccountInterface;
use Drupal\jsonapi\Routing\Routes;
/**
* Checks for Druxt route access.
*/
function druxt_access_check(AccountInterface $account) {
$route = \Drupal::routeMatch()->getRouteObject();
if (!$route) {
return FALSE;
}
$defaults = $route->getDefaults();
// Is the route a JSON API request?
if (!Routes::isJsonApiRequest($defaults)) {
return FALSE;
}
// Is the JSON API resource a Druxt resource?
$resources = [
'block--block',
'entity_form_display--entity_form_display',
'entity_form_mode--entity_form_mode',
'entity_view_display--entity_view_display',
'entity_view_mode--entity_view_mode',
'field_config--field_config',
'field_storage_config--field_storage_config',
'menu--menu',
'menu_link_content--menu_link_content',
'view--view',
];
if (!in_array($defaults['resource_type'], $resources)) {
return FALSE;
}
// Is the route a GET request?
if (!in_array('GET', $route->getMethods())) {
return FALSE;
}
// Does the user have access?
if (!$account->hasPermission('access druxt resources')) {
return FALSE;
}
return TRUE;
}
/**
* Implements hook_entity_access().
*/
function druxt_entity_access(EntityInterface $entity, $operation, AccountInterface $account) {
return druxt_access_check($account) ? AccessResult::allowed() : AccessResult::neutral();
}
/**
* Implements hook_entity_bundle_create().
*/
function druxt_entity_bundle_create($entity_type_id, $bundle) {
druxt_ensure_entity_view_display($entity_type_id, $bundle);
}
/**
* Implements hook_jsonapi_entity_filter_access().
*/
function druxt_jsonapi_entity_filter_access(EntityTypeInterface $entity_type, AccountInterface $account) {
if (!druxt_access_check($account)) {
return;
}
return ([
JSONAPI_FILTER_AMONG_ALL => AccessResult::allowed(),
]);
}
/**
* Implments hook_condition_info_alter().
*/
function druxt_condition_info_alter(array &$info) {
// Use custom 'request_path' plugin to allow setting of the path from DruxtJS
// frontend.
$info['request_path']['class'] = 'Drupal\druxt\Plugin\Condition\DruxtRequestPath';
$info['request_path']['provider'] = 'druxt';
}
/**
* Ensure that all required EntityViewDisplays are available.
*/
function druxt_ensure_entity_view_displays() {
$bundle_info = \Drupal::service('entity_type.bundle.info');
$display_repository = \Drupal::service('entity_display.repository');
$view_modes = $display_repository->getAllViewModes();
foreach (array_keys($view_modes) as $entity_type_id) {
$bundles = $bundle_info->getBundleInfo($entity_type_id);
foreach (array_keys($bundles) as $bundle) {
$displays = $display_repository->getViewModeOptionsByBundle($entity_type_id, $bundle);
if (empty($displays)) {
$displays = ['default' => TRUE];
}
foreach (array_keys($displays) as $mode) {
druxt_ensure_entity_view_display($entity_type_id, $bundle, $mode);
}
}
}
}
/**
* Ensure that a specified EntityViewDisplay is present, or create it.
*/
function druxt_ensure_entity_view_display($entity_type_id, $bundle, $mode = 'default') {
$result = \Drupal::entityQuery('entity_view_display')
->condition('id', "{$entity_type_id}.{$bundle}.{$mode}")
->execute();
if (empty($result)) {
// Create missing configuration entities.
EntityViewDisplay::create([
'targetEntityType' => $entity_type_id,
'bundle' => $bundle,
'mode' => $mode,
'status' => TRUE,
])->save();
}
}