-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathservice_request.module
107 lines (94 loc) · 3.39 KB
/
service_request.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
<?php
/**
* @file
* Contains service_request.module.
*
* This is a content type to run an georeport v2 server.
*/
use Drupal\Core\Entity\EntityInterface;
use Drupal\paragraphs\Entity\Paragraph;
/**
* Implements hook_entity_presave().
*/
function service_request_entity_presave(EntityInterface $entity) {
// Service Request Creation.
if ($entity->bundle() == 'service_request' && !$entity->id()) {
// Get some relevant config.
$config = \Drupal::configFactory()
->getEditable('markaspot_open311.settings');
// Add an intitial paragraph on post
// Status when inserting.
$status_open_start = $config->get('status_open_start');
// todo: put this in config.
$status_note_initial = t('The service request has been created.');
$paragraph = Paragraph::create([
'type' => 'status',
'field_status_note' => array(
"value" => $status_note_initial,
"format" => "full_html",
),
'field_status_term' => array(
"target_id" => $status_open_start,
),
]);
$paragraph->save();
$entity->field_status_notes = array(
array(
'target_id' => $paragraph->id(),
'target_revision_id' => $paragraph->getRevisionId(),
),
);
}
// Service Request Update
elseif ($entity->bundle() == 'service_request' && $entity->id()) {
foreach ($entity->field_status_notes as $paragraph) {
// Access the paragraph entity like this:
$statusNotes[] = $paragraph->entity;
}
// Get new Status from Taxonomy Reference.
$newStatus = $entity->field_status->target_id;
// Load last Paragraph and Update the Status there.
$paragraph = Paragraph::load(end($statusNotes)->Id());
$paragraph->field_status_term->target_id = $newStatus;
// Save paragraph.
$paragraph->save();
}
}
/**
* Implements hook_form_alter().
*
* Perform alterations before a form is rendered.
*
* One popular use of this hook is to add form elements to the node form. When
* altering a node form, the node object can be accessed at $form['#node'].
*
* Note that instead of hook_form_alter(), which is called for all forms, you
* can also use hook_form_FORM_ID_alter() to alter a specific form. For each
* module (in system weight order) the general form alter hook implementation
* is invoked first, then the form ID specific alter implementation is called.
* After all module hook implementations are invoked, the hook_form_alter()
* implementations from themes are invoked in the same manner.
*
* @param $form
* Nested array of form elements that comprise the form.
* @param $form_state
* A keyed array containing the current state of the form. The arguments
* that drupal_get_form() was originally called with are available in the
* array $form_state['build_info']['args'].
* @param $form_id
* String representing the name of the form itself. Typically this is the
* name of the function that generated the form.
*
* @see hook_form_FORM_ID_alter()
*
*
*/
function service_request_form_alter(&$form, &$form_state, $form_id) {
// $status_widget = $form['field_status_notes']['widget'];
// todo:
// Disable paragraph subform if subform hooks are stable:
// https://www.drupal.org/project/paragraphs/issues/2868155
$form['#attached']['library'][] = 'service_request/paragraphs';
$form['#attached']['drupalSettings']['service_request']['paragraphs']['variable'] = 'value';
return $form;
}