-
Notifications
You must be signed in to change notification settings - Fork 30
Expand file tree
/
Copy pathjsonapi_assessment.module
More file actions
26 lines (23 loc) · 940 Bytes
/
jsonapi_assessment.module
File metadata and controls
26 lines (23 loc) · 940 Bytes
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
<?php
use Symfony\Component\HttpFoundation\Response;
/**
* Alters the JSON:API response.
*
* @param array $jsonapi_response
* The decoded JSON data to be altered.
* @param \Symfony\Component\HttpFoundation\Response $response
* The response.
*/
function jsonapi_assessment_jsonapi_response_alter(array &$jsonapi_response, Response $response) {
// loop data to alter
if (isset($jsonapi_response['data']) && is_Array($jsonapi_response['data'])) {
$entity_manager = \Drupal::entityTypeManager();
$access_handler = $entity_manager->getAccessControlHandler('node');
$access = $access_handler->createAccess('article');
foreach ($jsonapi_response['data'] as $key => $item) {
if ($item['type'] == 'articles') {
$jsonapi_response['data'][$key]['attributes']['editable'] = $access; // append attributes to display edit link in front end.
}
}
}
}