-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathApiControllerPirateExample.php
More file actions
166 lines (136 loc) · 3.78 KB
/
ApiControllerPirateExample.php
File metadata and controls
166 lines (136 loc) · 3.78 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
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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
<?php
namespace Drupal\bc_api_example\Controller;
use Drupal\bc_api_base\Controller\ApiControllerBase;
/**
* Example API Controller Class.
*
* @ApiDoc(
* params = {
* @ApiParam(
* name = "status",
* type = "bool",
* description = "Filter on node status.",
* default = "TRUE",
* ),
* @ApiParam(
* name = "nat",
* type = "int",
* description = "Filter on nationality. This should use the taxonomy id of the nationality.",
* ),
* }
* )
*/
class ApiControllerPirateExample extends ApiControllerBase {
/**
* {@inheritdoc}
*/
public function getApiCacheTime($id = NULL) {
// Parent method looks at settings form and takes into account list or
// detail.
return 0;
}
/**
* {@inheritdoc}
*/
public function getCacheId() {
// This should be a unique string, characters only.
$cid = "pirates";
if (!empty($this->params)) {
$cid .= ":" . implode(":", $this->params);
}
$cid .= ":page-" . $this->page;
$cid .= ":limit-" . $this->limit;
return $cid;
}
/**
* {@inheritdoc}
*/
public function initCacheTags() {
$this->cacheTags = [
'myAwesomeCoolCacheTag',
];
}
/**
* {@inheritdoc}
*/
public function setParams() {
// It would be good to always run this.
parent::setParams();
$this->privateParams['status'] = 1;
// Here we also want to handle any bad param errors...
}
/**
* {@inheritdoc}
*/
public function getDefaultPlatform() {
return "cinder";
}
/**
* {@inheritdoc}
*/
public function getResourceQueryResult() {
// Just having this here as an example.
// Most times no need to override this method.
parent::getResourceQueryResult();
}
/**
* {@inheritdoc}
*/
public function getResourceListQueryResult() {
// This method should be overridden for any endpoint.
$query = $this->entityTypeManager->getStorage('node')->getQuery();
$query->accessCheck(TRUE);
$query->condition('status', $this->privateParams['status']);
$query->condition('type', 'pirate');
$count_query = clone $query;
$query->range(($this->page * $this->limit), $this->limit);
$entity_ids = $query->execute();
// Must set total result count so we can properly page.
$this->resultTotal = (int) $count_query->count()->execute();
// Process Items.
$nodes = $this->entityTypeManager->getStorage('node')->loadMultiple($entity_ids);
$this->rawData = $nodes;
}
/**
* {@inheritdoc}
*/
public function buildAllResourceData() {
$data = [];
foreach ($this->rawData as $node) {
$this->cacheTags = array_merge($this->cacheTags, $node->getCacheTags());
$created_changed = $this->transformer->createdChangedFieldVals($node);
$item = [
'nid' => (int) $node->id(),
'cms_title' => $node->label(),
'created' => $created_changed[0],
'updated' => $created_changed[1],
];
$data[] = $item;
}
$this->data = $data;
}
/**
* {@inheritdoc}
*/
public function buildLinks() {
// This method should be overridden for any endpoint.
$base_url = $this->request->getSchemeAndHttpHost() . $this->request->getPathInfo();
$tmp_query_params = $this->params;
$tmp_query_params['platform'] = $this->platform;
$tmp_query_params['limit'] = $this->limit;
if ($this->page == 0) {
$this->prev = "";
}
else {
$tmp_query_params['page'] = $this->page - 1;
$this->prev = $base_url . "?" . http_build_query($tmp_query_params);
}
if ($this->resultTotal > (($this->page + 1) * $this->limit)) {
$tmp_query_params['page'] = $this->page + 1;
$this->next = $base_url . "?" . http_build_query($tmp_query_params);
}
else {
$this->next = "";
}
}
}