Skip to content

Commit c8f9464

Browse files
authored
Merge pull request #17 from backdrop-contrib/16/backdropcoder
2 parents cb761d3 + 3dae46a commit c8f9464

File tree

7 files changed

+75
-35
lines changed

7 files changed

+75
-35
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
# Ignore vendor
2+
vendor

.travis.yml

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
language: php
2+
dist: xenial
3+
php:
4+
- 7.2
5+
cache:
6+
directories:
7+
- $HOME/.composer/cache/files
8+
- $HOME/.composer/cache/repo
9+
services:
10+
- mysql
11+
addons:
12+
- mariadb: '10.0'
13+
before_install:
14+
- composer install
15+
16+
script:
17+
# Check code standards
18+
- vendor/bin/phpcs -n --standard=vendor/backdrop/coder/coder_sniffer/Backdrop --ignore="vendor/*,README.md" --extensions=install,module,php,inc,theme .
19+
20+
# Unit tests
21+

composer.json

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"require-dev": {
3+
"backdrop/coder": "^1.0"
4+
}
5+
}

headless.install

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,10 @@
55
* Remove config after uninstalling headless.
66
*/
77

8+
/**
9+
* Implements hook_uninstall().
10+
*/
811
function headless_uninstall() {
912
$config = config('headless.settings');
1013
$config->delete();
1114
}
12-

