forked from ASUWebPlatforms/webspark-module-asu_events
-
Notifications
You must be signed in to change notification settings - Fork 0
/
asu_events.module
114 lines (103 loc) · 3.42 KB
/
asu_events.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
105
106
107
108
109
110
111
112
113
114
<?php
use Drupal\Core\Url;
/**
* Implements hook_preprocess_HOOK().
*/
function asu_events_preprocess_block(&$variables) {
$plugin_id = $variables['base_plugin_id'] ?? '';
if ($plugin_id != 'inline_block') {
return;
}
if ($variables['derivative_plugin_id'] == 'events') {
$variables = _preprocess_events($variables);
}
}
/**
* All of the preprocess logic.
*
* @param array $variables
* Array of variables.
*
* @return array
*
*/
function _preprocess_events($variables) {
$rand_id = random_int(0, PHP_INT_MAX);
$events_block = new \stdClass();
$variables['events_id'] = 'events-wrapper-' . $rand_id;
$block = $variables['content']['#block_content'];
$events_block->header = new \stdClass();
$events_block->dataSource = new \stdClass();
$events_block->ctaButton = new \stdClass();
if ($block->field_events_heading->value) {
$events_block->header->text = $block->field_events_heading->value;
}
if ($block->field_events_header_text_color->value) {
$events_block->header->color = $block->field_events_header_text_color->value;
}
if ($block->field_events_cta && $block->field_events_cta->title && $block->field_events_cta->uri) {
$events_block->ctaButton->text = $block->field_events_cta->title;
$link = Url::fromUri($block->field_events_cta->uri);
$events_block->ctaButton->url = $link->toString();
}
if ($block->field_events_cta_color->value) {
$events_block->ctaButton->color = $block->field_events_cta_color->value;
}
if ($block->field_events_items_to_display->value) {
$events_block->display = $block->field_events_items_to_display->value;
}
$events_block->dataSource->url = '/asu-events-feed/';
if ($block->field_feed_taxonomy_term->value) {
// Allow only alphanumeric characters and underscore.
$events_block->dataSource->url .= preg_replace('/[^A-Za-z0-9\_]/', '', $block->field_feed_taxonomy_term->value);
}
if ($block->field_events_feed_filter->value) {
$events_block->dataSource->filters = $block->field_events_feed_filter->value;
}
$settings = [];
$settings['components']['asu_events'][$rand_id] = $events_block;
$variables['content']['#attached']['drupalSettings']['asu'] = $settings;
$variables['content']['#attached']['library'][] = 'asu_events/events-component';
return $variables;
}
/**
* Adds block bundle/type to entity embed theme suggestions.
*
* {@inheritdoc}
*/
function asu_events_theme_suggestions_alter(array &$suggestions, array $variables, $hook) {
if (!empty($variables['element']['#entity'])) {
$block_type = $variables['element']['#entity']->bundle();
$suggestions[] = 'entity_embed_container__embed_button' . '__' . $block_type;
}
}
/**
* Implements hook_theme().
*
* {@inheritdoc}
*/
function asu_events_theme($existing, $type, $theme, $path) {
$templates = $path . '/templates';
return [
'entity_embed_container__embed_button__events' => [
'base hook' => 'entity_embed_container',
'render element' => 'element'
]
];
}
/**
* Implements hook_preprocess_HOOK().
*
* {@inheritdoc}
*/
function asu_events_preprocess_entity_embed_container(&$variables) {
$element = $variables['element'];
$block = $element['#entity'];
$block_type = $block->bundle();
if ($block_type == "events") {
$variables['base_plugin_id'] = 'inline_block';
$variables['derivative_plugin_id'] = 'events';
$variables['content']['#block_content'] = $block;
$variables = _preprocess_events($variables);
}
}