-
Notifications
You must be signed in to change notification settings - Fork 0
/
minutes.module
84 lines (77 loc) · 3.29 KB
/
minutes.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
<?php
use Drupal\Core\Form\FormStateInterface;
use Drupal\node\NodeInterface;
function minutes_form_alter(&$form, FormStateInterface $form_state, $form_id) {
switch ($form_id) {
case "node_minutes_form":
_setup_minutes_entity_builder($form);
break;
case "node_minutes_edit_form":
_hide_immutable_minutes_fields($form, $form_state);
break;
case "node_minutes_delete_form":
_setup_minutes_entity_delete_callback($form);
break;
}
}
function _setup_minutes_entity_builder(&$form) {
$form['title']['#access'] = FALSE;
$form['#entity_builders'][] = '_minutes_node_builder';
}
function _setup_minutes_entity_delete_callback(&$form) {
$form['#validate'][] = '_minutes_delete_validation';
}
function _hide_immutable_minutes_fields(&$form, $form_state) {
// These form the title .. and it's too late to change it now!
$form['title']['#access'] = FALSE;
$form['field_minutes_meeting']['#access'] = FALSE;
// Hide the Minutes text field unless in Draft Status
$minutes_node = $form_state->getFormObject()->getEntity();
$minutes_status_id = $minutes_node->field_minutes_status->target_id;
$referenced_tag = \Drupal::entityTypeManager()
->getStorage('taxonomy_term')
->load($minutes_status_id);
$minutes_status = $referenced_tag->name->value;
if ($minutes_status != 'Draft') {
// We'll prevent agenda text edits unless in Draft status
$form['body']['#access'] = FALSE;
}
}
function _minutes_node_builder($entity_type, NodeInterface $node, &$form, FormStateInterface $form_state) {
// Find the meeting date from the Meeting Node referenced by `field_minutes_meeting`
if ($node->field_minutes_meeting->target_id) {
$referenced_meeting_node = \Drupal::entityTypeManager()
->getStorage('node')
->load($node->field_minutes_meeting->target_id);
$title_of_meeting = 'Minutes for ' . $referenced_meeting_node->getTitle();
$node->setTitle($title_of_meeting);
} else {
$form_state->setErrorByName('field_minutes_meeting', 'Please choose a Meeting that already exists.');
$node->setTitle('Error');
}
} // end of minutes_node_builder
function _minutes_delete_validation(&$form, FormStateInterface $form_state) {
$minutes_node = $form_state->getFormObject()->getEntity();
$minutes_name = $minutes_node->getTitle();
$minutes_node_id = $minutes_node->id();
// Look for any Meetings referencing these Minutes
$query = \Drupal::entityQuery('node')
->accessCheck(TRUE)
->condition('type', 'meeting')
->condition('field_meeting_minutes', $minutes_node_id);
$nids = $query->execute();
$count_nids = count($nids);
if ($count_nids > 0) {
$msg_stem = ($count_nids == 1) ? 'There is a Meeting that references "' : 'There are ' . $count_nids . ' Meetings that reference "';
$form_state->setError($form, $msg_stem . $minutes_name . '". It cannot be deleted yet.');
}
$minutes_status_id = $minutes_node->field_minutes_status->target_id;
$referenced_tag = \Drupal::entityTypeManager()
->getStorage('taxonomy_term')
->load($minutes_status_id);
$minutes_status = $referenced_tag->name->value;
if ($minutes_status != 'Draft') {
// We'll prevent deleting Minutes unless in Draft status
$form_state->setError($form, 'Please change the Minutes "Document status to "Draft" before attempting to delete.');
}
}