-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathopeny_node_alert.module
118 lines (102 loc) · 3.32 KB
/
openy_node_alert.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
<?php
/**
* @file
* Open Y Node Alert module file.
*/
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Cache\Cache;
use Drupal\Core\Form\FormStateInterface;
/**
* Implements hook_preprocess_node().
*/
function openy_node_alert_preprocess_page(&$variables) {
$node = \Drupal::routeMatch()->getParameter('node');
if ($node instanceof \Drupal\node\NodeInterface) {
[$sendAlerts, $alerts] = \Drupal::service('openy_node_alert.alert_manager')->getAlerts($node);
if ($sendAlerts) {
$variables['#attached']['library'][] = 'openy_node_alert/alert';
}
}
}
/**
* Implements hook_entity_presave().
*/
function openy_node_alert_entity_presave(EntityInterface $entity) {
// Check that we are saving Alert node.
if (
($entity->getEntityTypeId() == 'node')
&& ($entity->bundle() == 'alert')
) {
if (
$entity->hasField('field_alert_visibility_pages')
&& !$entity->get('field_alert_visibility_pages')->isEmpty()
) {
// Trim any leading or trailing spaces as they can cause false positives.
$visibility_paths = trim($entity->get('field_alert_visibility_pages')->value);
$entity->field_alert_visibility_pages->value = $visibility_paths;
// Convert path to lowercase. This allows comparison of the same path.
// with different case. Ex: /Page, /page, /PAGE.
$visibility_paths = mb_strtolower($visibility_paths);
$pages = preg_split("(\r\n?|\n)", $visibility_paths);
$path_matcher = \Drupal::service('path_alias.manager');
$cacheTags = [];
foreach ($pages as $page) {
$canonical_path = $path_matcher->getPathByAlias($page);
// Check if this path is a node path.
if (strpos($canonical_path, 'node') !== FALSE) {
$nid = explode('/', $canonical_path)[2];
$cacheTags[] = 'node:' . $nid;
}
}
}
// Collecting nodes from the Reference field.
if (
$entity->hasField('field_alert_belongs')
&& !$entity->get('field_alert_belongs')->isEmpty()
) {
$references = $entity->get('field_alert_belongs')->getValue();
foreach ($references as $reference) {
// Reference field is referenced to nodes, using this node tag.
$cacheTags[] = 'node:' . $reference['target_id'];
}
}
if (!empty($cacheTags)) {
Cache::invalidateTags($cacheTags);
}
}
}
/**
* Implements hook_form_FORM_ID_alter().
*/
function openy_node_alert_form_node_alert_edit_form_alter(array &$form, FormStateInterface $form_state, $form_id): void {
_openy_node_alert_alter_node_alert_form($form);
}
/**
* Implements hook_form_FORM_ID_alter().
*/
function openy_node_alert_form_node_alert_form_alter(array &$form, FormStateInterface $form_state, $form_id): void {
_openy_node_alert_alter_node_alert_form($form);
}
/**
* Adds '#states' functionality to the node form.
*
* @param $form
* An object of the node form.
*
* @return void
*/
function _openy_node_alert_alter_node_alert_form(&$form): void {
// Show these fields only for the classic style option.
$fields_to_toggle = [
'field_alert_color',
'field_alert_text_color',
'field_alert_icon_color',
];
foreach ($fields_to_toggle as $field_name) {
$form[$field_name]['widget']['#states'] = [
'visible' => [
':input[name="field_alert_style"]' => ['value' => 'classic'],
],
];
}
}