-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathupdated.module
119 lines (110 loc) · 3.79 KB
/
updated.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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
<?php
/**
* @file
* Displays the last updated information for a node.
*/
/**
* Implements hook_config_info().
*/
function updated_config_info() {
$prefixes['updated.settings'] = [
'label' => t('Updated settings'),
'group' => t('Configuration'),
];
return $prefixes;
}
/**
* Implements hook_form_BASE_FORM_alter().
*/
function updated_form_node_type_form_alter(&$form, $form_values) {
$configuration = config('updated.settings');
$settings = $configuration->get('node_type_display_toggle');
$checkbox_state = $settings[$form['#node_type']->type] ?? TRUE;
$form['display']['node_updated'] = array(
'#type' => 'checkbox',
'#title' => t('Display last updated date.'),
'#default_value' => $checkbox_state,
'#description' => t('Last updated date will be displayed along with published date.'),
'#states' => array(
'visible' => array( // action to take.
':input[name=node_submitted]' => array('checked' => TRUE),
),
),
);
// Extend default submit method to involve configuration system
$form['#submit'][] = 'updated_form_node_type_form_submit';
}
/**
* Implements hook_form_BASE_FORM_submit().
*/
function updated_form_node_type_form_submit($form, &$form_state) {
$settings = config_get('updated.settings', 'node_type_display_toggle');
$settings[$form['#node_type']->type] = (int) $form_state['values']['node_updated'];
config_set('updated.settings', 'node_type_display_toggle', $settings, TRUE);
}
/**
* Implements hook_form_BASE_FORM_alter().
*/
function updated_form_node_form_alter(&$form, $form_values) {
$configuration = config('updated.settings');
$settings = $configuration->get('node_type_display_toggle');
$checkbox_state = $settings[$form['#node']->type] ?? TRUE;
if ($checkbox_state == TRUE) {
// Add the updated date so authors can see it here.
if (property_exists($form['#node'], 'changed')) {
$form['author']['updated_value'] = array(
'#type' => 'textfield',
'#title' => t('Last updated'),
'#value' => format_date($form['#node']->changed),
'#disabled' => TRUE,
);
}
if (property_exists($form['#node'], 'show_updated')) {
$form['options']['show_updated'] = array(
'#type' => 'checkbox',
'#title' => t('Display updated date'),
'#default_value' => $form['#node']->show_updated,
);
}
}
}
/**
* Implements hook_node_validate().
*/
function updated_node_validate($node, $form, &$form_state) {
if (!empty($form_state['values']['updated_value'])) {
// Remove the updated value added for editors to view.
unset($form_state['values']['updated_value']);
}
}
/**
* Implements hook_schema_alter().
*/
function updated_schema_alter(&$schema) {
// Add show_updated field to existing node schema.
$schema['node']['fields']['show_updated'] = array(
'description' => 'Boolean indicating whether the node display the updated date in addition to the created date.',
'type' => 'int',
'not null' => TRUE,
'default' => 0,
);
// Add show_updated field to existing node_revision schema.
$schema['node_revision']['fields']['show_updated'] = array(
'description' => 'Boolean indicating whether the node display the updated date in addition to the created date.',
'type' => 'int',
'not null' => TRUE,
'default' => 0,
);
}
/**
* Implements hook_preprocess_node().
*/
function updated_preprocess_node(&$variables) {
$configuration = config('updated.settings');
$settings = $configuration->get('node_type_display_toggle');
$checkbox_state = $settings[$variables['node']->type] ?? TRUE;
if ($checkbox_state == TRUE && $variables['node']->show_updated) {
$date = format_date($variables['node']->changed);
$variables['submitted'] .= ' • <span>' . t('Updated on !date', array('!date' => $date)) . '</span>';
}
}