This repository has been archived by the owner on Dec 27, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdrupal8_module_boilerplate.module
60 lines (51 loc) · 1.88 KB
/
drupal8_module_boilerplate.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
<?php
function drupal8_module_boilerplate_page_attachments(&$page)
{
// This could in most cases be added in template as well with:
// {{ attach_library('drupal8_module_boilerplate/drupal8-module-boilerplate-css-and-js') }}
$page['#attached']['library'][] = 'drupal8_module_boilerplate/drupal8-module-boilerplate-css-and-js';
}
function drupal8_module_boilerplate_theme($existing, $type, $theme, $path)
{
return [
'drupalboilerplate-block' => [
'variables' => [
'configtitle' => '',
],
],
'drupalboilerplate-page' => [
'variables' => [],
],
];
}
// Setup custom 404!
// -> add file "page--404.html.twig" to theme template folder
function drupal8_module_boilerplate_theme_suggestions_page(array $variables)
{
$path_args = explode('/', trim(\Drupal::service('path.current')->getPath(), '/'));
$suggestions = theme_get_suggestions($path_args, 'page');
$http_error_suggestions = [
'system.404' => 'page__404',
];
$route_name = \Drupal::routeMatch()->getRouteName();
if (isset($http_error_suggestions[$route_name])) {
$suggestions[] = $http_error_suggestions[$route_name];
}
return $suggestions;
}
// Create images based on image specific image style on upload:
function drupal8_module_boilerplate_insert(Drupal\Core\Entity\EntityInterface $entity)
{
generateImages($entity, 'large');
generateImages($entity, 'medium');
generateImages($entity, 'thumbnail');
}
function generateImages (Drupal\Core\Entity\EntityInterface $entity, $type = 'large')
{
if ($entity Instanceof \Drupal\file\FileInterface && $entity->getFileUri()) {
$imageUri = $entity->getFileUri();
$style = \Drupal\image\Entity\ImageStyle::load($type);
$destination = $style->buildUri($imageUri);
$style->createDerivative($imageUri, $destination);
}
}