-
Notifications
You must be signed in to change notification settings - Fork 1
/
kgaut_tools.module
executable file
·172 lines (158 loc) · 5.93 KB
/
kgaut_tools.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
170
171
172
<?php
/**
* @file
* Contains kgaut_tools.module.
*/
use Drupal\Core\Routing\RouteMatchInterface;
use Drupal\image\Entity\ImageStyle;
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\StreamWrapper\PublicStream;
use Drupal\file\FileInterface;
use Drupal\Core\Url;
/**
* Implements hook_help().
*/
function kgaut_tools_help($route_name, RouteMatchInterface $route_match) {
switch ($route_name) {
// Main module help for the kgaut_tools module.
case 'help.page.kgaut_tools':
$output = '';
$output .= '<h3>' . t('About') . '</h3>';
$output .= '<p>' . t('Tools and services for drupal 8') . '</p>';
return $output;
default:
}
}
/**
* Implements hook_entity_insert().
*
* Generate all image styles once an Image is uploaded.
* Tip by flocondetoile.
*
* @see http://flocondetoile.fr/blog/generer-des-styles-d-images-automatiquement-avec-drupal-8
*/
function kgaut_tools_entity_insert(EntityInterface $entity) {
if (\Drupal::config('kgaut_tools.config')->get('disable_image_derivate') === 1) {
return;
}
/** @var \Drupal\file\Entity\File $entity */
if ($entity instanceof FileInterface) {
$image = \Drupal::service('image.factory')->get($entity->getFileUri());
/** @var \Drupal\Core\Image\Image $image */
if ($image->isValid()) {
$styles = ImageStyle::loadMultiple();
$image_uri = $entity->getFileUri();
/** @var \Drupal\image\Entity\ImageStyle $style */
foreach ($styles as $style) {
$destination = $style->buildUri($image_uri);
$style->createDerivative($image_uri, $destination);
}
}
}
}
/**
* Implements hook_preprocess().
*
* Add custom variable to all templates.
*/
function kgaut_tools_preprocess(&$variables, $hook) {
$theme = \Drupal::theme()->getActiveTheme();
$isHttps = \Drupal::request()->isSecure() || \Drupal::service('settings')->get('force_https');
$variables['basepath'] = base_path();
$variables['baseurl_front'] = Url::fromRoute('<front>', [], ['absolute' => TRUE, 'https' => $isHttps])->toString();
$variables['pathtotheme'] = base_path() . $theme->getPath();
$variables['path_default_theme'] = base_path() . \Drupal::service('theme_handler')->getTheme(\Drupal::service('theme_handler')->getDefault())->getPath();
$variables['baseurl_theme'] = $variables['baseurl_front'] . '/' . $theme->getPath();
$variables['baseurl_default_theme'] = $variables['baseurl_front'] . $variables['path_default_theme'];
$variables['pathtotfiles'] = PublicStream::basePath();
$variables['baseurl_files'] = $variables['baseurl_front'] . PublicStream::basePath();
}
/**
* Implements hook_form_alter().
*/
function kgaut_tools_form_system_performance_settings_alter(&$form, \Drupal\Core\Form\FormStateInterface $form_state) {
$form['caching']['page_cache_maximum_age']['#options'] += [
172800 => '2 days',
345600 => '4 days',
432000 => '5 days',
864000 => '10 days',
1728000 => '20 days',
];
}
/**
* Implements hook_page_attachments().
*
* Add custom variables to DrupalSettings.
*/
function kgaut_tools_page_attachments(array &$attachments) {
$attachments['#attached']['drupalSettings']['basepath'] = base_path();
$attachments['#attached']['drupalSettings']['pathtotheme'] = base_path() . \Drupal::service('extension.list.theme')->getPathname(\Drupal::theme()->getActiveTheme()->getName());
$attachments['#attached']['drupalSettings']['pathtotfiles'] = PublicStream::basePath();
}
/**
* Implements hook_theme_suggestions_HOOK().
*
* Add custom variables to DrupalSettings.
*/
function kgaut_tools_theme_suggestions_user(array $variables) {
$suggestions = [];
$sanitized_view_mode = strtr($variables['elements']['#view_mode'], '.', '_');
$suggestions[] = $variables['theme_hook_original'] . '__' . $sanitized_view_mode;
return $suggestions;
}
/**
* Implements hook_theme_suggestions_HOOK().
*
* Add custom variables to DrupalSettings.
*/
function kgaut_tools_theme_suggestions_node(array $variables) {
$suggestions = [];
/** @var \Drupal\node\Entity\Node $node */
$node = $variables['elements']['#node'];
$sanitized_view_mode = strtr($variables['elements']['#view_mode'], '.', '_');
$suggestions[] = $variables['theme_hook_original'] . '__' . $sanitized_view_mode;
$bundle = $node->bundle();
$sanitized_bundle = strtr($bundle, '.', '_');
$suggestions[] = $variables['theme_hook_original'] . '__' . $sanitized_bundle;
$suggestions[] = $variables['theme_hook_original'] . '__' . $sanitized_bundle . '__' . $sanitized_view_mode;
return $suggestions;
}
/**
* Implements hook_theme_suggestions_HOOK().
*
*/
function kgaut_tools_theme_suggestions_taxonomy_term(array $variables) {
$suggestions = [];
/** @var \Drupal\taxonomy\Entity\Term $term */
$term = $variables['elements']['#taxonomy_term'];
$sanitized_view_mode = strtr($variables['elements']['#view_mode'], '.', '_');
$suggestions[] = $variables['theme_hook_original'] . '__' . $sanitized_view_mode;
$bundle = $term->bundle();
$sanitized_bundle = strtr($bundle, '.', '_');
$suggestions[] = $variables['theme_hook_original'] . '__' . $sanitized_bundle . '__' . $sanitized_view_mode;
return $suggestions;
}
/**
* Implements hook_theme_suggestions_HOOK().
*
* Add view mode to page based on view bundle.
*/
function kgaut_tools_theme_suggestions_page(array $variables) {
$suggestions = [];
if (\Drupal::routeMatch()->getRouteName() == 'entity.node.canonical') {
$bundle = \Drupal::routeMatch()->getParameter('node')->bundle();
$suggestions[] = $variables['theme_hook_original'] . '__node__' . $bundle;
}
return $suggestions;
}
/**
* Implements hook_user_login().
*
* Dispatch UserLoginEvent::kgaut_tools_user_login
*/
function kgaut_tools_user_login($account) {
$event = new \Drupal\kgaut_tools\Event\UserLoginEvent($account);
/** @var \Drupal\Component\EventDispatcher\ContainerAwareEventDispatcher $event_dispatcher */
$event_dispatcher = \Drupal::service('event_dispatcher');
$event_dispatcher->dispatch($event, $event::EVENT_NAME);
}