-
Notifications
You must be signed in to change notification settings - Fork 0
/
ecoindex.module
104 lines (90 loc) · 4.2 KB
/
ecoindex.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
<?php
/**
* @file
* Hook implementations for ecoindex module.
*/
use Drupal\Core\Field\FieldTypeCategoryManagerInterface;
use Drupal\Core\Routing\RouteMatchInterface;
/**
* Implements hook_help().
*/
function ecoindex_help($route_name, RouteMatchInterface $route_match) {
switch ($route_name) {
case 'help.page.ecoindex':
$output = '';
$output .= '<h2>' . t('Image and animation management') . '</h2>';
$output .= '<p>' . t("Optimize and generate images before importing them into the CMS") . '</p>';
$output .= '<p>' . t("Use images to convey information") . '</p>';
$output .= '<p>' . t("Propose a text alternative") . '</p>';
$output .= '<p>' . t("Limit animations") . '</p>';
$output .= '<p>' . t("Avoid animated GIFs") . '</p>';
$output .= '<p>' . t("Avoid carousels (avoid automatic scrolling)") . '</p>';
$output .= '<p>' . t("Use instant changes rather than animated changes (Javascript)") . '</p>';
$output .= '<p>' . t("Prefer pagination to infinite scrolling") . '</p>';
$output .= '<h2>' . t('Video and sound management') . '</h2>';
$output .= '<p>' . t("Ban video backgrounds") . '</p>';
$output .= '<p>' . t("Use video or audio content when it conveys information") . '</p>';
$output .= '<p>' . t("Avoid automatic playback and loading of videos and sounds") . '</p>';
$output .= '<p>' . t("Adapt videos and sounds to reading contexts (size and definition)") . '</p>';
$output .= '<p>' . t("Offer a text alternative") . '</p>';
$output .= '<p>' . t("Offer a 'listen only' mode for videos") . '</p>';
$output .= '<h2>' . t("Document management") . '</h2>';
$output .= '<p>' . t("Compress and optimize documents") . '</p>';
$output .= '<p>' . t("Provide document summaries") . '</p>';
$output .= '<p>' . t("Provide information on file size and format for downloading") . '</p>';
$output .= '<h2>' . t("Link management") . '</h2>';
$output .= '<p>' . t("Check that destination urls are valid") . '</p>';
return $output;
}
}
/**
* Implements hook_page_attachments_alter().
*/
function ecoindex_page_attachments_alter(array &$attachments) {
$route_name = \Drupal::routeMatch()->getRouteName();
// Add library and drupalSettings on preview node route.
if ($route_name === 'entity.node.preview') {
// Get current node.
$node = \Drupal::routeMatch()->getParameter('node_preview');
// Get EcoIndex field_name if exists.
$ecoindexField = \Drupal::service('ecoindex.helper')->getEcoIndexFieldName($node->getFields());
if ($ecoindexField && $node->preview_view_mode === 'full') {
// Add ecoindex library.
$attachments['#attached']['library'][] = 'ecoindex/ecoindex.preview';
// Add drupalSettings.
$settings = \Drupal::config('ecoindex.settings');
// Get current value for EcoIndex field.
$value = $node->get($ecoindexField)->getValue();
// Send related information to drupalSettings.
$attachments['#attached']['drupalSettings']['ecoindex'] = [
'score' => $value[0]['score'] ?? 0,
'grade' => $value[0]['grade'] ?? '',
'minimum_score' => $settings->get('minimum_score'),
'nid' => $node->id(),
];
}
}
// Add library and drupalSettings on edit form node route.
if ($route_name === 'entity.node.edit_form') {
// Get current node.
$node = \Drupal::routeMatch()->getParameter('node');
// Get EcoIndex field_name if exists.
$ecoindexField = \Drupal::service('ecoindex.helper')->getEcoIndexFieldName($node->getFields());
if ($ecoindexField) {
$attachments['#attached']['library'][] = 'ecoindex/ecoindex.edit';
// Send related information to drupalSettings.
$attachments['#attached']['drupalSettings']['ecoindex'] = [
'nid' => $node->id(),
'field' => $ecoindexField,
];
}
}
}
/**
* Implements hook_field_type_category_info_alter().
*/
function ecoindex_field_type_category_info_alter(&$definitions) {
// The `ecoindex` field type belongs in the `general` category,
// so the libraries need to be attached using an alter hook.
$definitions[FieldTypeCategoryManagerInterface::FALLBACK_CATEGORY]['libraries'][] = 'ecoindex/ecoindex.icon';
}