-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsingle_node_form_alerts.module
63 lines (54 loc) · 1.99 KB
/
single_node_form_alerts.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
<?php
/**
* @file
* Contains single_node_form_alerts.module.
*/
use Drupal\Core\Routing\RouteMatchInterface;
use Drupal\Core\Form\FormStateInterface;
/**
* Implements hook_help().
*/
function single_node_form_alerts_help($route_name, RouteMatchInterface $route_match) {
switch ($route_name) {
// Main module help for the single_node_form_alerts module.
case 'help.page.single_node_form_alerts':
$output = '';
$output .= '<h3>' . t('About') . '</h3>';
$output .= '<p>' . t('Provide alerts on node forms specific to a single node') . '</p>';
return $output;
default:
}
}
/**
* Implements hook_form_BASE_FORM_ID_alter().
*/
function single_node_form_alerts_form_node_form_alter(&$form, FormStateInterface $form_state, $form_id)
{
$node = $form_state->getFormObject()->getEntity();
$message_config = \Drupal::state()->get('single_node_form_alerts.node_' . $node->id(), []);
$form['#attached']['library'][] = 'single_node_form_alerts/node_form_alerts';
if (!empty($message_config['top_message'])) {
$msg = !empty($form['#prefix']) ? $form['#prefix'] : '';
$form['#prefix'] = $msg.format_node_form_message($message_config['top_message']);
}
if (!empty($message_config['bottom_message'])) {
$msg = !empty($form['actions']['#prefix']) ? $form['actions']['#prefix'] : '';
$form['actions']['#prefix'] = format_node_form_message($message_config['bottom_message']) . $msg;
}
foreach ($message_config as $k => $v) {
$key = str_replace('_message', '', $k);
if (isset($form[$key])) {
$msg = !empty($form[$key]['#prefix']) ? $form[$key]['#prefix'] : '';
$form[$key]['#prefix'] = $msg . format_node_form_message($v);
}
}
}
/**
* @param string $message
* The saved message for the field.
* @return string
* It is wrapped in div to show as a very visible block of text.
*/
function format_node_form_message($message) {
return sprintf('<div class="single-node-form-message">%s</div>', strip_tags($message, '<p><a><b><i><br>'));
}