-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathajax_alter_title.module
179 lines (164 loc) · 5.24 KB
/
ajax_alter_title.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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
<?php
/**
* @file
* Primary module hooks for Ajax alter title module.
*/
use Drupal\Core\Ajax\AjaxResponse;
use Drupal\Core\Ajax\InvokeCommand;
use Drupal\Core\Ajax\HtmlCommand;
use Drupal\Core\Routing\RouteMatchInterface;
/**
* Implements hook_help().
*/
function ajax_alter_title_help($route_name, RouteMatchInterface $route_match) {
switch ($route_name) {
case 'help.page.ajax_alter_title':
$output = '';
$output .= '<h3>' . t('About') . '</h3>';
$output .= '<p>' . t('This custom module is a great option for a Drupal website to announce concerts by using nodes.') . '</p>';
$output .= '<p>' . t('Essentially, "ajax_alter_title" allows you to add the status of the concert to the title of its node.') . '</p>';
$output .= '<p>' . t('The status can be "canceled", "postponed" or "sold out".') . '</p>';
return $output;
default:
}
}
/**
* Implements hook_form_FORM_ID_alter().
*/
function ajax_alter_title_form_node_page_edit_form_alter(&$form, $form_state, $form_id) {
$form['canceled_title'] = [
'#type' => 'button',
'#value' => t('Add "CANCELED" prefix'),
'#arg' => 'CANCELED',
'#ajax' => [
'callback' => 'ajax_alter_title_prefix',
'wrapper' => 'edit-title-0-value',
'event' => 'click',
'progress' => [
'type' => 'throbber',
'message' => 'Updating title...'
]
]
];
$form['postponed_title'] = [
'#type' => 'button',
'#value' => t('Add "POSTPONED" prefix'),
'#arg' => 'POSTPONED',
'#ajax' => [
'callback' => 'ajax_alter_title_prefix',
'wrapper' => 'edit-title-0-value',
'event' => 'click',
'progress' => [
'type' => 'throbber',
'message' => 'Updating title...'
]
]
];
$form['sold_out_title'] = [
'#type' => 'button',
'#value' => t('Add "SOLD OUT" prefix'),
'#arg' => 'SOLD OUT',
'#ajax' => [
'callback' => 'ajax_alter_title_prefix',
'wrapper' => 'edit-title-0-value',
'event' => 'click',
'progress' => [
'type' => 'throbber',
'message' => 'Updating title...'
]
]
];
$form['delete_title'] = [
'#type' => 'button',
'#value' => t('Delete current prefix'),
'#arg' => 'delete',
'#ajax' => [
'callback' => 'ajax_alter_title_prefix',
'wrapper' => 'edit-title-0-value',
'event' => 'click',
'progress' => [
'type' => 'throbber',
'message' => 'Deleting preffix...'
]
]
];
$form['message_title'] = [
'#type' => 'markup',
'#markup' => '<div id="message-title"></div>',
];
}
/**
* AJAX callback to add or delete prefixes.
*
* @param array &$form
* The form structure.
* @param FormStateInterface &$form_state
* The current state of the form.
*
* @return \Drupal\Core\Ajax\AjaxResponse
* An AJAX response that updates the title field and displays a status message.
*/
function ajax_alter_title_prefix(&$form, &$form_state) {
$response = new AjaxResponse();
$current_title = $form['title']['widget'][0]['value']['#default_value'];
$prefix = $form_state->getTriggeringElement()['#arg'];
$default_options = ['CANCELED', 'POSTPONED', 'SOLD OUT'];
if (in_array($prefix, $default_options)) {
$parameters = ajax_alter_title_get_parameters($current_title, $prefix);
}
else {
$parameters = ajax_alter_title_get_parameters_delete($current_title);
}
$response->addCommand(new InvokeCommand('#edit-title-0-value', 'val', [$parameters[0]]));
$response->addCommand(new HtmlCommand('#message-title', $parameters[1]));
return $response;
}
/**
* Gets new title and message.
*
* @param string $current_title
* The current title of the node.
* @param string $prefix
* The prefix to be added to the title.
*
* @return array
* An array containing:
* - The modified title (string).
* - A message indicating the status of the operation (string).
*/
function ajax_alter_title_get_parameters($current_title, $prefix) {
if (!str_starts_with($current_title, $prefix)) {
$current_title = preg_replace('/^(CANCELED \/|POSTPONED \/|SOLD OUT \/)\s*/', '', $current_title);
$new_title = $prefix . ' / ' . $current_title;
$message = '<p style="color:red;">"' . $prefix . '" has been added. Please, <b>save this node</b></p>';
}
else {
$new_title = $current_title;
$message = '<p style="color:red;">"' . $prefix . '" is already in the original title.</p>';
}
$result = [$new_title, $message];
return $result;
}
/**
* Gets new title and message when deleting any prefix.
*
* @param string $current_title
* The current title of the node.
*
* @return array
* An array containing:
* - The modified title (string).
* - A message indicating whether a prefix was removed or no change was needed (string).
*/
function ajax_alter_title_get_parameters_delete($current_title) {
if (preg_match('/^(CANCELED|POSTPONED|SOLD OUT)/', $current_title)) {
$new_title = preg_replace('/^(CANCELED \/|POSTPONED \/|SOLD OUT \/)\s*/', '', $current_title);
$message = '<p style="color:red;">Prefix has been deleted. Please, <b>save this node</b></p>';
}
else {
$new_title = $current_title;
$message = '<p style="color:red;">There is no prefix in the original title.</p>';
}
$result = [$new_title, $message];
return $result;
}