-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjsonapi_views.module
More file actions
61 lines (54 loc) · 1.46 KB
/
jsonapi_views.module
File metadata and controls
61 lines (54 loc) · 1.46 KB
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
<?php
/**
* @file
* Core functions for the JSON:API Views module.
*/
use Drupal\Core\Link;
use Drupal\Core\Url;
/**
* Implements hook_views_preview_info_alter().
*/
function jsonapi_views_views_preview_info_alter(&$rows, $executable) {
// Build JSON:API View path for the current display.
$jsonapi_base_path = \Drupal::getContainer()->getParameter('jsonapi.base_path');
$path = implode([
$jsonapi_base_path,
'views',
$executable->id(),
$executable->current_display,
], '/');
// Build Exposed sort queries.
$query = [];
$input = $executable->getExposedInput();
if ($input['sort_by']) {
$query['views-sort[sort_by]'] = $input['sort_by'];
}
if ($input['sort_order']) {
$query['views-sort[sort_order]'] = $input['sort_order'];
}
// Build Exposed filter queries.
$display_handler = $executable->getDisplay();
foreach ($display_handler->view->filter as $filter) {
if ($filter->isExposed() && $input[$filter->field]) {
$query["views-filter[{$filter->field}]"] = $input[$filter->field];
}
}
// Build JSON:API View URL.
$url = Url::fromUri("internal:{$path}");
$url->setOptions(['query' => $query]);
// Build render array.
$rows['query'][] = [
[
'data' => [
'#prefix' => '<strong>',
'#markup' => t('JSON:API Views'),
'#suffix' => '</strong>',
],
],
[
'data' => [
'#markup' => Link::fromTextAndUrl($url->toString(), $url)->toString(),
],
],
];
}