-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsak.module
65 lines (48 loc) · 1.99 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
<?php
/**
* @file
*
* Convert given basic page node to json using api key.
*/
/**
* Implements hook_form_FORM_ID_alter().
*/
function sak_form_system_site_information_settings_alter(&$form, \Drupal\Core\Form\FormStateInterface $form_state, $form_id) {
// Get the API Key configuration
$config = \Drupal::config('sak.settings');
// Check whether the API value is set by the user or empty.
if(empty($config->get('sakkey')) ) {
$site_api = $config->get('sakkey');
}
// Create Site API Key Textfield
$form['site_information']['site_api_key'] = [
'#type' => 'textfield',
'#title' => t('Site API Key'),
'#placeholder' =>t('Add Site API Key here'),
'#required' => TRUE,
'#default_value' => $site_api,
];
// Change the text of "Save configuration" button to "Update Configuration".
if ( !empty($config->get('sakkey')) && $site_api != '' ) {
$form['actions']['submit']['#value'] = t('Update Configuration');
}
// Add custom submit handler
$form['#submit'][] = 'sak_system_site_information_settings_submit_handler';
}
/**
* Custom Submit handler for FormID system_site_information_settings.
*/
function sak_system_site_information_settings_submit_handler(&$form,\Drupal\Core\Form\FormStateInterface $form_state ) {
// Get the value from Form State
$site_api_key = $form_state->getValue('site_api_key');
// Set the sakkey value using configuration API.
\Drupal::configFactory()->getEditable('sak.settings')->set('sakkey', $site_api_key)->save();;
// Show a warning message if the user enters and saves an empty API Key value.
if (empty($site_api_key) || $site_api_key === 'No API Key yet' ) {
drupal_set_message(t('You have not set your Site\'s API Key. Please set the API key below and Save Configuration.'), 'warning');
}
// Inform the user that the Site API Key has been saved successfully.
else {
drupal_set_message(t('You have successfully set your Site\'s API Key to @site_api_key .', array('@site_api_key' => $site_api_key)));
}
}