-
Notifications
You must be signed in to change notification settings - Fork 0
/
meeting.module
62 lines (54 loc) · 1.86 KB
/
meeting.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
<?php
use Drupal\Core\Form\FormStateInterface;
use Drupal\node\NodeInterface;
/**
* Implements HOOK_form_BASE_FORM_ID_alter
* for Meeting Node forms
* @param $form
* @param \Drupal\Core\Form\FormStateInterface $form_state
* @param $form_id
*/
function meeting_form_node_form_alter(&$form, FormStateInterface $form_state, $form_id) {
switch ($form_id) {
// Node creation form
case "node_meeting_form":
_setup_meeting_entity_builder($form);
break;
// Node edit form
case "node_meeting_edit_form":
_hide_immutable_fields($form);
break;
}
}
function _setup_meeting_entity_builder(&$form) {
$form['title']['#access'] = FALSE;
$form['#entity_builders'][] = 'meeting_node_builder';
}
function _hide_immutable_fields(&$form) {
// These form the title .. so it's too late to change it now!
$form['title']['#access'] = FALSE;
$form['field_meeting_date']['#access'] = FALSE;
$form['field_meeting_type']['#access'] = FALSE;
}
/**
* entity_builder for new and changed Meeting Nodes
* @param $entity_type
* @param NodeInterface $node
* @param $form
* @param FormStateInterface $form_state
*/
function meeting_node_builder($entity_type, NodeInterface $node, &$form, FormStateInterface $form_state) {
// Construct the title from the type and date of the meeting
$date_of_meeting = $node->field_meeting_date->date;
$type_of_meeting_id = $form_state->getValue('field_meeting_type')[0]['target_id'];
$referenced_tag = \Drupal::entityTypeManager()
->getStorage('taxonomy_term')
->load($type_of_meeting_id);
$name_of_meeting = $referenced_tag->name->value;
if ($date_of_meeting instanceof Drupal\Core\Datetime\DrupalDateTime) {
$node->setTitle($name_of_meeting . ' on ' . $date_of_meeting->format('l, j F Y'));
}
else {
$form_state->setErrorByName('field_meeting_date', "Oops! I didn't understand that date.");
}
}