diff --git a/headless.module b/headless.module index bad0b1d..6e7d5b9 100644 --- a/headless.module +++ b/headless.module @@ -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; } @@ -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. */ @@ -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. */ @@ -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. * @@ -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); @@ -298,6 +221,6 @@ function _views_get_view_result($name, $display_id = NULL, $args = NULL) { return $return; } else { - return array(); + return []; } } diff --git a/includes/headless.v2.inc b/includes/headless.v2.inc new file mode 100644 index 0000000..e5726f8 --- /dev/null +++ b/includes/headless.v2.inc @@ -0,0 +1,71 @@ + 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(); +} diff --git a/includes/headless.v3.inc b/includes/headless.v3.inc new file mode 100644 index 0000000..426fb73 --- /dev/null +++ b/includes/headless.v3.inc @@ -0,0 +1,23 @@ +bundle] != 1) { + $json_error = ['code' => 404]; + backdrop_json_output($json_error); + backdrop_exit(); + } + $result = $p->view(); + backdrop_json_output($result); + backdrop_exit(); +}