-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathos2web_spotbox.module
142 lines (129 loc) · 4.39 KB
/
os2web_spotbox.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
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
<?php
/**
* @file
* OS2Web Spotbox module file.
*/
use Drupal\Core\Url;
use Drupal\os2web_spotbox\Form\SpotboxForm;
use Drupal\Component\Utility\NestedArray;
use Symfony\Component\Yaml\Yaml;
use Drupal\Core\Render\Element;
/**
* Implements hook_inline_entity_form_entity_form_alter().
*/
function os2web_spotbox_inline_entity_form_entity_form_alter(&$entity_form, &$form_state) {
if ($entity_form['#entity_type'] == 'os2web_spotbox') {
SpotboxForm::adjustForm($entity_form, $form_state);
}
}
/**
* Update entity from yml.
*
* Some config like form display needs to be updated via config manager.
*
* @param string $ymlFileName
* Yml file name.
* @param string $entityType
* The entity type for this storage.
* @param mixed $id
* The ID of the entity to load.
* @param array $setComponents
* Array of components you want to add.
* - The key will be what we are setting.
* - The value is the key that will be used from the new config file
* (Can have in string for array).
*/
function os2web_spotbox_update_entity_from_yml($ymlFileName, $entityType, $id, array $setComponents) {
$yml = Yaml::parse(file_get_contents(drupal_get_path('module', 'os2web_spotbox') . '/config/optional/' . $ymlFileName . '.yml'));
$entity = \Drupal::entityTypeManager()
->getStorage($entityType)
->load($id);
foreach ($setComponents as $key => $value) {
$parts = explode('.', $value);
if (count($parts) == 1) {
$entity->setComponent($key, $yml[$value]);
}
else {
$value = NestedArray::getValue($yml, $parts);
if (empty($value)) {
\Drupal::messenger()->addWarning('Component ' . $key . ' has empty configuration');
continue;
}
$entity->setComponent($key, $value);
}
}
$entity->save();
}
/**
* Implements hook_theme().
*/
function os2web_spotbox_theme() {
return [
'os2web_spotbox' => [
'render element' => 'elements',
],
'taxonomy_term__os2web_icons' => [
'base hook' => 'taxonomy_term',
],
'field__taxonomy_term__os2web_icons' => [
'base hook' => 'field',
],
'paragraph__os2web_spotbox_reference' => [
'base hook' => 'paragraph',
],
];
}
/**
* Implements hook_theme_suggestions_HOOK().
*/
function os2web_spotbox_theme_suggestions_os2web_spotbox(array $variables) {
$suggestions = [];
$spotbox = $variables['elements']['#os2web_spotbox'];
$type = $spotbox->getType();
$sanitized_view_mode = strtr($variables['elements']['#view_mode'], '.', '_');
$suggestions[] = 'os2web_spotbox__' . $type;
$suggestions[] = 'os2web_spotbox__' . $type . '__' . $sanitized_view_mode;
$suggestions[] = 'os2web_spotbox__' . $type . '__' . $spotbox->id();
$suggestions[] = 'os2web_spotbox__' . $type . '__' . $spotbox->id() . '__' . $sanitized_view_mode;
return $suggestions;
}
/**
* Prepares variables for OS2Web Spotbox templates.
*
* Default template: os2web_spotbox.html.twig.
*
* @param array $variables
* An associative array containing:
* - elements: An associative array containing the user information and any
* - attributes: HTML attributes for the containing element.
*/
function template_preprocess_os2web_spotbox(array &$variables) {
// Fetch Spotbox Entity Object.
$os2web_spotbox = $variables['elements']['#os2web_spotbox'];
$variables['spotbox'] = $os2web_spotbox;
$variables['spotbox_type'] = $os2web_spotbox->getType();
$variables['background_color'] = $os2web_spotbox->getBackgroundColor();
if (!empty($os2web_spotbox->field_os2web_spotbox_link)
&& !$os2web_spotbox->field_os2web_spotbox_link->isEmpty()) {
$link = $os2web_spotbox->field_os2web_spotbox_link->first()->getValue();
$variables['spotbox_url'] = Url::fromUri($link['uri'], ['absolut' => TRUE]);
$variables['spotbox_heading'] = $link['title'];
}
// Helpful $content variable for templates.
foreach (Element::children($variables['elements']) as $key) {
$variables['content'][$key] = $variables['elements'][$key];
}
}
/**
* Implements hook_preprocess_HOOK().
*/
function os2web_spotbox_preprocess_paragraph(&$variables) {
$paragraph = $variables['paragraph'];
if ($paragraph->bundle() != 'os2web_spotbox_reference') {
return;
}
$field_os2web_spotbox_ref_display_value = $paragraph->field_os2web_spotbox_ref_display->getValue();
if (!empty($field_os2web_spotbox_ref_display_value)) {
$variables['grid_type'] = $field_os2web_spotbox_ref_display_value[0]['value'];
}
}