-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCrumbRenderer.php
More file actions
147 lines (127 loc) · 4.31 KB
/
Copy pathCrumbRenderer.php
File metadata and controls
147 lines (127 loc) · 4.31 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
<?php
declare(strict_types=1);
namespace Drupal\crumb;
use Drupal\Core\Config\ConfigFactoryInterface;
use Drupal\Core\Extension\ModuleHandlerInterface;
/**
* Builds the render array for the Crumb widget container.
*/
class CrumbRenderer {
public const ALLOWED_VIEWS = ['list', 'map'];
public function __construct(
protected ConfigFactoryInterface $configFactory,
protected ModuleHandlerInterface $moduleHandler,
) {}
/**
* Build a render array for the widget.
*
* @param array $overrides
* Per-instance overrides. Keys: server, service_body, view, geolocation.
* Use NULL to fall back to saved config; '' (empty string) explicitly omits
* service_body so all meetings show.
*/
public function build(array $overrides = []): array {
$config = $this->configFactory->get('crumb.settings');
$server = trim((string) ($overrides['server'] ?? $config->get('server') ?? ''));
if ($server === '') {
return [
'#markup' => '<p style="color:red"><strong>Crumb:</strong> a <code>server</code> URL is required.</p>',
];
}
$service_body = $overrides['service_body'] ?? $config->get('service_body');
$format_ids = $overrides['format_ids'] ?? $config->get('format_ids');
$view_raw = $overrides['view'] ?? $config->get('view') ?? '';
$view = in_array($view_raw, self::ALLOWED_VIEWS, TRUE) ? $view_raw : '';
$base_path = trim((string) ($config->get('base_path') ?? ''), '/');
$template = (string) ($config->get('css_template') ?? '');
$attributes = [
'id' => 'crumb-widget',
'data-server' => $server,
];
if ($service_body !== NULL && $service_body !== '') {
$attributes['data-service-body'] = trim((string) $service_body);
}
if ($format_ids !== NULL && $format_ids !== '') {
$attributes['data-format-ids'] = trim((string) $format_ids);
}
if ($view !== '') {
$attributes['data-view'] = $view;
}
if ($base_path !== '') {
$attributes['data-path'] = '/' . $base_path;
}
$widget = [
'#type' => 'html_tag',
'#tag' => 'div',
'#attributes' => $attributes,
];
// #attached lives at the TOP level so it bubbles correctly through both
// BlockViewBuilder (which only steals #attributes) and FilterProcessResult
// (which reads #attached / #cache via BubbleableMetadata::createFromRenderArray).
$attached = [
'library' => ['crumb/widget'],
];
$config_array = $this->buildWidgetConfig();
if (isset($overrides['geolocation']) && $overrides['geolocation'] !== NULL) {
$config_array['geolocation'] = filter_var(
$overrides['geolocation'],
FILTER_VALIDATE_BOOLEAN
);
}
$this->moduleHandler->alter('crumb_config', $config_array);
if (!empty($config_array)) {
$attached['html_head'][] = [
[
'#tag' => 'script',
'#value' => 'window.CrumbWidgetConfig = ' . json_encode(
$config_array,
JSON_UNESCAPED_SLASHES
) . ';',
],
'crumb_widget_config',
];
}
if ($template === 'full_width') {
$attached['library'][] = 'crumb/full_width';
return [
'wrapper' => [
'#type' => 'html_tag',
'#tag' => 'div',
'#attributes' => ['class' => ['crumb-full-width']],
'widget' => $widget,
],
'#attached' => $attached,
];
}
if ($template === 'full_width_force') {
$attached['library'][] = 'crumb/full_width_force';
return [
'wrapper' => [
'#type' => 'html_tag',
'#tag' => 'div',
'#attributes' => ['class' => ['crumb-full-width-force']],
'widget' => $widget,
],
'#attached' => $attached,
];
}
// Wrap in a child key so block plugins (or any container that hoists a
// top-level #attributes onto its own wrapper) cannot strip the widget
// element's id and data-* attributes.
return [
'widget' => $widget,
'#attached' => $attached,
];
}
/**
* {@inheritdoc}
*/
protected function buildWidgetConfig(): array {
$json = (string) ($this->configFactory->get('crumb.settings')->get('widget_config') ?? '');
if ($json === '') {
return [];
}
$decoded = json_decode($json, TRUE);
return is_array($decoded) ? $decoded : [];
}
}