From 416b27ff048ce4247e9aafa7c95dc6cec371bd57 Mon Sep 17 00:00:00 2001 From: robertgarrigos Date: Thu, 2 Jul 2020 11:08:43 +0200 Subject: [PATCH 1/4] fixes #47: refactor code for different versions --- headless.module | 133 +++++++++------------------------------ includes/headless.v2.inc | 74 ++++++++++++++++++++++ includes/headless.v3.inc | 24 +++++++ 3 files changed, 129 insertions(+), 102 deletions(-) create mode 100644 includes/headless.v2.inc create mode 100644 includes/headless.v3.inc diff --git a/headless.module b/headless.module index bad0b1d..270b3ae 100644 --- a/headless.module +++ b/headless.module @@ -8,56 +8,61 @@ /** * Implements hook_menu(). */ -function headless_menu() { +function headless_menu() +{ $items = array(); // Return JSON for individual nodes. $items['api/node/%/%'] = array( 'page callback' => 'headless_type', - 'access callback' => TRUE, + 'access callback' => true, 'page arguments' => array(2, 3), ); $items['api/v2/node/%/%'] = array( 'page callback' => 'headless_type_v2', - 'access callback' => TRUE, + 'access callback' => true, 'page arguments' => array(3, 4), + 'file' => 'includes/headless.v2.inc', ); // Return JSON for individual terms. $items['api/%/term/%'] = array( 'page callback' => 'headless_term_item', - 'access callback' => TRUE, + 'access callback' => true, 'page arguments' => array(1, 3), ); $items['api/views/%'] = array( 'page callback' => 'headless_views', - 'access callback' => TRUE, + 'access callback' => true, 'page arguments' => array(2), ); $items['api/v2/views/%/%'] = array( 'page callback' => 'headless_views_v2', - 'access callback' => TRUE, + 'access callback' => true, 'page arguments' => array(3, 4), + 'file' => 'includes/headless.v2.inc', ); if (module_exists('paragraphs')) { // Return json for individual paragraphs. $items['api/paragraphs/%/%'] = array( 'page callback' => 'headless_paragraphs_item', - 'access callback' => TRUE, + 'access callback' => true, 'page arguments' => array(2, 3), ); $items['api/v2/paragraphs/%/%'] = array( 'page callback' => 'headless_paragraphs_item_v2', - 'access callback' => TRUE, + 'access callback' => true, 'page arguments' => array(3, 4), + 'file' => 'includes/headless.v2.inc', ); $items['api/v3/paragraphs/%'] = array( 'page callback' => 'headless_paragraphs_item_v3', - 'access callback' => TRUE, + 'access callback' => true, 'page arguments' => array(3), + 'file' => 'includes/headless.v3.inc', ); } $items['api/blocks/%'] = array( 'page callback' => 'headless_blocks', - 'access callback' => TRUE, + 'access callback' => true, 'page arguments' => array(2), ); $items['admin/config/services/headless'] = array( @@ -75,7 +80,7 @@ function headless_menu() { 'description' => 'Allow queries via string like /api/my-node-title', 'page callback' => 'headless_router', 'page arguments' => array(2), - 'access callback' => TRUE, + 'access callback' => true, 'file' => 'includes/headless.router.inc', ); @@ -87,7 +92,8 @@ function headless_menu() { * * This is kept to make it backwards compatible */ -function headless_type($type, $nid) { +function headless_type($type, $nid) +{ $config = config_get('headless.settings', 'node'); // Check if the JSON output is enabled for the selected type. if ($config[$type] != 1) { @@ -100,33 +106,7 @@ function headless_type($type, $nid) { if ($requested_node->type == $type) { backdrop_json_output($requested_node); backdrop_exit(); - } - else { - $json_error = ['code' => 404]; - backdrop_json_output($json_error); - backdrop_exit(); - } -} - -/** - * 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 { + } else { $json_error = ['code' => 404]; backdrop_json_output($json_error); backdrop_exit(); @@ -136,7 +116,8 @@ function headless_type_v2($type, $nid) { /** * Page callback for terms. */ -function headless_term_item($vocab, $tid) { +function headless_term_item($vocab, $tid) +{ $config = config_get('headless.settings', 'vocabularies'); if ($config[$vocab] != 1) { $json_error = ['code' => 404]; @@ -153,7 +134,8 @@ function headless_term_item($vocab, $tid) { * * This is kept to make it backwards compatible */ -function headless_views($view) { +function headless_views($view) +{ $config = config_get('headless.settings', 'views'); if ($config[$view] != 1) { $json_error = ['code' => 404]; @@ -170,36 +152,12 @@ 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. */ -function headless_paragraphs_item($type, $entity_id) { +function headless_paragraphs_item($type, $entity_id) +{ $config = config_get('headless.settings', 'paragraphs'); if ($config[$type] != 1) { $json_error = ['code' => 404]; @@ -211,42 +169,14 @@ 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. * * This is kept to make it backwards compatible */ -function headless_blocks($block) { +function headless_blocks($block) +{ $config = config_get('headless.settings', 'blocks'); if ($config[$block] != 1) { @@ -266,7 +196,8 @@ function headless_blocks($block) { * We mimic the core function to add some extra pager data to the * returned value. */ -function _views_get_view_result($name, $display_id = NULL, $args = NULL) { +function _views_get_view_result($name, $display_id = null, $args = null) +{ array_shift($args); if (count($args)) { array_shift($args); @@ -280,8 +211,7 @@ function _views_get_view_result($name, $display_id = NULL, $args = NULL) { } if (is_string($display_id)) { $view->set_display($display_id); - } - else { + } else { $view->init_display(); } $view->pre_execute(); @@ -296,8 +226,7 @@ function _views_get_view_result($name, $display_id = NULL, $args = NULL) { $return['current_page'] = $current_page; $return['results'] = $view->result; return $return; - } - else { + } else { return array(); } } diff --git a/includes/headless.v2.inc b/includes/headless.v2.inc new file mode 100644 index 0000000..c953a44 --- /dev/null +++ b/includes/headless.v2.inc @@ -0,0 +1,74 @@ + 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..2976419 --- /dev/null +++ b/includes/headless.v3.inc @@ -0,0 +1,24 @@ +bundle] != 1) { + $json_error = ['code' => 404]; + backdrop_json_output($json_error); + backdrop_exit(); + } + $result = $p->view(); + backdrop_json_output($result); + backdrop_exit(); +} From a36cd53eb5c22d5bc92d962669336ea575a73ba5 Mon Sep 17 00:00:00 2001 From: robertgarrigos Date: Thu, 2 Jul 2020 16:52:33 +0200 Subject: [PATCH 2/4] code style fixing --- headless.module | 340 +++++++++++++++++++-------------------- includes/headless.v2.inc | 99 ++++++------ includes/headless.v3.inc | 3 +- 3 files changed, 216 insertions(+), 226 deletions(-) diff --git a/headless.module b/headless.module index 270b3ae..baaefcd 100644 --- a/headless.module +++ b/headless.module @@ -8,83 +8,82 @@ /** * Implements hook_menu(). */ -function headless_menu() -{ - $items = array(); - // Return JSON for individual nodes. - $items['api/node/%/%'] = array( - 'page callback' => 'headless_type', - 'access callback' => true, - 'page arguments' => array(2, 3), - ); - $items['api/v2/node/%/%'] = array( - 'page callback' => 'headless_type_v2', - 'access callback' => true, - 'page arguments' => array(3, 4), - 'file' => 'includes/headless.v2.inc', - ); - // Return JSON for individual terms. - $items['api/%/term/%'] = array( - 'page callback' => 'headless_term_item', - 'access callback' => true, - 'page arguments' => array(1, 3), - ); - $items['api/views/%'] = array( - 'page callback' => 'headless_views', - 'access callback' => true, - 'page arguments' => array(2), - ); - $items['api/v2/views/%/%'] = array( - 'page callback' => 'headless_views_v2', - 'access callback' => true, - 'page arguments' => array(3, 4), - 'file' => 'includes/headless.v2.inc', - ); - if (module_exists('paragraphs')) { - // Return json for individual paragraphs. - $items['api/paragraphs/%/%'] = array( - 'page callback' => 'headless_paragraphs_item', - 'access callback' => true, - 'page arguments' => array(2, 3), - ); - $items['api/v2/paragraphs/%/%'] = array( - 'page callback' => 'headless_paragraphs_item_v2', - 'access callback' => true, - 'page arguments' => array(3, 4), - 'file' => 'includes/headless.v2.inc', - ); - $items['api/v3/paragraphs/%'] = array( - 'page callback' => 'headless_paragraphs_item_v3', - 'access callback' => true, - 'page arguments' => array(3), - 'file' => 'includes/headless.v3.inc', - ); - } - $items['api/blocks/%'] = array( - 'page callback' => 'headless_blocks', - 'access callback' => true, - 'page arguments' => array(2), - ); - $items['admin/config/services/headless'] = array( - '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'), - 'file' => 'includes/headless.admin.inc', - ); - - // Router: allow API queries via string paths. - $items['api/router/%'] = array( - 'title' => 'Headless router', - 'description' => 'Allow queries via string like /api/my-node-title', - 'page callback' => 'headless_router', - 'page arguments' => array(2), - 'access callback' => true, - 'file' => 'includes/headless.router.inc', - ); - - return $items; +function headless_menu() { + $items = []; + // Return JSON for individual nodes. + $items['api/node/%/%'] = [ + 'page callback' => 'headless_type', + 'access callback' => TRUE, + 'page arguments' => [2, 3], + ]; + $items['api/v2/node/%/%'] = [ + 'page callback' => 'headless_type_v2', + 'access callback' => TRUE, + 'page arguments' => [3, 4], + 'file' => 'includes/headless.v2.inc', + ]; + // Return JSON for individual terms. + $items['api/%/term/%'] = [ + 'page callback' => 'headless_term_item', + 'access callback' => TRUE, + 'page arguments' => [1, 3], + ]; + $items['api/views/%'] = [ + 'page callback' => 'headless_views', + 'access callback' => TRUE, + 'page arguments' => [2], + ]; + $items['api/v2/views/%/%'] = [ + 'page callback' => 'headless_views_v2', + 'access callback' => TRUE, + 'page arguments' => [3, 4], + 'file' => 'includes/headless.v2.inc', + ]; + if (module_exists('paragraphs')) { + // Return json for individual paragraphs. + $items['api/paragraphs/%/%'] = [ + 'page callback' => 'headless_paragraphs_item', + 'access callback' => TRUE, + 'page arguments' => [2, 3], + ]; + $items['api/v2/paragraphs/%/%'] = [ + 'page callback' => 'headless_paragraphs_item_v2', + 'access callback' => TRUE, + 'page arguments' => [3, 4], + 'file' => 'includes/headless.v2.inc', + ]; + $items['api/v3/paragraphs/%'] = [ + 'page callback' => 'headless_paragraphs_item_v3', + 'access callback' => TRUE, + 'page arguments' => [3], + 'file' => 'includes/headless.v3.inc', + ]; + } + $items['api/blocks/%'] = [ + 'page callback' => 'headless_blocks', + 'access callback' => TRUE, + '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' => ['headless_settings_form'], + 'access arguments' => ['administer site configuration'], + 'file' => 'includes/headless.admin.inc', + ]; + + // Router: allow API queries via string paths. + $items['api/router/%'] = [ + 'title' => 'Headless router', + 'description' => 'Allow queries via string like /api/my-node-title', + 'page callback' => 'headless_router', + 'page arguments' => [2], + 'access callback' => TRUE, + 'file' => 'includes/headless.router.inc', + ]; + + return $items; } /** @@ -92,41 +91,40 @@ function headless_menu() * * This is kept to make it backwards compatible */ -function headless_type($type, $nid) -{ - $config = config_get('headless.settings', 'node'); - // Check if the JSON output is enabled for the selected type. - 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) { - backdrop_json_output($requested_node); - backdrop_exit(); - } else { - $json_error = ['code' => 404]; - backdrop_json_output($json_error); - backdrop_exit(); - } +function headless_type($type, $nid) { + $config = config_get('headless.settings', 'node'); + // Check if the JSON output is enabled for the selected type. + 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) { + backdrop_json_output($requested_node); + backdrop_exit(); + } + else { + $json_error = ['code' => 404]; + backdrop_json_output($json_error); + backdrop_exit(); + } } /** * Page callback for terms. */ -function headless_term_item($vocab, $tid) -{ - $config = config_get('headless.settings', 'vocabularies'); - if ($config[$vocab] != 1) { - $json_error = ['code' => 404]; - backdrop_json_output($json_error); +function headless_term_item($vocab, $tid) { + $config = config_get('headless.settings', 'vocabularies'); + if ($config[$vocab] != 1) { + $json_error = ['code' => 404]; + backdrop_json_output($json_error); + backdrop_exit(); + } + $my_json = taxonomy_term_load($tid); + backdrop_json_output($my_json); backdrop_exit(); - } - $my_json = taxonomy_term_load($tid); - backdrop_json_output($my_json); - backdrop_exit(); } /** @@ -134,60 +132,55 @@ function headless_term_item($vocab, $tid) * * This is kept to make it backwards compatible */ -function headless_views($view) -{ - $config = config_get('headless.settings', 'views'); - if ($config[$view] != 1) { - $json_error = ['code' => 404]; - backdrop_json_output($json_error); +function headless_views($view) { + $config = config_get('headless.settings', 'views'); + if ($config[$view] != 1) { + $json_error = ['code' => 404]; + backdrop_json_output($json_error); + backdrop_exit(); + } + $results = views_get_view_result($view); + $count = count($results); + $my_json = [ + 'results' => $results, + 'count' => $count, + ]; + backdrop_json_output($my_json); backdrop_exit(); - } - $results = views_get_view_result($view); - $count = count($results); - $my_json = [ - 'results' => $results, - 'count' => $count, - ]; - backdrop_json_output($my_json); - backdrop_exit(); } - /** * Page callback for paragraph types. */ -function headless_paragraphs_item($type, $entity_id) -{ - $config = config_get('headless.settings', 'paragraphs'); - if ($config[$type] != 1) { - $json_error = ['code' => 404]; - backdrop_json_output($json_error); +function headless_paragraphs_item($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); + backdrop_json_output($result); backdrop_exit(); - } - $result = paragraphs_item_load($entity_id); - backdrop_json_output($result); - backdrop_exit(); } - /** * Page callback for blocks. * * This is kept to make it backwards compatible */ -function headless_blocks($block) -{ - $config = config_get('headless.settings', 'blocks'); +function headless_blocks($block) { + $config = config_get('headless.settings', 'blocks'); - if ($config[$block] != 1) { - $json_error = ['code' => 404]; - backdrop_json_output($json_error); - backdrop_exit(); - } + if ($config[$block] != 1) { + $json_error = ['code' => 404]; + backdrop_json_output($json_error); + backdrop_exit(); + } - $result = config_get('block.custom.' . $block); - backdrop_json_output($result); - backdrop_exit(); + $result = config_get('block.custom.' . $block); + backdrop_json_output($result); + backdrop_exit(); } /** @@ -196,37 +189,38 @@ function headless_blocks($block) * We mimic the core function to add some extra pager data to the * returned value. */ -function _views_get_view_result($name, $display_id = null, $args = null) -{ - array_shift($args); - if (count($args)) { +function _views_get_view_result($name, $display_id = NULL, $args = NULL) { array_shift($args); - } + if (count($args)) { + array_shift($args); + } - $view = views_get_view($name); - $return = array(); - if (is_object($view)) { - if (is_array($args)) { - $view->set_arguments($args); + $view = views_get_view($name); + $return = []; + if (is_object($view)) { + if (is_array($args)) { + $view->set_arguments($args); + } + if (is_string($display_id)) { + $view->set_display($display_id); + } + else { + $view->init_display(); + } + $view->pre_execute(); + $view->execute(); + + $total_items = $view->query->pager->total_items; + $items_per_page = $view->query->pager->options['items_per_page']; + $current_page = $view->query->pager->current_page; + + $return['total_items'] = $total_items; + $return['items_per_page'] = $items_per_page; + $return['current_page'] = $current_page; + $return['results'] = $view->result; + return $return; } - if (is_string($display_id)) { - $view->set_display($display_id); - } else { - $view->init_display(); + else { + return []; } - $view->pre_execute(); - $view->execute(); - - $total_items = $view->query->pager->total_items; - $items_per_page = $view->query->pager->options['items_per_page']; - $current_page = $view->query->pager->current_page; - - $return['total_items'] = $total_items; - $return['items_per_page'] = $items_per_page; - $return['current_page'] = $current_page; - $return['results'] = $view->result; - return $return; - } else { - return array(); - } } diff --git a/includes/headless.v2.inc b/includes/headless.v2.inc index c953a44..7940237 100644 --- a/includes/headless.v2.inc +++ b/includes/headless.v2.inc @@ -5,70 +5,67 @@ * 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(); - } +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); +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(); + $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); +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(); - } - $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 index 2976419..426fb73 100644 --- a/includes/headless.v3.inc +++ b/includes/headless.v3.inc @@ -9,8 +9,7 @@ /** * Page callback for paragraph types v3. */ -function headless_paragraphs_item_v3($entity_id) -{ +function headless_paragraphs_item_v3($entity_id) { $config = config_get('headless.settings', 'paragraphs'); $p = paragraphs_item_load($entity_id); if ($config[$p->bundle] != 1) { From 13665de5bad0c8e1e7db59237a6e5ecb00555df3 Mon Sep 17 00:00:00 2001 From: robertgarrigos Date: Thu, 2 Jul 2020 17:08:34 +0200 Subject: [PATCH 3/4] code style fixing again --- headless.module | 308 ++++++++++++++++++++++++------------------------ 1 file changed, 154 insertions(+), 154 deletions(-) diff --git a/headless.module b/headless.module index baaefcd..6e7d5b9 100644 --- a/headless.module +++ b/headless.module @@ -9,81 +9,81 @@ * Implements hook_menu(). */ function headless_menu() { - $items = []; - // Return JSON for individual nodes. - $items['api/node/%/%'] = [ - 'page callback' => 'headless_type', - 'access callback' => TRUE, - 'page arguments' => [2, 3], + $items = []; + // Return JSON for individual nodes. + $items['api/node/%/%'] = [ + 'page callback' => 'headless_type', + 'access callback' => TRUE, + 'page arguments' => [2, 3], + ]; + $items['api/v2/node/%/%'] = [ + 'page callback' => 'headless_type_v2', + 'access callback' => TRUE, + 'page arguments' => [3, 4], + 'file' => 'includes/headless.v2.inc', + ]; + // Return JSON for individual terms. + $items['api/%/term/%'] = [ + 'page callback' => 'headless_term_item', + 'access callback' => TRUE, + 'page arguments' => [1, 3], + ]; + $items['api/views/%'] = [ + 'page callback' => 'headless_views', + 'access callback' => TRUE, + 'page arguments' => [2], + ]; + $items['api/v2/views/%/%'] = [ + 'page callback' => 'headless_views_v2', + 'access callback' => TRUE, + 'page arguments' => [3, 4], + 'file' => 'includes/headless.v2.inc', + ]; + if (module_exists('paragraphs')) { + // Return json for individual paragraphs. + $items['api/paragraphs/%/%'] = [ + 'page callback' => 'headless_paragraphs_item', + 'access callback' => TRUE, + 'page arguments' => [2, 3], ]; - $items['api/v2/node/%/%'] = [ - 'page callback' => 'headless_type_v2', - 'access callback' => TRUE, - 'page arguments' => [3, 4], - 'file' => 'includes/headless.v2.inc', + $items['api/v2/paragraphs/%/%'] = [ + 'page callback' => 'headless_paragraphs_item_v2', + 'access callback' => TRUE, + 'page arguments' => [3, 4], + 'file' => 'includes/headless.v2.inc', ]; - // Return JSON for individual terms. - $items['api/%/term/%'] = [ - 'page callback' => 'headless_term_item', - 'access callback' => TRUE, - 'page arguments' => [1, 3], - ]; - $items['api/views/%'] = [ - 'page callback' => 'headless_views', - 'access callback' => TRUE, - 'page arguments' => [2], - ]; - $items['api/v2/views/%/%'] = [ - 'page callback' => 'headless_views_v2', - 'access callback' => TRUE, - 'page arguments' => [3, 4], - 'file' => 'includes/headless.v2.inc', - ]; - if (module_exists('paragraphs')) { - // Return json for individual paragraphs. - $items['api/paragraphs/%/%'] = [ - 'page callback' => 'headless_paragraphs_item', - 'access callback' => TRUE, - 'page arguments' => [2, 3], - ]; - $items['api/v2/paragraphs/%/%'] = [ - 'page callback' => 'headless_paragraphs_item_v2', - 'access callback' => TRUE, - 'page arguments' => [3, 4], - 'file' => 'includes/headless.v2.inc', - ]; - $items['api/v3/paragraphs/%'] = [ - 'page callback' => 'headless_paragraphs_item_v3', - 'access callback' => TRUE, - 'page arguments' => [3], - 'file' => 'includes/headless.v3.inc', - ]; - } - $items['api/blocks/%'] = [ - 'page callback' => 'headless_blocks', - 'access callback' => TRUE, - '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' => ['headless_settings_form'], - 'access arguments' => ['administer site configuration'], - 'file' => 'includes/headless.admin.inc', + $items['api/v3/paragraphs/%'] = [ + 'page callback' => 'headless_paragraphs_item_v3', + 'access callback' => TRUE, + 'page arguments' => [3], + 'file' => 'includes/headless.v3.inc', ]; + } + $items['api/blocks/%'] = [ + 'page callback' => 'headless_blocks', + 'access callback' => TRUE, + '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' => ['headless_settings_form'], + 'access arguments' => ['administer site configuration'], + 'file' => 'includes/headless.admin.inc', + ]; - // Router: allow API queries via string paths. - $items['api/router/%'] = [ - 'title' => 'Headless router', - 'description' => 'Allow queries via string like /api/my-node-title', - 'page callback' => 'headless_router', - 'page arguments' => [2], - 'access callback' => TRUE, - 'file' => 'includes/headless.router.inc', - ]; + // Router: allow API queries via string paths. + $items['api/router/%'] = [ + 'title' => 'Headless router', + 'description' => 'Allow queries via string like /api/my-node-title', + 'page callback' => 'headless_router', + 'page arguments' => [2], + 'access callback' => TRUE, + 'file' => 'includes/headless.router.inc', + ]; - return $items; + return $items; } /** @@ -92,39 +92,39 @@ function headless_menu() { * This is kept to make it backwards compatible */ function headless_type($type, $nid) { - $config = config_get('headless.settings', 'node'); - // Check if the JSON output is enabled for the selected type. - 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) { - backdrop_json_output($requested_node); - backdrop_exit(); - } - else { - $json_error = ['code' => 404]; - backdrop_json_output($json_error); - backdrop_exit(); - } + $config = config_get('headless.settings', 'node'); + // Check if the JSON output is enabled for the selected type. + 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) { + backdrop_json_output($requested_node); + backdrop_exit(); + } + else { + $json_error = ['code' => 404]; + backdrop_json_output($json_error); + backdrop_exit(); + } } /** * Page callback for terms. */ function headless_term_item($vocab, $tid) { - $config = config_get('headless.settings', 'vocabularies'); - if ($config[$vocab] != 1) { - $json_error = ['code' => 404]; - backdrop_json_output($json_error); - backdrop_exit(); - } - $my_json = taxonomy_term_load($tid); - backdrop_json_output($my_json); + $config = config_get('headless.settings', 'vocabularies'); + if ($config[$vocab] != 1) { + $json_error = ['code' => 404]; + backdrop_json_output($json_error); backdrop_exit(); + } + $my_json = taxonomy_term_load($tid); + backdrop_json_output($my_json); + backdrop_exit(); } /** @@ -133,35 +133,35 @@ function headless_term_item($vocab, $tid) { * This is kept to make it backwards compatible */ function headless_views($view) { - $config = config_get('headless.settings', 'views'); - if ($config[$view] != 1) { - $json_error = ['code' => 404]; - backdrop_json_output($json_error); - backdrop_exit(); - } - $results = views_get_view_result($view); - $count = count($results); - $my_json = [ - 'results' => $results, - 'count' => $count, - ]; - backdrop_json_output($my_json); + $config = config_get('headless.settings', 'views'); + if ($config[$view] != 1) { + $json_error = ['code' => 404]; + backdrop_json_output($json_error); backdrop_exit(); + } + $results = views_get_view_result($view); + $count = count($results); + $my_json = [ + 'results' => $results, + 'count' => $count, + ]; + backdrop_json_output($my_json); + backdrop_exit(); } /** * Page callback for paragraph types. */ function headless_paragraphs_item($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); - backdrop_json_output($result); + $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); + backdrop_json_output($result); + backdrop_exit(); } /** @@ -170,17 +170,17 @@ function headless_paragraphs_item($type, $entity_id) { * This is kept to make it backwards compatible */ function headless_blocks($block) { - $config = config_get('headless.settings', 'blocks'); + $config = config_get('headless.settings', 'blocks'); - if ($config[$block] != 1) { - $json_error = ['code' => 404]; - backdrop_json_output($json_error); - backdrop_exit(); - } - - $result = config_get('block.custom.' . $block); - backdrop_json_output($result); + if ($config[$block] != 1) { + $json_error = ['code' => 404]; + backdrop_json_output($json_error); backdrop_exit(); + } + + $result = config_get('block.custom.' . $block); + backdrop_json_output($result); + backdrop_exit(); } /** @@ -190,37 +190,37 @@ function headless_blocks($block) { * returned value. */ function _views_get_view_result($name, $display_id = NULL, $args = NULL) { + array_shift($args); + if (count($args)) { array_shift($args); - if (count($args)) { - array_shift($args); - } + } - $view = views_get_view($name); - $return = []; - if (is_object($view)) { - if (is_array($args)) { - $view->set_arguments($args); - } - if (is_string($display_id)) { - $view->set_display($display_id); - } - else { - $view->init_display(); - } - $view->pre_execute(); - $view->execute(); - - $total_items = $view->query->pager->total_items; - $items_per_page = $view->query->pager->options['items_per_page']; - $current_page = $view->query->pager->current_page; - - $return['total_items'] = $total_items; - $return['items_per_page'] = $items_per_page; - $return['current_page'] = $current_page; - $return['results'] = $view->result; - return $return; + $view = views_get_view($name); + $return = []; + if (is_object($view)) { + if (is_array($args)) { + $view->set_arguments($args); + } + if (is_string($display_id)) { + $view->set_display($display_id); } else { - return []; + $view->init_display(); } + $view->pre_execute(); + $view->execute(); + + $total_items = $view->query->pager->total_items; + $items_per_page = $view->query->pager->options['items_per_page']; + $current_page = $view->query->pager->current_page; + + $return['total_items'] = $total_items; + $return['items_per_page'] = $items_per_page; + $return['current_page'] = $current_page; + $return['results'] = $view->result; + return $return; + } + else { + return []; + } } From 5685d0145764e42145d0c7fe793e142c24536091 Mon Sep 17 00:00:00 2001 From: robertgarrigos Date: Thu, 2 Jul 2020 17:11:25 +0200 Subject: [PATCH 4/4] code style fixing 3 --- includes/headless.v2.inc | 90 ++++++++++++++++++++-------------------- 1 file changed, 45 insertions(+), 45 deletions(-) diff --git a/includes/headless.v2.inc b/includes/headless.v2.inc index 7940237..e5726f8 100644 --- a/includes/headless.v2.inc +++ b/includes/headless.v2.inc @@ -9,63 +9,63 @@ * 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(); - } + // 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(); + $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); + $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(); }