Skip to content
Permalink
Browse files

add custom blocks end point

  • Loading branch information...
robertgarrigos committed Aug 18, 2019
1 parent 4cdb6d0 commit 707c16420e697589857dab66995ed826adcd6b0e
Showing with 79 additions and 1 deletion.
  1. +2 −1 README.md
  2. +24 −0 headless.module
  3. +53 −0 includes/headless.admin.inc
@@ -51,11 +51,12 @@ Endpoints:
* example: `?page=0`
* ?{exposed_filter}={value}
* you can use also any exposed filter you have set for your view
* example: show only results with a title which contains a string use `?title=some_string`
* example: show only results with a title which contains a string use `?title=some_string`
* You can combine filter arguments by chaning them wiht a `&`
* example: `?type=post&title=string`
* The best way to see what are the arguments you can use is by creating the exposed filters and/or contextual arguments, then go to the view, do a search and look at the url that it generates.
* Paragraphs: `/api/paragraphs/{type}/{id}`
* Custom blocks: `/api/blocks/{block_name}`

License
-------
@@ -50,6 +50,11 @@ function headless_menu() {
'page arguments' => array(3, 4),
);
}
$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.',
@@ -197,6 +202,25 @@ function headless_paragraphs_item_v2($type, $entity_id) {
backdrop_exit();
}

/**
* Page callback for blocks.
*
* This is kept to make it backwards compatible
*/
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();
}

$result = config_get('block.custom.' . $block);
backdrop_json_output($result);
backdrop_exit();
}

/**
* Helper function.
*
@@ -211,6 +211,50 @@ function headless_settings_form($form, &$form_state) {
}
}
// Get all available custom blocks.
$my_blocks = block_block_info();
$form['blocks'] = array(
'#type' => 'fieldset',
'#title' => t('Custom blocks'),
'#collapsible' => TRUE,
);
if (empty($my_blocks)) {
$no_blocks = t('No blocks defined yet.');
if (module_exists('block')) {
$no_blocks .= ' ' . l(t('Manage custom blocks'), 'admin/structure/block');
}
$form['blocks']['blocks-empty'] = array(
'#markup' => $no_blocks,
);
}
else {
$my_blocks_header = array(
'block' => t('Block'),
'endpoint_v1' => t('End point'),
);
foreach ($my_blocks as $key => $block) {
$my_blocks_options[$key] = array(
'block' => $key,
'endpoint_v1' => array(
'data' => array(
'#type' => 'link',
'#title' => '/api/blocks/' . $key,
'#href' => '/api/blocks/' . $key,
),
),
);
}
$form['blocks']['blocks-checkboxes'] = array(
'#type' => 'tableselect',
'#header' => $my_blocks_header,
'#options' => $my_blocks_options,
'#default_value' => _headless_get_default_options('blocks'),
'#js_select' => FALSE,
);
}
$form['actions'] = array('#type' => 'actions');
$form['actions']['submit'] = array(
'#type' => 'submit',
@@ -275,6 +319,15 @@ function headless_settings_form_submit($form, &$form_state) {
config_set('headless.settings', 'paragraphs.' . $type, $val);
}
}
foreach ($form_state['input']['blocks-checkboxes'] as $type => $value) {
if ($value != NULL) {
$val = 1;
}
else {
$val = 0;
}
config_set('headless.settings', 'blocks.' . $type, $val);
}
backdrop_set_message(t('The Headless configuration has been saved.'));
$form_state['redirect'] = 'admin/config/services/headless';

0 comments on commit 707c164

Please sign in to comment.
You can’t perform that action at this time.