forked from stratoserp/stratoserp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
stratoserp.install
99 lines (86 loc) · 2.55 KB
/
stratoserp.install
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
<?php
/**
* @file
*/
use Drupal\taxonomy\Entity\Term;
/**
* Implements hook_install().
*
* If the toolbar menu module is installed, add a menu.
*
* @throws \Drupal\Core\Entity\EntityStorageException
*/
function stratoserp_install() {
try {
// If the toolbar_menu module exists, enable it.
if (\Drupal::service('extension.list.module')
->getName('toolbar_menu')) {
\Drupal::service('module_installer')
->install(['toolbar_menu'], FALSE);
}
// If its now enabled, create a toolbar menu item.
if (\Drupal::moduleHandler()->moduleExists('toolbar_menu')) {
$entity_manager = \Drupal::service('entity_type.manager');
$entity_manager->getStorage('toolbar_menu_element')->create([
'id' => 'stratoserp',
'label' => 'StratosERP',
'menu' => 'stratos-erp',
'weight' => NULL,
'rewrite_label' => TRUE,
])->save();
}
}
catch (Exception $e) {
}
// Status for quotes, purchase orders, invoices.
$terms = [
'Ordered',
'Closed',
'Open',
];
$term = stratoserp_create_terms('se_status',$terms);
}
/**
* Helper function for checking/adding taxonomy terms during setup.
*
* @param string $taxonomy
* Taxonomy vocabulary to check/add.
* @param array $terms
* Array of terms to check/add.
*
* @return \Drupal\taxonomy\Entity\Term|bool
* The last created term, or false if none were created.
*
* @throws \Drupal\Core\Entity\EntityStorageException
*/
function stratoserp_create_terms(string $taxonomy, array $terms) {
$term = FALSE;
// The last one will be used as the default.
foreach ($terms as $new_term) {
$find_term = taxonomy_term_load_multiple_by_name($new_term, 'se_contact_type');
if (empty($find_term)) {
/** @var \Drupal\taxonomy\Entity\Term $term */
$term = Term::create([
'parent' => [],
'name' => $new_term,
'vid' => $taxonomy,
]);
$term->save();
}
}
// Return the last term created.
return $term;
}
/**
* Helper function for term setup.
*/
function stratoserp_set_term_field_config(string $search_term, string $vocabulary, string $field): \Drupal\Core\Entity\EntityInterface {
// Load the already created default status terms.
$find_term = taxonomy_term_load_multiple_by_name($search_term, $vocabulary);
$term = reset($find_term);
// Now set the last one as the default for the field.
$field = \Drupal::entityTypeManager()->getStorage('field_config')->load($field);
$field->setDefaultValue(['target_uuid' => $term->uuid()]);
$field->save();
return $term;
}