generated from symbioquine/farmOS_vue_page_demo
-
Notifications
You must be signed in to change notification settings - Fork 2
/
farmos_asset_link.module
75 lines (54 loc) · 2.2 KB
/
farmos_asset_link.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
<?php
use Drupal\Core\Access\AccessResult;
use Drupal\Core\Routing\RouteObjectInterface;
use Drupal\jsonapi\ResourceType\ResourceType;
/**
* Implements hook_page_attachments().
*/
function farmos_asset_link_page_attachments(array &$attachments) {
// TODO: special handling for not-yet-logged-in users
$storage = \Drupal::entityTypeManager()->getStorage('asset_link_default_plugin');
$ids = \Drupal::entityQuery('asset_link_default_plugin')
->condition('sidebarUrlPattern', '', '<>')->execute();
$defaultPluginConfigs = $storage->loadMultiple($ids);
$sidebarUrlPatterns = [];
foreach($defaultPluginConfigs as $defaultPluginConfig) {
if (!$defaultPluginConfig->status()) {
continue;
}
$sidebarUrlPatterns[] = $defaultPluginConfig->sidebarUrlPattern();
}
$attachments['#attached']['library'][] = 'farmos_asset_link/farmos_asset_link_sidecar';
$attachments['#attached']['drupalSettings']['farmos_asset_link'] = [
'sidebar_url_patterns' => $sidebarUrlPatterns,
];
}
/**
* Implements hook_entity_create_access().
*
* Grant JSON:API file upload permissions for entity fields based on
* bundle-level permissions. e.g. Allow image uploads for animal assets if the
* user has 'create animal asset' permissions.
*
* TODO: Follow up on https://github.com/farmOS/farmOS/pull/563#issuecomment-1241952618
*/
function farmos_asset_link_entity_create_access($account, $context, $entity_bundle) {
$route_match = \Drupal::routeMatch();
if (empty($route_match)) {
return AccessResult::neutral();
}
$resource_type = $route_match->getParameter("resource_type");
if (!($resource_type instanceof ResourceType)) {
return AccessResult::neutral();
}
$entity_type_id = $resource_type->getEntityTypeId();
$entity_bundle = $resource_type->getBundle();
if (empty($entity_type_id) || empty($entity_bundle)) {
return AccessResult::neutral();
}
$route = $route_match->getRouteObject();
if (empty($route) || $route->getDefault(RouteObjectInterface::CONTROLLER_NAME) != 'jsonapi.file_upload:handleFileUploadForNewResource') {
return AccessResult::neutral();
}
return AccessResult::allowedIfHasPermission($account, "create " . $entity_bundle . " " . $entity_type_id);
}