-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathagenda.module
152 lines (131 loc) · 5.57 KB
/
agenda.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
<?php
use Drupal\Core\Form\FormStateInterface;
use Drupal\node\NodeInterface;
/**
* Implements HOOK_form_alter() for Agenda nodes
* @param $form
* @param \Drupal\Core\Form\FormStateInterface $form_state
* @param $form_id
*
* agenda_form_alter() deals with Agenda's create, update, and deletion forms
*/
function agenda_form_alter(&$form, FormStateInterface $form_state, $form_id) {
switch ($form_id) {
// Agenda creation form
case "node_agenda_form":
_setup_agenda_entity_builder($form);
break;
// Agenda edit form
case "node_agenda_edit_form":
_prepare_agenda_edit_form($form, $form_state);
break;
// Agenda delete form
case "node_agenda_delete_form":
_setup_agenda_entity_delete_callback($form);
break;
}
}
function _setup_agenda_entity_builder(&$form) {
// We'll supply the Agenda title based on the Meeting it's for
$form['title']['#access'] = FALSE;
// The timestamp it entirely our affair and never shown to the user
$form['field_agenda_time_stamp']['#access'] = FALSE;
// Hide the agenda status from form creation
$form['field_agenda_status']['#access'] = FALSE;
// Now set up the callback
$form['#entity_builders'][] = '_agenda_node_builder';
}
function _prepare_agenda_edit_form(&$form, FormStateInterface $form_state) {
// We won't allow the Meeting to be changed once it's established
$form['field_agenda_meeting']['#access'] = FALSE;
// We'll retain the Agenda title based on the Meeting it's for
$form['title']['#access'] = FALSE;
// The timestamp it entirely our affair and never shown to the user
$form['field_agenda_time_stamp']['#access'] = FALSE;
// Hide the Agenda text field unless in Draft Status
$agenda_node = $form_state->getFormObject()->getEntity();
$agenda_status_id = $agenda_node->field_agenda_status->target_id;
$referenced_tag = \Drupal::entityTypeManager()
->getStorage('taxonomy_term')
->load($agenda_status_id);
$agenda_status = $referenced_tag->name->value;
if ($agenda_status != 'Draft') {
// We'll prevent agenda text edits unless in Draft status
$form['body']['#access'] = FALSE;
}
}
function _setup_agenda_entity_delete_callback(&$form) {
$form['#validate'][] = '_agenda_delete_validation';
}
/**
* _agenda_node_builder is Agenda's entity_builder callback
* It is used to populate / update the Agenda node being created or edited
* prior to it being saved consequent upon form submission
* It does:
* - sets up the Agenda title on behalf of the user
* - records a timestamp for comparison and sorting purposes
* @param $entity_type
* @param \Drupal\node\NodeInterface $node
* @param $form
* @param \Drupal\Core\Form\FormStateInterface $form_state
*/
function _agenda_node_builder($entity_type, NodeInterface $node, &$form, FormStateInterface $form_state) {
// Find the meeting date from the Meeting Node referenced by `field_agenda_meeting`
if ($node->field_agenda_meeting->target_id) {
// Compute Agenda Title
$referenced_meeting_node = \Drupal::entityTypeManager()
->getStorage('node')
->load($node->field_agenda_meeting->target_id);
$date_of_meeting = $referenced_meeting_node->field_meeting_date->date;
$title_of_agenda = 'Agenda for ' . $referenced_meeting_node->getTitle();
$node->setTitle($title_of_agenda);
// Record time stamp in Agenda node
$this_meeting_time_stamp = $date_of_meeting->format('U');
$node->set('field_agenda_time_stamp', $this_meeting_time_stamp);
$query = \Drupal::entityQuery('node')
->accessCheck(TRUE)
->condition('type', 'agenda')
->condition('title', $title_of_agenda);
$nids = $query->execute();
if (count($nids) > 0) {
$form_state->setError($form,
"An " . $title_of_agenda . " has not been created as one already exists. Most likely the Meeting page has not been edited to include its Agenda yet.");
}
}
else {
// Exceptional error - shouldn't happen
$form_state->setErrorByName('field_agenda_meeting', 'Please choose a Meeting that already exists.');
}
} // end of agenda_node_builder
/**
* _agenda_delete_validation is the Agenda's delete validator callback
* @param $form
* @param \Drupal\Core\Form\FormStateInterface $form_state
* It will prevent an Agenda from being deleted when a Meeting references it
*/
function _agenda_delete_validation(&$form, FormStateInterface $form_state) {
// Check whether this Agenda is currently in use by a Meeting
$agenda_node = $form_state->getFormObject()->getEntity();
$agenda_name = $agenda_node->getTitle();
$agenda_node_id = $agenda_node->id();
$query = \Drupal::entityQuery('node')
->accessCheck(TRUE)
->condition('type', 'meeting')
->condition('field_agenda', $agenda_node_id);
$nids = $query->execute();
$count_nids = count($nids); // Used several times
if ($count_nids > 0) {
// Multiple Meetings with same Agenda can (but shouldn't) happen
$msg_stem = ($count_nids == 1) ? 'There is a Meeting that references "' : 'There are ' . $count_nids . ' Meetings that reference "';
$form_state->setError($form, $msg_stem . $agenda_name . '". It cannot be deleted yet until the corresponding Meeting is either edited or deleted.');
}
// The Agenda should be in Draft status to be deleted
$agenda_status_id = $agenda_node->field_agenda_status->target_id;
$referenced_tag = \Drupal::entityTypeManager()
->getStorage('taxonomy_term')
->load($agenda_status_id);
$agenda_status = $referenced_tag->name->value;
if ($agenda_status != 'Draft') {
$form_state->setError($form, 'Please change the Agenda to "Draft" status before attempting to delete.');
}
}