-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbt_crm.module
166 lines (152 loc) · 5.83 KB
/
bt_crm.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
<?php
/**
* @file
* Module file for bt_crm.
*/
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Render\Element;
use Drupal\path_alias\Entity\PathAlias;
/**
* Implements hook_theme().
*/
function bt_crm_theme() {
return [
'bt_opportunity' => [
'render element' => 'elements',
],
];
}
/**
* Prepares variables for opportunity templates.
*
* Default template: bt-opportunity.html.twig.
*
* @param array $variables
* An associative array containing:
* - elements: An associative array containing the opportunity
* information and any fields attached to the entity.
* - attributes: HTML attributes for the containing element.
*/
function template_preprocess_bt_opportunity(array &$variables) {
foreach (Element::children($variables['elements']) as $key) {
$variables['content'][$key] = $variables['elements'][$key];
}
}
/**
* Implements hook_entity_type_alter().
*/
function bt_crm_entity_type_alter(array &$entity_types) {
if (array_key_exists('redhen_contact', $entity_types)) {
$entity_types['redhen_contact']->setFormClass('delete', 'Drupal\bt_crm\Form\ContactsDeleteForm');
$entity_types['redhen_contact']->setHandlerClass('views_data', 'Drupal\bt_crm\ContactViewsData');
$entity_types['redhen_contact']->setLinkTemplate('convert-to-client', "/app/contacts/persons/{redhen_contact}/convert-to-client");
}
if (array_key_exists('redhen_org', $entity_types)) {
$entity_types['redhen_org']->setFormClass('delete', 'Drupal\bt_crm\Form\OrganizationsDeleteForm');
}
if (array_key_exists('redhen_contact_type', $entity_types)) {
$entity_types['redhen_contact_type']->setHandlerClass('access', 'Drupal\bt_crm\RedhenContactTypeAccessControlHandler');
}
if (array_key_exists('redhen_org_type', $entity_types)) {
$entity_types['redhen_org_type']->setHandlerClass('access', 'Drupal\bt_crm\RedhenOrgTypeAccessControlHandler');
}
}
/**
* Implements hook_ENTITY_TYPE_insert().
*/
function bt_crm_redhen_org_insert(EntityInterface $entity) {
// Convert to lowercase, trim spaces,
// replace spaces with underscore and then truncate to 60 chars.
$pathTitle = substr(str_replace(" ", "-", trim(strtolower($entity->label()))), 0, 60);
$bundle = $entity->bundle();
if ($bundle == 'bt_supplier' || $bundle == 'bt_company') {
$bundle_name = [
'bt_supplier' => [
'en' => 'supplier',
'es' => 'proveedor',
],
'bt_company' => [
'en' => 'company',
'es' => 'empresa',
],
];
$en_aliases = [
"/app/contacts/organization/" . $entity->id() => "/app/contacts/organization/" . $bundle_name[$bundle]['en'] . "/" . $pathTitle,
"/app/contacts/organization/" . $entity->id() . "/edit" => "/app/contacts/organization/" . $bundle_name[$bundle]['en'] . "/" . $entity->id() . "/edit",
"/app/contacts/organization/" . $entity->id() . "/delete" => "/app/contacts/organization/" . $bundle_name[$bundle]['en'] . "/" . $entity->id() . "/delete",
];
foreach ($en_aliases as $path => $alias) {
$path_alias = PathAlias::create([
'path' => $path,
'alias' => $alias,
'langcode' => 'en',
]);
$path_alias->save();
}
$es_aliases = [
"/app/contacts/organization/" . $entity->id() => "/app/contactos/organizacion/" . $bundle_name[$bundle]['es'] . "/" . $pathTitle,
"/app/contacts/organization/" . $entity->id() . "/edit" => "/app/contactos/organizacion/" . $bundle_name[$bundle]['es'] . "/" . $entity->id() . "/editar",
"/app/contacts/organization/" . $entity->id() . "/delete" => "/app/contactos/organizacion/" . $bundle_name[$bundle]['es'] . "/" . $entity->id() . "/eliminar",
];
foreach ($es_aliases as $path => $alias) {
$path_alias = PathAlias::create([
'path' => $path,
'alias' => $alias,
'langcode' => 'es',
]);
$path_alias->save();
}
}
else {
$en_aliases = [
"/app/contacts/organization/" . $entity->id() => "/app/contacts/organization/" . $bundle . "/" . $pathTitle,
"/app/contacts/organization/" . $entity->id() . "/edit" => "/app/contacts/organization/" . $bundle . "/" . $entity->id() . "/edit",
"/app/contacts/organization/" . $entity->id() . "/delete" => "/app/contacts/organization/" . $bundle . "/" . $entity->id() . "/delete",
];
foreach ($en_aliases as $path => $alias) {
$path_alias = PathAlias::create([
'path' => $path,
'alias' => $alias,
'langcode' => 'en',
]);
$path_alias->save();
}
$es_aliases = [
"/app/contacts/organization/" . $entity->id() => "/app/contactos/organizacion/" . $bundle . "/" . $pathTitle,
"/app/contacts/organization/" . $entity->id() . "/edit" => "/app/contactos/organizacion/" . $bundle . "/" . $entity->id() . "/editar",
"/app/contacts/organization/" . $entity->id() . "/delete" => "/app/contactos/organizacion/" . $bundle . "/" . $entity->id() . "/eliminar",
];
foreach ($es_aliases as $path => $alias) {
$path_alias = PathAlias::create([
'path' => $path,
'alias' => $alias,
'langcode' => 'es',
]);
$path_alias->save();
}
}
}
/**
* Declares entity operations.
*
* @param \Drupal\Core\Entity\EntityInterface $entity
* The entity on which the linked operations will be performed.
*
* @return array
* An operations array as returned by
* EntityListBuilderInterface::getOperations().
*
* @see \Drupal\Core\Entity\EntityListBuilderInterface::getOperations()
*/
function bt_crm_entity_operation(EntityInterface $entity) {
if ($entity->getEntityTypeId() == 'redhen_contact' && $entity->bundle() == 'bt_potential_client' && $entity->hasLinkTemplate('convert-to-client')) {
return [
'convert-to-client' => [
'title' => t('Convert to client'),
'weight' => -50,
'url' => $entity->toUrl('convert-to-client'),
],
];
}
return [];
}