forked from stratoserp/stratoserp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
stratoserp.module
169 lines (151 loc) · 4.55 KB
/
stratoserp.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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
<?php
/**
* @file
* Core functions for StratosERP module file.
*/
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Routing\RouteMatchInterface;
use Drupal\Core\Url;
use Drupal\taxonomy\Entity\Term;
use Drupal\stratoserp\ErpCore;
use Symfony\Component\HttpFoundation\RedirectResponse;
/**
* Implements hook_help().
*
* @param $route_name
* @param \Drupal\Core\Routing\RouteMatchInterface $route_match
*
* @return \Drupal\Core\StringTranslation\TranslatableMarkup
*/
function stratoserp_help($route_name, RouteMatchInterface $route_match) {
if ($route_name === 'stratoserp_main') {
return t('Main console page.');
}
return NULL;
}
/**
* Change the node buttons to be what we want.
*
* Implements hook_form_alter().
*
* @todo Needs review since moving to Entities.
*
* @param $form
* @param \Drupal\Core\Form\FormStateInterface $form_state
* @param $form_id
*/
function stratoserp_form_alter(&$form, FormStateInterface $form_state, $form_id) {
// Make a list of forms from the core types.
$se_forms = array_map(static function ($value) {
return $value . '_add_form';
}, ErpCore::SE_ENTITY_TYPES);
if (in_array($form_id, $se_forms, TRUE)) {
// We don't need the publish button, anywhere.
stratoserp_hide_publish_buttons($form);
// Hide the title fields for some node types.
if (!in_array($form_id, [
'se_business_add_form', 'se_contact_add_form', 'se_ticket_add_form',
])) {
// Pre-fill the title field with sensible values.
stratoserp_populate_title_field($form);
}
}
// If using the ExtraField Form plugins, then some fields need to be removed.
// unset($form['meta'], $form['advanced'], $form['status'], $form['actions']);.
// If this is something being deleted, redirect back to the frontpage.
if (preg_match('/(.*?)_delete_form/', $form_id)) {
$form['actions']['submit']['#submit'][] = 'stratoserp_entity_delete_redirect_submit';
}
}
/**
* Redirect delete submissions to the frontpage.
*
* @todo Make customisable/redirect back to business?
*
* @param array $form
* @param \Drupal\Core\Form\FormStateInterface $form_state
*/
function stratoserp_entity_delete_redirect_submit(array &$form, FormStateInterface $form_state) {
$form_state->setRedirectUrl(Url::fromRoute('<front>'));
}
/**
* Helper function to hide the publish buttons.
*
* @param array $form
* Form array to be manipulated.
*/
function stratoserp_hide_publish_buttons(array &$form) {
// This should work.
$form['actions']['unpublish']['#access'] = FALSE;
$form['actions']['publish']['#access'] = FALSE;
// But it doesn't. Sledge hammer time.
unset($form['actions']['unpublish'], $form['actions']['publish']);
// Ensure that we can 'Save' as well.
$form['actions']['submit']['#access'] = TRUE;
}
/**
* Nodes are handy, but we don't really need the title field.
*
* @param array $form
* Form array to be manipulated.
*/
function stratoserp_populate_title_field(array &$form) {
// $form['title']['#hidden'] = TRUE;
$form['title']['widget'][0]['value']['#default_value'] = \Drupal::service('se.form_alter')->generateTitle();
}
/**
* Implements hook_form_FORM_ID_alter().
*
* @param $form
* @param \Drupal\Core\Form\FormStateInterface $form_state
* @param $form_id
*/
function stratoserp_form_user_login_form_alter(&$form, FormStateInterface $form_state, $form_id) {
// Alter login form and add own custom submit handler.
$form['#submit'][] = '_stratoserp_user_login_form_submit';
}
/**
* Custom submit handler for login form.
*
* @param $form
* @param \Drupal\Core\Form\FormStateInterface $form_state
*/
function _stratoserp_user_login_form_submit($form, FormStateInterface $form_state) {
$form_state->setRedirect('<front>');
}
/**
* I don't think we're using this.
*/
function stratoserp_theme() {
return [
'stratoserp_admin_page' => [
'variables' => [
'counters' => [],
'general_info' => [],
'requirements' => NULL,
],
],
];
}
/**
* Modify field types for which remove button will be added.
* See multiple_fields_remove_button.api.php.
*
* @param array $fieldTypes
* A list with field types.
*/
function stratoserp_multiple_field_remove_button_field_types_alter(array &$fieldTypes) {
$fieldTypes[] = 'se_item_line';
$fieldTypes[] = 'se_payment_line';
}
/**
* Implements hook_toolbar_alter().
*
* See: https://www.drupal.org/project/toolbar_menu/issues/2658470
*/
function stratoserp_toolbar_alter(&$items) {
$account = Drupal::currentUser();
if (!in_array('administrator', $account->getRoles())) {
unset($items['administration']);
}
}