headless.module

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,12 @@
11
<?php
22
/**
3+
* @file
34
* Headless allows you to use Backdrop CMS as an API.
45
*/
6+
7+
/**
8+
* Implements hook_menu().
9+
*/
510
function headless_menu() {
611
$items = array();
712
// Return JSON for individual nodes.
@@ -88,7 +93,7 @@ function headless_term_item($vocab, $tid) {
8893

8994
/**
9095
* Page callback for views.
91-
*
96+
*
9297
* This is to make it backwards compatible
9398
*/
9499
function headless_views($view) {
@@ -148,16 +153,18 @@ function headless_paragraphs_item($type, $entity_id) {
148153
}
149154

150155
/**
151-
* Helper function
156+
* Helper function to add pager data to /api/v2/views/%/% endpoints.
152157
*
153-
* We mimic the core function to add some extra pager data to the
158+
* We mimic the core function to add some extra pager data to the
154159
* returned value.
155160
*/
156161
function _views_get_view_result($name, $display_id = NULL) {
157162
$args = func_get_args();
158-
array_shift($args); // remove $name
163+
// Remove $name.
164+
array_shift($args);
159165
if (count($args)) {
160-
array_shift($args); // remove $display_id
166+
// Remove $display_id.
167+
array_shift($args);
161168
}
162169

163170
$view = views_get_view($name);

includes/headless.admin.inc

Lines changed: 29 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -8,32 +8,34 @@
88
* Headless configuration form.
99
*/
1010
function headless_settings_form($form, &$form_state) {
11+
$help = '
12+
<div class="headless-settings__help">
13+
Use this form to select wich entities you would like to expose as json endpoints.
14+
</div>
15+
';
1116
$form['help'] = array(
12-
'#markup' => '<div class="headless-settings__help">
13-
Use this form to select wich entities you would like to expose
14-
as json endpoints.
15-
</div>',
17+
'#markup' => $help,
1618
);
1719
$form['nodes'] = array(
1820
'#type' => 'fieldset',
1921
'#title' => 'Node Types',
2022
'#collapsible' => TRUE,
2123
);
2224
// Get the node types.
23-
$nodeTypes = node_type_get_types();
24-
$nodeTypeOptions = [];
25-
foreach($nodeTypes as $key => $type) {
26-
$nodeTypeOptions[$key] = $key;
25+
$node_types = node_type_get_types();
26+
$node_type_options = [];
27+
foreach ($node_types as $key => $type) {
28+
$node_type_options[$key] = $key;
2729
}
2830
$form['nodes']['node-checkboxes'] = array(
2931
'#type' => 'checkboxes',
30-
'#options' => $nodeTypeOptions,
32+
'#options' => $node_type_options,
3133
'#default_value' => _headless_get_default_options('node'),
3234
);
33-
$vocabularyTypes = taxonomy_get_vocabularies();
34-
$vocabularyTypeOptions = [];
35-
foreach($vocabularyTypes as $key => $type) {
36-
$vocabularyTypeOptions[$key] = $key;
35+
$vocabulary_types = taxonomy_get_vocabularies();
36+
$vocabulary_type_options = [];
37+
foreach ($vocabulary_types as $key => $type) {
38+
$vocabulary_type_options[$key] = $key;
3739
}
3840
$form['vocabularies'] = array(
3941
'#type' => 'fieldset',
@@ -43,7 +45,7 @@ function headless_settings_form($form, &$form_state) {
4345
);
4446
$form['vocabularies']['vocabularies-checkboxes'] = array(
4547
'#type' => 'checkboxes',
46-
'#options' => $vocabularyTypeOptions,
48+
'#options' => $vocabulary_type_options,
4749
'#default_value' => _headless_get_default_options('vocabularies'),
4850
);
4951
$form['views'] = array(
@@ -52,17 +54,17 @@ function headless_settings_form($form, &$form_state) {
5254
'#collapsible' => TRUE,
5355
'#collapsed' => TRUE,
5456
);
55-
$myViews = views_get_views_as_options(TRUE, 'enabled', NULL, TRUE, TRUE);
57+
$my_views = views_get_views_as_options(TRUE, 'enabled', NULL, TRUE, TRUE);
5658
$form['views']['views-checkboxes'] = array(
5759
'#type' => 'checkboxes',
58-
'#options' => $myViews,
60+
'#options' => $my_views,
5961
'#default_value' => _headless_get_default_options('views'),
6062
);
6163
if (module_exists('paragraphs')) {
62-
$paragraphsTypes = paragraphs_bundle_load();
63-
$paragraphsTypeOptions = [];
64-
foreach($paragraphsTypes as $key => $type) {
65-
$paragraphsTypeOptions[$key] = $key;
64+
$paragraphs_types = paragraphs_bundle_load();
65+
$paragraphs_type_options = [];
66+
foreach ($paragraphs_types as $key => $type) {
67+
$paragraphs_type_options[$key] = $key;
6668
}
6769
$form['paragraphs'] = array(
6870
'#type' => 'fieldset',
@@ -72,7 +74,7 @@ function headless_settings_form($form, &$form_state) {
7274
);
7375
$form['paragraphs']['paragraphs-checkboxes'] = array(
7476
'#type' => 'checkboxes',
75-
'#options' => $paragraphsTypeOptions,
77+
'#options' => $paragraphs_type_options,
7678
'#default_value' => _headless_get_default_options('paragraphs'),
7779
);
7880
}
@@ -92,7 +94,7 @@ function headless_settings_form($form, &$form_state) {
9294
function headless_settings_form_submit($form, &$form_state) {
9395
// Remove unnecessary values.
9496
form_state_values_clean($form_state);
95-
foreach($form_state['input']['node-checkboxes'] as $type => $value) {
97+
foreach ($form_state['input']['node-checkboxes'] as $type => $value) {
9698
if ($value != NULL) {
9799
$val = 1;
98100
}
@@ -101,7 +103,7 @@ function headless_settings_form_submit($form, &$form_state) {
101103
}
102104
config_set('headless.settings', 'node.' . $type, $val);
103105
}
104-
foreach($form_state['input']['vocabularies-checkboxes'] as $type => $value) {
106+
foreach ($form_state['input']['vocabularies-checkboxes'] as $type => $value) {
105107
if ($value != NULL) {
106108
$val = 1;
107109
}
@@ -110,7 +112,7 @@ function headless_settings_form_submit($form, &$form_state) {
110112
}
111113
config_set('headless.settings', 'vocabularies.' . $type, $val);
112114
}
113-
foreach($form_state['input']['views-checkboxes'] as $type => $value) {
115+
foreach ($form_state['input']['views-checkboxes'] as $type => $value) {
114116
if ($value != NULL) {
115117
$val = 1;
116118
}
@@ -120,7 +122,7 @@ function headless_settings_form_submit($form, &$form_state) {
120122
config_set('headless.settings', 'views.' . $type, $val);
121123
}
122124
if (isset($form_state['input']['views-checkboxes'])) {
123-
foreach($form_state['input']['views-checkboxes'] as $type => $value) {
125+
foreach ($form_state['input']['views-checkboxes'] as $type => $value) {
124126
if ($value != NULL) {
125127
$val = 1;
126128
}
@@ -131,7 +133,7 @@ function headless_settings_form_submit($form, &$form_state) {
131133
}
132134
}
133135
if (isset($form_state['input']['paragraphs-checkboxes'])) {
134-
foreach($form_state['input']['paragraphs-checkboxes'] as $type => $value) {
136+
foreach ($form_state['input']['paragraphs-checkboxes'] as $type => $value) {
135137
if ($value != NULL) {
136138
$val = 1;
137139
}
@@ -164,7 +166,7 @@ function _headless_get_default_options($types) {
164166
$types = config_get('headless.settings', $types);
165167

166168
if (isset($types)) {
167-
foreach($types as $key => $type) {
169+
foreach ($types as $key => $type) {
168170
if ($type != 0) {
169171
$checked[] = $key;
170172
}

includes/headless.router.inc

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
<?php
22
/**
3+
* @file
34
* Router: allow API queries via strings in URL path.
4-
* The router will try to resolve a string to an API asset.
5+
* The router will try to resolve a string to an API asset.
56
*/
67

78
/**
@@ -18,7 +19,7 @@ function headless_router(string $path) {
1819
->condition('alias', '%' . db_like($path) . '%', 'LIKE')
1920
->addTag('node_access')
2021
->execute();
21-
foreach($query as $i => $asset) {
22+
foreach ($query as $i => $asset) {
2223
$assets[] = $asset;
2324
}
2425
$source = $assets[0]->source;

0 commit comments

Comments
 (0)