-
Notifications
You must be signed in to change notification settings - Fork 1
/
blellow_helper.module
165 lines (154 loc) · 4.23 KB
/
blellow_helper.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
<?php
/**
* @file
* Blellow Corporate Blocks module.
*/
use Drupal\Core\Entity\ContentEntityInterface;
/**
* Implements hook_theme().
*/
function blellow_helper_theme() {
return [
'cnect_corporate_blocks_login' => [
'variables' => [
'corporate_login' => [],
],
],
'cnect_corporate_blocks_register' => [
'variables' => [
'corporate_register' => [],
],
],
'cnect_corporate_blocks_footer' => [
'variables' => [
'corporate_footer' => [],
'custom_footer' => [],
],
],
'cnect_corporate_blocks_newsletter' => [
'variables' => [
'corporate_newsletter' => [],
],
],
'cnect_corporate_blocks_select_languages' => [
'variables' => [
'links' => [],
'languages' => [],
]
],
'cnect_corporate_blocks_welcome' => [
'variables' => [
'corporate_welcome' => []
]
],
'cnect_corporate_blocks_join_futurium' => [
'variables' => [
'corporate_welcome' => []
]
],
'group_page_title_block' => [
'variables' => [
'title' => NULL
],
],
];
}
/**
* Implements hook_preprocess_menu__account().
*/
function blellow_helper_preprocess_menu__account(&$variables) {
$items = $variables['items'];
foreach ($items as $key => &$item) {
// Hide register link.
if ($item['url']->getRouteName() == 'user.register' && Drupal::currentUser()->isAuthenticated()) {
unset($variables['items'][$key]);
}
}
}
/**
* Implements hook_preprocess().
*/
function blellow_helper_preprocess(&$variables) {
}
/**
* Implements hook_themes_installed().
*/
function blellow_helper_themes_installed($theme_list) {
// Flush plugins cache to catch new patterns defined by the theme.
$container = \Drupal::getContainer();
$uiPatternsManager = $container->get('plugin.manager.ui_patterns');
$uiPatternsManager->clearCachedDefinitions();
}
/**
* Implements hook_preprocess_cnect_corporate_blocks_select_languages().
*/
function blellow_helper_preprocess_cnect_corporate_blocks_select_languages(array &$variables): void {
template_preprocess_links($variables);
}
/**
* Implements hook_preprocess_html().
*/
function blellow_helper_preprocess_html(&$variables) {
$map = [
'Drupal\group\Entity\Group' => 'path-group-home',
'parent//Drupal\group\Entity\Group' => 'has-group-parent'
];
$entity = _blellow_helper_get_route_entity();
$parent = null;
if ($entity instanceof ContentEntityInterface) {
$parent = _blellow_helper_parent_group($entity);
}
$className[] = $entity ? get_class($entity) : null;
$className[] = $parent ? 'parent//' . get_class($parent) : null;
foreach ($className as $item) {
if(isset($map[$item])) {
$classCSS[] = $map[$item];
}
}
$variables['attributes']['class'][] = isset($classCSS) ? implode(' ', $classCSS) : '';
}
/**
* Return the current entity of a canonical route or null in any other case.
*
* @return mixed|null
*/
function _blellow_helper_get_route_entity() {
$re = '/^entity\..*\.canonical$/m';
$route_match = \Drupal::routeMatch();
$is_canonical = preg_match_all($re, $route_match->getRouteName());
if(!$is_canonical) {
return NULL;
}
// Entity will be found in the route parameters.
if (($route = $route_match->getRouteObject()) && ($parameters = $route->getOption('parameters'))) {
// Determine if the current route represents an entity.
foreach ($parameters as $name => $options) {
if (isset($options['type']) && strpos($options['type'], 'entity:') === 0) {
$entity = $route_match->getParameter($name);
if ($entity->hasLinkTemplate('canonical')) {
return $entity;
}
// Since entity was found, no need to iterate further.
return NULL;
}
}
}
}
/**
* Resolves the parent group of an entity, if any.
*
* @param ContentEntityInterface|null $entity
* @return \Drupal\group\Entity\Group|null
*/
function _blellow_helper_parent_group(ContentEntityInterface $entity = null) {
if (!$entity) {
return null;
}
$content = \Drupal\group\Entity\GroupContent::loadByEntity($entity);
$content = current($content) ?? null;
if (!!$content) {
$group = $content->getGroup();
return $group;
}
return null;
}