Building enough Drupal 8 websites I ended up applying always the same basic set of code in my Bootstrap sub theme.
Preprocess hooks are a common way in Drupal to add or manipulate variables that you can use in your Twig templates files.
Suggestions hooks is a common way in Drupal 8 to extend the templates you can use for any part of your code.
Here is a snippet of this, copy this in your theme.theme file and replace MY_D8_THEME
by... your theme name!
MY_DRUPAL8_THEME.theme
7.61 KiB
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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
<?php
/**
* @file
* Preprocess and suggestions for a Drupal sub-theme.
*
* Change MY_D8_THEME by your theme name.
*/
use Drupal\Core\Entity\EntityInterface;
use Drupal\block\Entity\Block;
/**
* Implements hook_preprocess().
*
* Add a global base pat variables to all twig templates and javascript.
*/
function MY_D8_THEME_preprocess(&$variables, $hook) {
$variables['base_path'] = base_path();
if (isset($variables['directory'])) {
$variables['theme_path'] = base_path() . $variables['directory'];
if (isset($variables['#attached'])) {
$variables['#attached']['drupalSettings']['path']['themePath'] = $variables['theme_path'];
}
}
$variables['lang'] = \Drupal::languageManager()->getCurrentLanguage()->getId();
}
/**
* Implements template_preprocess_html().
*
* Add classes to the body.
*/
function MY_D8_THEME_preprocess_html(&$variables) {
// Add a class.
$variables['attributes']['class'][] = 'my-new-class';
$variables['attributes']['class'][] = 'lang-' . \Drupal::languageManager()->getCurrentLanguage()->getId();
}
/**
* Implements hook_preprocess_page_title().
*
* Add entity information inline to the title if unpublished and moderation
* state if enabled.
*/
function MY_D8_THEME_preprocess_page_title(&$variables, $hook) {
// Load the entity from current route.
foreach (\Drupal::routeMatch()->getParameters() as $entity) {
// Filter for entity.
if ($entity instanceof EntityInterface) {
$suffix = '';
// Add an Unpublished suffix to the title for any entity
if (method_exists($entity, 'isPublished')) {
if (!$entity->isPublished()) {
// Example with Bootstrap classes.
$suffix .= ' <span class="label label-danger">' . t('Unpublished') . '</span>';
}
}
// Check content moderation if enable.
$moduleHandler = \Drupal::service('module_handler');
if ($moduleHandler->moduleExists('content_moderation')){
// Add a moderation state label.
if (isset($entity->moderation_state) && method_exists($entity, 'isPublished')) {
if (!$entity->isPublished()) {
if ($current_state = $entity->moderation_state->value) {
$moderation_info = \Drupal::service('content_moderation.moderation_information');
// Add moderation state label to the title.
$workflow = $moderation_info->getWorkflowForEntity($entity);
$state = $workflow->getTypePlugin()->getState($current_state)->label();
$suffix .= ' <span class="label label-info">' . $state . '</span>';
}
}
}
}
if (!empty($suffix)) {
$variables['title'] = [
'#markup' => $variables['title'],
'#suffix' => $suffix,
];
}
}
}
}
/**
* Implements hook_preprocess_comment().
*
* Add variables to comment template:
* user picture, author name, comment depth and changed date.
*/
function MY_D8_THEME_preprocess_comment(&$variables) {
$variables['author_raw'] = $variables['comment']->getAuthorName();
$variables['author_picture'] = user_view($variables['comment']->getOwner(), 'picture');
$variables['comment_depth'] = count(explode('.', $variables['comment']->getThread()));
if ($variables['comment']->getChangedTime() == $variables['comment']->getCreatedTime()) {
$variables['changed_short'] = NULL;
}
else {
$variables['changed_short'] = format_date($variables['comment']->getChangedTime(), 'short');
}
}
/**
* Implements hook_preprocess_node().
*
* Add variables to node template.
* Author picture and name.
*/
function MY_D8_THEME_preprocess_node(&$variables) {
$variables['author_name'] = $variables['node']->getOwner()->getDisplayName();
$variables['author_picture'] = user_view($variables['node']->getOwner(), 'picture');
}
/**
* Implements hook_theme_suggestions_HOOK_alter().
*
* Add custom block theme suggestion based on block type and display.
*/
function MY_D8_THEME_theme_suggestions_block_alter(array &$suggestions, array $variables) {
// View mode suggestion for custom blocks.
if (isset($variables['elements']['#configuration']['view_mode'])) {
$view_mode = $variables['elements']['#configuration']['view_mode'];
// block--full.html.twig
// block--MY_DISPLAY_ID.html.twig
$suggestions[] = 'block__' . $view_mode;
}
else {
$view_mode = NULL;
}
// Region suggestion for blocks in Panels.
if (isset($variables['elements']['#configuration']['region'])) {
$region = $variables['elements']['#configuration']['region'];
$suggestions[] = 'block__' . $region;
if (isset($variables['elements']['#configuration']['provider'])) {
$provider = $variables['elements']['#configuration']['provider'];
$suggestions[] = 'block__' . $region . '__' . $provider;
}
}
// Region suggestion for blocks in Drupal.
if (isset($variables['elements']['#id'])) {
if ($block = Block::load($variables["elements"]["#id"])) {
$region = $block->getRegion();
// block--REGION_ID.html.twig
$suggestions[] = 'block__' . $region;
// block--REGION_ID--CUSTOM_BLOCK_ID.html.twig
$suggestions[] = 'block__' . $region . '__' . $variables['elements']['#base_plugin_id'];
// block--REGION_ID--MY_BLOCK_ID.html.twig
$suggestions[] = 'block__' . $region . '__' . $variables['elements']['#id'];
// block--REGION_ID--MY_BLOCK_ID--CUSTOM_BLOCK_ID.html.twig
$suggestions[] = 'block__' . $region . '__' . $variables['elements']['#base_plugin_id'] . '__' . $variables['elements']['#id'];
}
}
// Custom Blocks (Bundles and view mode).
if ($variables['elements']['#base_plugin_id'] === 'block_content'
&& isset($variables['elements']['content']['#block_content'])) {
// Bundle type.
$bundle = $variables['elements']['content']['#block_content']->bundle();
if (isset($region)) {
// block--REGION_ID--BUNDLE_ID.html.twig
$suggestions[] = 'block__' . $region . '__' . $bundle;
}
if ($view_mode = $variables['elements']['content']['#view_mode']) {
if (isset($region)) {
// block--REGION_ID--BUNDLE_ID--MY_DISPLAY_ID.html.twig
$suggestions[] = 'block__' . $region . '__' . $bundle . '__' . $view_mode;
}
// block--BUNDLE_ID--MY_DISPLAY_ID.html.twig
$suggestions[] = 'block__' . $bundle . '__' . $view_mode;
}
$suggestions[] = 'block__' . $bundle;
}
}
/**
* Implements hook_theme_suggestions_form_alter().
*
* Add form id theme suggestion on all forms.
*/
function bootstrap_dd_theme_suggestions_form_alter(array &$suggestions, array $variables) {
$suggestions[] = 'form__' . str_replace('-', '_', $variables['element']['#id']);
}
/**
* Implements hook_theme_suggestions_field_alter().
*/
function MY_D8_THEME_theme_suggestions_field_alter(array &$suggestions, array $variables) {
$name = $variables['element']['#field_name'];
// View mode.
if ($view_mode = $variables['element']['#view_mode']) {
$suggestions[] = 'field__' . $view_mode;
$suggestions[] = 'field__' . $view_mode . '__' . $name;
}
}
/**
* Implements hook_theme_suggestions_user_alter().
*/
function MY_D8_THEME_theme_suggestions_user_alter(array &$suggestions, array $variables) {
if ($view_mode = $variables['elements']['#view_mode']) {
$suggestions[] = 'user__' . $view_mode;
}
}
/**
* Implements hook_theme_suggestions_views_exposed_form_alter().
*/
function MY_D8_THEME_theme_suggestions_views_exposed_form_alter(array &$suggestions, array $variables) {
if (isset($variables['form']['#theme'])) {
// Add all views exposed theme function except base one.
array_pop($variables['form']['#theme']);
$suggestions = $variables['form']['#theme'] + $suggestions;
}
}