-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconvivial_gin.theme
225 lines (192 loc) · 6.87 KB
/
convivial_gin.theme
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
218
219
220
221
222
223
224
225
<?php
declare(strict_types=1);
use Drupal\Component\Utility\Html;
use Drupal\Core\Form\FormStateInterface;
/**
* @file
* Functions to support theming in the convivial_gin theme.
*/
function convivial_gin_form_system_theme_settings_alter(&$form, FormStateInterface $form_state, $form_id = NULL) {
// Work-around for a core bug affecting admin themes. See issue #943212.
if (isset($form_id)) {
return;
}
$groups = [
'admin_ui' => t('Node edit'),
];
foreach ($groups as $group => $title) {
$form[$group] = [
'#type' => 'details',
'#title' => $title,
'#group' => 'convivial_admin_settings',
'#weight' => 99,
];
}
$form['admin_ui']['content_groups'] = [
'#type' => 'textarea',
'#title' => t('Content groups'),
'#description' => t('Define the fields to be displayed in a content group. e.g. Group X|field_a, field_b'),
'#default_value' => theme_get_setting('content_groups'),
];
$form['admin_ui']['sidebar_groups'] = [
'#type' => 'textarea',
'#title' => t('Sidebar groups'),
'#description' => t('Define the fields to be displayed in a sidebar group. e.g. Group Y|field_c, field_d '),
'#default_value' => theme_get_setting('sidebar_groups'),
];
}
/**
* Implements hook_form_BASE_FORM_ID_alter() for \Drupal\node\NodeForm.
*/
function convivial_gin_form_node_form_alter(&$form, FormStateInterface $form_state, $form_id) {
// Specify main content groups with title and fields.
$main_content_groups = _convivial_gin_content_sidebar_groups(theme_get_setting('content_groups'));
// If main content groups is no empty.
if (!empty($main_content_groups)) {
// Loop for main content groups.
foreach ($main_content_groups as $mkey => $mgroup) {
$form[$mkey] = [
'#type' => 'details',
'#title' => Html::escape($mgroup['title']),
'#group' => 'content',
'#attributes' => [
'class' => ['main-content-group'],
],
'#attached' => [
'library' => ['node/drupal.node'],
],
'#weight' => 100,
'#optional' => TRUE,
'#open' => FALSE,
];
// Move fields to the content body column.
foreach ($mgroup['fields'] as $mfield) {
if (isset($form[$mfield]) && !empty($form[$mfield])) {
$form[$mfield]['#group'] = $mkey;
}
}
}
}
// Specify right sidebar groups with title and fields.
$sidebar_groups = _convivial_gin_content_sidebar_groups(theme_get_setting('sidebar_groups'));
// If sidebar groups is no empty.
if (!empty($sidebar_groups)) {
// Create groups in the right-hand column.
foreach ($sidebar_groups as $key => $group) {
$form[$key] = [
'#type' => 'details',
'#title' => Html::escape($group['title']),
'#group' => 'advanced',
'#attributes' => [
'class' => ['node-form-options'],
],
'#attached' => [
'library' => ['node/drupal.node'],
],
'#weight' => 100,
'#optional' => TRUE,
'#open' => FALSE,
];
// Move fields to the right-hand column.
foreach ($group['fields'] as $field) {
if (isset($form[$field]) && !empty($form[$field])) {
$form[$field]['#group'] = $key;
}
}
}
}
// Update field_custom_* boolean values.
$form['#validate'][] = '_convivial_gin_node_form_validate';
// Attach color palettes
$theme = \Drupal::config('system.theme')->getRawData();
$default_theme = $theme['default'];
if ($default_theme === 'convivial_bootstrap') {
_convivial_gin_theme_color_palette($form, 'convivial_bootstrap');
}
return $form;
}
// Create array for content and tab grouping.
function _convivial_gin_content_sidebar_groups($theme_settings) {
// Define array.
$content_tab_groups = [];
// Get value from theme settings.
$get_content_group = $theme_settings;
if (isset($get_content_group) && !empty($get_content_group)) {
// Create array by line break.
$content_group_explode = explode("\n", $get_content_group);
foreach ($content_group_explode as $groups) {
// Create array by | symbol.
$group = explode("|", $groups);
// Create group machine name using title.
$group_machine_name = Html::getUniqueId('group-' . $group[0]);
$group_fields = array_map('trim', explode(",", $group[1]));
// Node contents group.
$content_tab_groups[$group_machine_name]['title'] = Html::escape($group[0]);
$content_tab_groups[$group_machine_name]['fields'] = $group_fields;
}
}
return $content_tab_groups;
}
/**
* Node form validation handler.
*
* @see convivial_admin_form_node_form_alter()
*/
function _convivial_gin_node_form_validate(array &$form, FormStateInterface $form_state) {
/** @var \Drupal\Core\Entity\EntityFormInterface $form_object */
$form_object = $form_state->getFormObject();
/** @var \Drupal\Core\Entity\FieldableEntityInterface $entity */
$entity = $form_object->getEntity();
// Skip nodes without identifier field or if empty.
if (!$entity->hasField('field_identifier') || $entity->get('field_identifier')->isEmpty()) {
return;
}
// Update field_custom_* boolean values.
$fields_map = [
'title' => 'field_custom_title',
'field_description' => 'field_custom_description',
'field_url' => 'field_custom_url',
];
foreach ($fields_map as $key => $value) {
if ($entity->hasField($key) && $entity->hasField($value)) {
// Get node field and form values.
$field = $entity->get($key);
$field_definition = $field->getFieldDefinition();
$field_storage = $field_definition->getFieldStorageDefinition();
$property = $field_storage->getMainPropertyName();
$field_value = $field->getValue()[0][$property];
$form_value = $form_state->getValue($key)[0][$property];
// Set flag if value was changed.
if ((string) $field_value !== (string) $form_value) {
$form_state->setValue($value, ['value' => TRUE]);
}
}
}
}
/**
* Attach Color Palette feature.
*/
function _convivial_gin_theme_color_palette(&$form, $theme_name) {
$settings = theme_get_setting('colors', $theme_name);
$styles = '';
if (!empty($settings) && !empty($settings['colors_enable']) && $settings['colors_enable'] == 1) {
unset($settings['colors_enable']);
foreach ($settings as $palette) {
foreach ($palette['derivatives'] as $derivative_color_key => $derivative_color) {
$color_key = str_replace("_", "-", $derivative_color_key);
$styles .= "--$color_key: $derivative_color;";
}
}
}
if (!empty($styles)) {
$form['#attached']['html_head'][] = [
[
'#tag' => 'style',
'#attributes' => ['type' => 'text/css'],
'#value' => ":root { $styles }",
],
'theme-colors',
['weight' => 100],
];
}
}