-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathdrupalcamp_theme.theme
More file actions
executable file
·179 lines (167 loc) · 5.33 KB
/
drupalcamp_theme.theme
File metadata and controls
executable file
·179 lines (167 loc) · 5.33 KB
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
<?php
/**
* @file
* Drupal camp theme thing.
*/
use Drupal\Core\Url;
use Drupal\image\Entity\ImageStyle;
use Drupal\views\ViewExecutable;
/**
* Implements hook_preprocess_HOOK().
*/
function drupalcamp_theme_preprocess_block(&$variables) {
if ($variables['base_plugin_id'] == 'system_branding_block') {
if ($variables['content']['site_logo']['#access'] && $variables['content']['site_logo']['#uri']) {
$variables['site_logo'] = str_replace('.svg', '.png', $variables['content']['site_logo']['#uri']);
}
}
}
/**
* Implements hook_preprocess_HOOK().
*/
function drupalcamp_theme_preprocess_node(&$variables) {
/** @var \Drupal\node\Entity\Node $node */
$node = $variables['node'];
if ($node->bundle() == 'session' && $variables['view_mode'] == 'full') {
// Attach also the user display under the node.
$renderer = \Drupal::entityTypeManager()
->getViewBuilder('user');
$variables['content']['user'] = $renderer->view($node->getOwner());
$variables['content']['user']['#weight'] = 200;
$variables['content']['user']['#prefix'] = '<h4>' . t('About the speaker') . '</h4>';
}
if ($node->bundle() == 'sponsor') {
// Add the link URL as a variable.
$url = $node->field_sponsor_url->first()->getValue();
$variables['sponsor_url'] = $url['uri'];
$variables['sponsor_image'] = $variables['content']['field_sponsor_logo'];
}
if ($node->bundle() == 'featured_speaker') {
_drupalcamp_theme_create_featured_speaker_variables($variables);
}
}
/**
* Creates some variables.
*/
function _drupalcamp_theme_create_featured_speaker_variables(&$variables) {
/** @var \Drupal\node\Entity\Node $node */
$node = $variables['node'];
// Make some variables available.
$variables['user_image'] = '';
$variables['name'] = '';
$variables['intro'] = '';
$variables['user_url'] = '';
// Try to load the user in question.
if (!$node->field_speaker || $node->field_speaker->isEmpty()) {
return;
}
/** @var \Drupal\user\Entity\User $user */
$user = $node->field_speaker->entity;
$variables['user_url'] = Url::fromRoute('entity.user.canonical', [
'user' => $user->id(),
])->toString();
$variables['name'] = $node->label();
// Get the picture, if any.
if ($user->field_user_picture && !$user->field_user_picture->isEmpty()) {
/** @var \Drupal\file\Entity\File $pic */
$pic = $user->field_user_picture->entity;
$variables['user_image'] = [
'#type' => 'inline_template',
'#template' => '<img src="{{ image }} " />',
'#context' => [
'image' => ImageStyle::load('square')->buildUrl($pic->getFileUri()),
],
];
}
if ($node->field_short_intro && !$node->field_short_intro->isEmpty()) {
$variables['intro'] = $node->field_short_intro->first()->getString();
}
}
/**
* Implements hook_preprocess_HOOK().
*
* Add column padding to sponsor blocks so they are centered.
*/
function drupalcamp_theme_preprocess_views_view_unformatted(&$vars) {
/** @var \Drupal\views\ViewExecutable $view */
$view = $vars['view'];
if ($view->id() == 'sessions') {
drupalcamp_theme_preprocess_sessions_view($vars);
}
if ($view->id() != 'sponsor_blocks') {
return;
}
if (empty($vars['rows'])) {
return;
}
/** @var \Drupal\views\Plugin\views\display\DisplayPluginBase $display */
$display = $view->getDisplay();
$display_id = $display->display['id'];
$max_in_row = 2;
$padding = 2;
$divider = 1;
switch ($display_id) {
case 'block_2':
$max_in_row = 4;
break;
case 'block_3';
$max_in_row = 8;
$divider = 2;
break;
}
// See how many results there are, and then how much column padding we should
// add.
$num_results = count($view->result);
if ($num_results !== $max_in_row) {
$padding = (int) ($padding + (($max_in_row - $num_results) / $divider));
}
/** @var \Drupal\Core\Template\Attribute $attr */
$attr = $vars['rows'][0]['attributes'];
$attr->addClass('col-md-offset-' . $padding);
}
/**
* Fix day headers on the rows.
*/
function drupalcamp_theme_preprocess_sessions_view(&$vars) {
// See if this is the first session of the day.
/** @var \Drupal\views\ViewExecutable $view */
$view = $vars['view'];
$headings = drupalcamp_theme_get_session_headings($view);
foreach ($vars["rows"] as $delta => $row) {
if (!empty($headings[$delta])) {
$vars['pre_title'] = $headings[$delta];
}
}
}
/**
* Helper.
*/
function drupalcamp_theme_get_session_headings(ViewExecutable $view) {
$data = &drupal_static(__FUNCTION__);
if (isset($data)) {
return $data;
}
$headings = [];
$last_heading = NULL;
foreach ($view->result as $delta => $row) {
$date = strtotime($row->node__field_session_time_field_session_time_value);
if (!$date) {
continue;
}
$potential_heading = format_date($date, 'custom', 'l d.m.Y');
if ($potential_heading != $last_heading) {
$last_heading = $potential_heading;
$headings[$delta] = $potential_heading;
}
}
$data = $headings;
return $headings;
}
/**
* Implements hook_preprocess_HOOK().
*/
function drupalcamp_theme_preprocess_page(&$variables) {
$variables["page"]["footer"]['media_partner'] = [
'#markup' => '<section class="media-partner"><h2>' . t('Media partner') . '</h2><p><a href="http://www.theweeklydrop.com/"><img src="/themes/contrib/drupalcamp_theme/images/weekly.png" /></a></p></section>',
];
}