Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fixes #47: refactor code for different versions #54

Open
wants to merge 4 commits into
base: 1.x-1.x
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
159 changes: 41 additions & 118 deletions headless.module
Original file line number Diff line number Diff line change
Expand Up @@ -9,75 +9,79 @@
* Implements hook_menu().
*/
function headless_menu() {
$items = array();
$items = [];
// Return JSON for individual nodes.
$items['api/node/%/%'] = array(
$items['api/node/%/%'] = [
'page callback' => 'headless_type',
'access callback' => TRUE,
'page arguments' => array(2, 3),
);
$items['api/v2/node/%/%'] = array(
'page arguments' => [2, 3],
];
$items['api/v2/node/%/%'] = [
'page callback' => 'headless_type_v2',
'access callback' => TRUE,
'page arguments' => array(3, 4),
);
'page arguments' => [3, 4],
'file' => 'includes/headless.v2.inc',
];
// Return JSON for individual terms.
$items['api/%/term/%'] = array(
$items['api/%/term/%'] = [
'page callback' => 'headless_term_item',
'access callback' => TRUE,
'page arguments' => array(1, 3),
);
$items['api/views/%'] = array(
'page arguments' => [1, 3],
];
$items['api/views/%'] = [
'page callback' => 'headless_views',
'access callback' => TRUE,
'page arguments' => array(2),
);
$items['api/v2/views/%/%'] = array(
'page arguments' => [2],
];
$items['api/v2/views/%/%'] = [
'page callback' => 'headless_views_v2',
'access callback' => TRUE,
'page arguments' => array(3, 4),
);
'page arguments' => [3, 4],
'file' => 'includes/headless.v2.inc',
];
if (module_exists('paragraphs')) {
// Return json for individual paragraphs.
$items['api/paragraphs/%/%'] = array(
$items['api/paragraphs/%/%'] = [
'page callback' => 'headless_paragraphs_item',
'access callback' => TRUE,
'page arguments' => array(2, 3),
);
$items['api/v2/paragraphs/%/%'] = array(
'page arguments' => [2, 3],
];
$items['api/v2/paragraphs/%/%'] = [
'page callback' => 'headless_paragraphs_item_v2',
'access callback' => TRUE,
'page arguments' => array(3, 4),
);
$items['api/v3/paragraphs/%'] = array(
'page arguments' => [3, 4],
'file' => 'includes/headless.v2.inc',
];
$items['api/v3/paragraphs/%'] = [
'page callback' => 'headless_paragraphs_item_v3',
'access callback' => TRUE,
'page arguments' => array(3),
);
'page arguments' => [3],
'file' => 'includes/headless.v3.inc',
];
}
$items['api/blocks/%'] = array(
$items['api/blocks/%'] = [
'page callback' => 'headless_blocks',
'access callback' => TRUE,
'page arguments' => array(2),
);
$items['admin/config/services/headless'] = array(
'page arguments' => [2],
];
$items['admin/config/services/headless'] = [
'title' => 'Headless settings',
'description' => 'Configure the entity types you wish to expose as json endpoints.',
'page callback' => 'backdrop_get_form',
'page arguments' => array('headless_settings_form'),
'access arguments' => array('administer site configuration'),
'page arguments' => ['headless_settings_form'],
'access arguments' => ['administer site configuration'],
'file' => 'includes/headless.admin.inc',
);
];

// Router: allow API queries via string paths.
$items['api/router/%'] = array(
$items['api/router/%'] = [
'title' => 'Headless router',
'description' => 'Allow queries via string like /api/my-node-title',
'page callback' => 'headless_router',
'page arguments' => array(2),
'page arguments' => [2],
'access callback' => TRUE,
'file' => 'includes/headless.router.inc',
);
];

return $items;
}
Expand Down Expand Up @@ -108,31 +112,6 @@ function headless_type($type, $nid) {
}
}

/**
* Page callback for node types v2.
*/
function headless_type_v2($type, $nid) {
// Check if the JSON output is enabled for the selected type.
$config = config_get('headless.settings', 'node');
if ($config[$type] != 1) {
$json_error = ['code' => 404];
backdrop_json_output($json_error);
backdrop_exit();
}
// Load the requested node and check to see if types match or not.
$requested_node = node_load($nid);
if ($requested_node->type == $type) {
$my_json = node_view($requested_node);
backdrop_json_output($my_json);
backdrop_exit();
}
else {
$json_error = ['code' => 404];
backdrop_json_output($json_error);
backdrop_exit();
}
}

/**
* Page callback for terms.
*/
Expand Down Expand Up @@ -170,32 +149,6 @@ function headless_views($view) {
backdrop_exit();
}

