-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsak.module
81 lines (77 loc) · 2.28 KB
/
sak.module
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
<?php
/**
* @file
* Convert given basic page node to json using api key.
* @author : Sunny.
*/
/**
* Implements hook_menu().
*/
function sak_menu() {
$menu['node_json/%'] = array(
'title' => 'Basic node to json',
'page callback' => 'drupal_get_form',
'page arguments' => array('sak_logic'),
'access callback' => TRUE,
'type' => MENU_CALLBACK,
);
return $menu;
}
/**
* Convert given node to its json value.
*/
function sak_logic() {
$arg = arg();
// for URL node_json only
if ($arg[0] == 'node_json' && $arg[1] != NULL && is_numeric($arg[1])) {
if (variable_get('siteapikey') != '' and variable_get('siteapikey') != NULL ) {
if ($node = node_load ($arg[1]) ) {
// Should be 'Page' content type only and should be published.
if ( $node->type == 'page' && ($node->status ==1) ) {
// Display JSON data of given node
drupal_add_http_header('Content-Type', 'application/json');
if (isset($node)) {
echo drupal_json_encode($node);
exit();
}
}
else
{
drupal_set_message('Access Denied' , 'error');
}
}
}
}
}
/**
* Implements hook_form_alter().
*/
function sak_form_alter(&$form, &$form_state, $form_id) {
if ( $form_id == 'system_site_information_settings' ) {
// Textbox for API secret key
$form['site_information']['siteapikey'] = array(
'#type' => 'textfield',
'#title' => t('Site API Key'),
'#default_value' => variable_get('siteapikey'),
'#description' => t('Enter your API key here.'),
'#required' => TRUE,
);
// If API Key value already set then Update Configuration.
if ( variable_get('siteapikey') != NULL ) {
$form['actions']['submit']['#value'] = t('Update Configuration');
}
// adding custom form submit in form.
$form['#submit'][] = 'sak_settings_form_submit';
}
return system_settings_form($form);
}
/**
* Form submit for sak_settings_form_submit.
*/
function sak_settings_form_submit(&$form, &$form_state) {
// Set Drupal set meeage when field is saved.
if ($form_state['values']['siteapikey'] != NULL || $form_state['values']['siteapikey'] != ''){
$message = t('Site API Key has been saved');
drupal_set_message($message);
}
}