-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathmaterial_admin_support.install
More file actions
219 lines (193 loc) · 7.6 KB
/
material_admin_support.install
File metadata and controls
219 lines (193 loc) · 7.6 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
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
<?php
/**
* @file
* Contains material_admin_support.install.
*/
/**
* Implements hook_install().
*/
function material_admin_support_install() {
$entity_type_manager = \Drupal::entityTypeManager();
$module_handler = \Drupal::moduleHandler();
// Add type style configuration for common content types.
if ($module_handler->moduleExists('node')) {
$node_type_style_presets = [
'article' => [
'color' => '#039BE5',
'icon' => 'book',
],
'page' => [
'color' => '#f57f17',
'icon' => 'web_asset',
],
'product' => [
'color' => '#43A047',
'icon' => 'shopping_cart',
],
'landing_page' => [
'color' => '#009688',
'icon' => 'star',
],
];
/** @var \Drupal\node\NodeTypeInterface[] $content_types */
$content_types = $entity_type_manager
->getStorage('node_type')
->loadMultiple();
foreach ($content_types as $id => $type) {
if (empty($type->getThirdPartySettings('type_style')) && isset($node_type_style_presets[$id])) {
$type->setThirdPartySetting('type_style', 'color', $node_type_style_presets[$id]['color']);
$type->setThirdPartySetting('type_style', 'icon', $node_type_style_presets[$id]['icon']);
$type->save();
}
}
}
// Add type style configuration for common media types.
if ($module_handler->moduleExists('media')) {
$media_type_style_presets = [
'document' => [
'color' => '#43A047',
'icon' => 'insert_drive_file',
],
'image' => [
'color' => '#2196F3',
'icon' => 'insert_photo',
],
'video' => [
'color' => '#F44336',
'icon' => 'movie',
],
'tweet' => [
'color' => '#03A9F4',
'icon' => 'twitter',
],
'instagram' => [
'color' => '#AB47BC',
'icon' => 'instagram',
],
];
/** @var \Drupal\media\MediaTypeInterface[] $media_types */
$media_types = $entity_type_manager
->getStorage('media_type')
->loadMultiple();
foreach ($media_types as $id => $bundle) {
if (empty($bundle->getThirdPartySettings('type_style')) && isset($media_type_style_presets[$id])) {
$bundle->setThirdPartySetting('type_style', 'color', $media_type_style_presets[$id]['color']);
$bundle->setThirdPartySetting('type_style', 'icon', $media_type_style_presets[$id]['icon']);
$bundle->save();
}
}
}
// Add type style configuration for common moderation states and transitions.
if ($module_handler->moduleExists('type_style_moderation') && $module_handler->moduleExists('workbench_moderation')) {
$workbench_moderation_type_style_presets = [
'archived' => [
'color' => '#00897b',
'icon' => 'archive',
],
'draft' => [
'color' => '#EF6C00',
'icon' => 'edit',
],
'needs_review' => [
'color' => '#ff8f00',
'icon' => 'speaker_notes',
],
'published' => [
'color' => '#689f38',
'icon' => 'public',
],
];
/** @var \Drupal\workbench_moderation\ModerationStateInterface[] $moderation_states */
$moderation_states = $entity_type_manager
->getStorage('moderation_state')
->loadMultiple();
foreach ($moderation_states as $id => $state) {
if (empty($state->getThirdPartySettings('type_style')) && isset($workbench_moderation_type_style_presets[$id])) {
$state->setThirdPartySetting('type_style', 'color', $workbench_moderation_type_style_presets[$id]['color']);
$state->setThirdPartySetting('type_style', 'icon', $workbench_moderation_type_style_presets[$id]['icon']);
$state->save();
}
}
/** @var \Drupal\workbench_moderation\ModerationStateTransitionInterface[] $moderation_state_transitions */
$moderation_state_transitions = $entity_type_manager
->getStorage('moderation_state_transition')
->loadMultiple();
foreach ($moderation_state_transitions as $id => $transition) {
if (empty($transition->getThirdPartySettings('type_style')) && isset($workbench_moderation_type_style_presets[$transition->getToState()])) {
$transition->setThirdPartySetting('type_style', 'color', $workbench_moderation_type_style_presets[$transition->getToState()]['color']);
$transition->setThirdPartySetting('type_style', 'icon', $workbench_moderation_type_style_presets[$transition->getToState()]['icon']);
$transition->setSyncing(TRUE);
$transition->save();
}
}
}
// Add type style configuration for common commerce products.
if ($module_handler->moduleExists('commerce_product')) {
// Commerce product support.
$commerce_product_type_style_preset = [
'color' => '#d3d3d3',
'icon' => 'shopping_cart',
];
/** @var \Drupal\commerce_product\Entity\ProductTypeInterface[] $product_types */
$product_types = $entity_type_manager
->getStorage('commerce_product_type')
->loadMultiple();
foreach ($product_types as $id => $bundle) {
$bundle->setThirdPartySetting('type_style', 'color', $commerce_product_type_style_preset['color']);
$bundle->setThirdPartySetting('type_style', 'icon', $commerce_product_type_style_preset['icon']);
$bundle->save();
}
}
// Customize existing views.
if ($module_handler->moduleExists('views')) {
$storage = $entity_type_manager
->getStorage('view');
// Customize the 'content browser' view.
/** @var \Drupal\views\ViewEntityInterface $view */
if (($view = $storage->load('content_browser')) && $view !== NULL) {
material_admin_support_view_insert($view);
}
// Customize the 'media' view.
/** @var \Drupal\views\ViewEntityInterface $view */
if (($view = $storage->load('media')) && $view !== NULL) {
material_admin_support_view_insert($view);
}
$entity_browser_view_displays = [];
/** @var \Drupal\views\ViewEntityInterface[] $views */
$views = $storage->loadMultiple();
// Retrieve all entity browser view displays.
foreach ($views as $view_id => $view_data) {
foreach ($view_data->get('display') as $display_id => $display_definitions) {
// Verify that the view displays an entity browser.
if ($display_definitions['display_plugin'] !== 'entity_browser') {
continue;
};
// Check whether a display is overridden. If so, the master must be
// modified.
$display_options = array_filter($display_definitions['display_options']);
$display_to_process = empty($display_options['row']) ? 'default' : $display_id;
$entity_browser_view_displays[$view_id][$display_to_process] = $display_to_process;
}
}
// Alter the display options of each entity browser view, adding additional
// classes and styling.
foreach ($entity_browser_view_displays as $view_id => $display_ids) {
foreach ($display_ids as $display_id) {
$display = &$views[$view_id]->getDisplay($display_id);
$display['display_options']['style']['type'] = 'default';
$display['display_options']['style']['options']['row_class'] = 'browser--item';
if ($display_id !== 'default') {
$display['display_options']['defaults']['css_class'] = FALSE;
}
$display['display_options']['css_class'] = 'browser--row';
// Notify administrators that the view has been altered by logging a
// message.
\Drupal::logger('material_admin_support')->info('View config for display @display-id of @view-id view changed', [
'@display-id' => $display_id,
'@view-id' => $view_id,
]);
}
$views[$view_id]->save();
}
}
}