/**
* Page callback for views v2.
*/
function headless_views_v2($view, $display_id) {
$args = func_get_args();
$config = config_get('headless.settings', 'views');
if ($config[$view] != 1) {
$json_error = ['code' => 404];
backdrop_json_output($json_error);
backdrop_exit();
}
$return = _views_get_view_result($view, $display_id, $args);

$my_json = [
'results' => $return['results'],
'total_items' => $return['total_items'],
'items_per_page' => $return['items_per_page'],
'total_pages' =>
(isset($return['items_per_page'])) ? ceil($return['total_items'] / $return['items_per_page']) : NULL,
'current_page' => $return['current_page'],
];
backdrop_json_output($my_json);
json_last_error_msg();
backdrop_exit();
}

/**
* Page callback for paragraph types.
*/
Expand All @@ -211,36 +164,6 @@ function headless_paragraphs_item($type, $entity_id) {
backdrop_exit();
}

/**
* Page callback for paragraph types v2.
*/
function headless_paragraphs_item_v2($type, $entity_id) {
$config = config_get('headless.settings', 'paragraphs');
if ($config[$type] != 1) {
$json_error = ['code' => 404];
backdrop_json_output($json_error);
backdrop_exit();
}
$result = paragraphs_item_load($entity_id)->view();
backdrop_json_output($result);
backdrop_exit();
}

/**
* Page callback for paragraph types v3.
*/
function headless_paragraphs_item_v3($entity_id) {
$config = config_get('headless.settings', 'paragraphs');
$p = paragraphs_item_load($entity_id);
if ($config[$p->bundle] != 1) {
$json_error = ['code' => 404];
backdrop_json_output($json_error);
backdrop_exit();
}
$result = $p->view();
backdrop_json_output($result);
backdrop_exit();
}
/**
* Page callback for blocks.
*
Expand Down Expand Up @@ -273,7 +196,7 @@ function _views_get_view_result($name, $display_id = NULL, $args = NULL) {
}

$view = views_get_view($name);
$return = array();
$return = [];
if (is_object($view)) {
if (is_array($args)) {
$view->set_arguments($args);
Expand All @@ -298,6 +221,6 @@ function _views_get_view_result($name, $display_id = NULL, $args = NULL) {
return $return;
}
else {
return array();
return [];
}
}
71 changes: 71 additions & 0 deletions includes/headless.v2.inc
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
<?php

/**
* @file
* V2 functions for Headless module.
*/

/**
* Page callback for node types v2.
*/
function headless_type_v2($type, $nid) {
// Check if the JSON output is enabled for the selected type.
$config = config_get('headless.settings', 'node');
if ($config[$type] != 1) {
$json_error = ['code' => 404];
backdrop_json_output($json_error);
backdrop_exit();
}
// Load the requested node and check to see if types match or not.
$requested_node = node_load($nid);
if ($requested_node->type == $type) {
$my_json = node_view($requested_node);
backdrop_json_output($my_json);
backdrop_exit();
}
else {
$json_error = ['code' => 404];
backdrop_json_output($json_error);
backdrop_exit();
}
}

/**
* Page callback for views v2.
*/
function headless_views_v2($view, $display_id) {
$args = func_get_args();
$config = config_get('headless.settings', 'views');
if ($config[$view] != 1) {
$json_error = ['code' => 404];
backdrop_json_output($json_error);
backdrop_exit();
}
$return = _views_get_view_result($view, $display_id, $args);

$my_json = [
'results' => $return['results'],
'total_items' => $return['total_items'],
'items_per_page' => $return['items_per_page'],
'total_pages' => (isset($return['items_per_page'])) ? ceil($return['total_items'] / $return['items_per_page']) : NULL,
'current_page' => $return['current_page'],
];
backdrop_json_output($my_json);
json_last_error_msg();
backdrop_exit();
}

/**
* Page callback for paragraph types v2.
*/
function headless_paragraphs_item_v2($type, $entity_id) {
$config = config_get('headless.settings', 'paragraphs');
if ($config[$type] != 1) {
$json_error = ['code' => 404];
backdrop_json_output($json_error);
backdrop_exit();
}
$result = paragraphs_item_load($entity_id)->view();
backdrop_json_output($result);
backdrop_exit();
}
23 changes: 23 additions & 0 deletions includes/headless.v3.inc
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

/**
* @file
* V3 functions for Headless module.
*/


/**
* Page callback for paragraph types v3.
*/
function headless_paragraphs_item_v3($entity_id) {
$config = config_get('headless.settings', 'paragraphs');
$p = paragraphs_item_load($entity_id);
if ($config[$p->bundle] != 1) {
$json_error = ['code' => 404];
backdrop_json_output($json_error);
backdrop_exit();
}
$result = $p->view();
backdrop_json_output($result);
backdrop_exit();
}