File tree Expand file tree Collapse file tree 3 files changed +52
-2
lines changed Expand file tree Collapse file tree 3 files changed +52
-2
lines changed Original file line number Diff line number Diff line change 1
1
<?php
2
2
/**
3
- * big module
3
+ * Headless allows you to use Backdrop CMS as an API.
4
4
*/
5
5
function headless_menu() {
6
6
$items = array();
@@ -35,7 +35,17 @@ function headless_menu() {
35
35
'page callback' => 'backdrop_get_form',
36
36
'page arguments' => array('headless_settings_form'),
37
37
'access arguments' => array('administer site configuration'),
38
- 'file' => 'headless.admin.inc',
38
+ 'file' => 'includes/headless.admin.inc',
39
+ );
40
+
41
+ // Router: allow API queries via string paths.
42
+ $items['api/router/%'] = array(
43
+ 'title' => 'Headless router',
44
+ 'description' => 'Allow queries via string like /api/my-node-title',
45
+ 'page callback' => 'headless_router',
46
+ 'page arguments' => array(2),
47
+ 'access callback' => TRUE,
48
+ 'file' => 'includes/headless.router.inc',
39
49
);
40
50
41
51
return $items;
File renamed without changes.
Original file line number Diff line number Diff line change
1
+ <?php
2
+ /**
3
+ * Router: allow API queries via strings in URL path.
4
+ * The router will try to resolve a string to an API asset.
5
+ */
6
+
7
+ /**
8
+ * Resolve a string path to an API asset.
9
+ *
10
+ * @param string $path
11
+ * The string to try to resolve to an API asset (usually a path).
12
+ */
13
+ function headless_router (string $ path ) {
14
+ $ path = filter_xss ($ path );
15
+ $ assets = [];
16
+ $ query = db_select ('url_alias ' , 'u ' )
17
+ ->fields ('u ' , ['source ' ])
18
+ ->condition ('alias ' , '% ' . db_like ($ path ) . '% ' , 'LIKE ' )
19
+ ->addTag ('node_access ' )
20
+ ->execute ();
21
+ foreach ($ query as $ i => $ asset ) {
22
+ $ assets [] = $ asset ;
23
+ }
24
+ $ source = $ assets [0 ]->source ;
25
+ $ source = explode ('/ ' , $ source );
26
+ if ($ source [0 ] == 'node ' ) {
27
+ $ node = node_load ($ source [1 ]);
28
+ $ type = $ node ->type ;
29
+ headless_type ($ type , $ source [1 ]);
30
+ }
31
+ elseif ($ source [0 ] == 'taxonomy ' ) {
32
+ $ term = taxonomy_term_load ($ source [2 ]);
33
+ headless_term_item ($ term ->vocabulary , $ source [2 ]);
34
+ }
35
+ else {
36
+ $ error = ['code ' => 404 ];
37
+ backdrop_json_output ($ error );
38
+ backdrop_exit ();
39
+ }
40
+ }
You can’t perform that action at this time.
0 commit comments