This repository has been archived by the owner on Nov 16, 2021. It is now read-only.
/
lightning_core.module
377 lines (334 loc) · 11.4 KB
/
lightning_core.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
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
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
<?php
/**
* @file
* Contains core functionality for the Lightning distribution.
*/
use Drupal\Core\Block\BlockPluginInterface;
use Drupal\Core\Config\FileStorage;
use Drupal\Core\Config\InstallStorage;
use Drupal\Core\Entity\EntityDisplayModeInterface;
use Drupal\Core\Entity\EntityFormInterface;
use Drupal\Core\Entity\Entity\EntityViewMode;
use Drupal\Core\Entity\FieldableEntityInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Render\Element;
use Drupal\Core\Routing\RouteMatchInterface;
use Drupal\node\NodeTypeInterface;
use Drupal\path\Plugin\Field\FieldType\PathFieldItemList;
use Drupal\user\Entity\Role;
use Drupal\user\RoleInterface;
/**
* Implements hook_help().
*/
function lightning_core_help($route_name, RouteMatchInterface $route_match) {
$matched = [];
// Parse the route name to figure out what display mode we're looking at:
// 0 is the entire string.
// 1 is 'view' or 'form'.
// 2 is the ID of the affected entity type.
// 3 is 'view_mode' or 'form_mode'.
// 4 is 'view' or 'form'.
$expr = '/^entity\.entity_(view|form)_display\.([a-z_]+)\.((view|form)_mode)$/';
if (preg_match($expr, $route_name, $matched)) {
$entity_id = $matched[2] . '.' . $route_match->getParameter($matched[3] . '_name');
$description = \Drupal::entityTypeManager()
->getStorage('entity_' . $matched[3])
->load($entity_id)
->getThirdPartySetting('lightning_core', 'description');
if ($description) {
return '<p>' . $description . '</p>';
}
}
}
/**
* Implements hook_modules_installed().
*/
function lightning_core_modules_installed(array $modules) {
if (in_array('token', $modules)) {
$view_modes = [];
foreach (\Drupal::entityTypeManager()->getDefinitions() as $entity_type) {
$view_modes[] = $entity_type->id() . '.token';
}
$view_modes = EntityViewMode::loadMultiple($view_modes);
/** @var \Drupal\Core\Entity\EntityViewModeInterface $view_mode */
foreach ($view_modes as $view_mode) {
$view_mode->setThirdPartySetting('lightning_core', 'internal', TRUE)
->save();
}
}
}
/**
* Implements hook_entity_load().
*/
function lightning_core_entity_load(array $entities) {
// Maintain a list of entity UUIDs whose aliases we've already looked up.
// We only want to look up a particular entity's alias once per request in
// order to prevent infinite loops (see issue #2831550).
static $looked_up = [];
/** @var \Drupal\Core\Path\AliasStorageInterface $alias_storage */
$alias_storage = \Drupal::service('path.alias_storage');
foreach ($entities as $entity) {
$uuid = $entity->uuid();
// If the entity has an empty path field, try to set its value. Amazingly,
// Path does not do this on its freaking own.
if (
empty($looked_up[$uuid]) &&
$entity instanceof FieldableEntityInterface &&
$entity->hasField('path') &&
$entity->path instanceof PathFieldItemList &&
$entity->path->isEmpty()
) {
$looked_up[$uuid] = TRUE;
$alias = $alias_storage->load([
'source' => '/' . $entity->toUrl()->getInternalPath(),
]);
if ($alias) {
$entity->path->setValue($alias);
}
}
}
}
/**
* Puts an associative array into an arbitrary order.
*
* @param array $values
* The array to reorder.
* @param array $keys
* The keys, in their desired order.
*/
function array_reorder(array &$values, array $keys) {
$keys = array_values($keys);
uksort($values, function ($a, $b) use ($keys) {
return array_search($a, $keys) - array_search($b, $keys);
});
}
/**
* Pre-render function to disable all buttons in a renderable element.
*
* @param array $element
* The renderable element.
*
* @return array
* The renderable element with all buttons (at all levels) disabled.
*
* @internal
*/
function lightning_core_disable_buttons(array $element) {
if (isset($element['#type'])) {
$element['#access'] = !in_array($element['#type'], [
'button',
'submit',
'image_button',
]);
}
// Recurse into child elements.
foreach (Element::children($element) as $key) {
if (is_array($element[$key])) {
$element[$key] = call_user_func(__FUNCTION__, $element[$key]);
}
}
return $element;
}
/**
* Implements hook_form_alter().
*/
function lightning_core_form_alter(array &$form, FormStateInterface $form_state) {
$form_object = $form_state->getFormObject();
if ($form_object instanceof EntityFormInterface) {
$supported_operations = ['default', 'add', 'edit'];
$entity = $form_object->getEntity();
if (($entity instanceof EntityDisplayModeInterface || $entity instanceof RoleInterface) && in_array($form_object->getOperation(), $supported_operations)) {
$form['description'] = [
'#type' => 'textarea',
'#title' => t('Description'),
'#description' => t('Additional relevant information about this @entity_type, such as where it is used and what it is for.', [
'@entity_type' => $entity->getEntityType()->getSingularLabel(),
]),
'#rows' => 2,
'#default_value' => $entity->getThirdPartySetting('lightning_core', 'description'),
];
$form['actions']['submit']['#submit'][] = 'lightning_core_set_third_party_description';
}
}
}
/**
* Implements hook_block_view_alter().
*/
function lightning_core_block_view_alter(array &$build, BlockPluginInterface $block) {
\Drupal::service('renderer')->addCacheableDependency($build, $block);
// Always add block_view:BASE_PLUGIN_ID as a cache tag.
$build['#cache']['tags'][] = 'block_view:' . $block->getBaseId();
// If the plugin is a derivative, add block_view:FULL_PLUGIN_ID as well.
if ($block->getDerivativeId()) {
$build['#cache']['tags'][] = 'block_view:' . $block->getPluginId();
}
}
/**
* Form submit handler: updates the description of a configuration entity.
*
* @param array $form
* The complete form.
* @param \Drupal\Core\Form\FormStateInterface $form_state
* The current form state.
*/
function lightning_core_set_third_party_description(array &$form, FormStateInterface $form_state) {
$form_state
->getFormObject()
->getEntity()
->setThirdPartySetting('lightning_core', 'description', $form_state->getValue('description'))
->save();
// The help text block is very likely to be render cached, so invalidate the
// relevant cache tag. See lightning_core_block_view_alter() and
// lightning_core_help().
\Drupal::service('cache_tags.invalidator')->invalidateTags(['block_view:help_block']);
}
/**
* Implements hook_form_FORM_ID_alter().
*/
function lightning_core_form_system_modules_alter(array &$form) {
$package = 'Lightning (Experimental)';
if (isset($form['modules'][$package])) {
$warning = t('Here may be dragons! Please be sure you <a href="@url">understand the potential risks</a> of experimental extensions before enabling one.', [
'@url' => 'http://lightning.acquia.com/lightning-experimental-modules',
]);
$form['modules'][$package]['#description'] = '<p>' . $warning . '</p>';
}
}
/**
* Implements hook_form_FORM_ID_alter().
*/
function lightning_core_form_user_form_alter(array &$form) {
if (isset($form['account']['roles'])) {
// Ensure the standard process functions run first.
\Drupal::service('lightning.form_helper')
->applyStandardProcessing($form['account']['roles']);
$form['account']['roles']['#process'][] = 'lightning_core_set_role_descriptions';
}
}
/**
* Element process function: sets descriptions for role checkboxes on user_form.
*
* @param array $element
* The element to process.
* @param \Drupal\Core\Form\FormStateInterface $form_state
* The current form state.
* @param array $complete_form
* The complete form.
*
* @return array
* The processed element.
*/
function lightning_core_set_role_descriptions(array $element, FormStateInterface $form_state, array &$complete_form) {
// Try to add a description for each individual role.
foreach (Element::children($element) as $role) {
// Don't overwrite any existing description.
if (empty($element[$role]['#description'])) {
$element[$role]['#description'] = Role::load($role)
->getThirdPartySetting('lightning_core', 'description');
}
}
return $element;
}
/**
* Implements hook_ENTITY_TYPE_insert().
*/
function lightning_core_node_type_insert(NodeTypeInterface $node_type) {
$config = \Drupal::config('lightning_core.settings')->get('content_roles');
if (empty($config)) {
return;
}
$permission_map = function ($permission) use ($node_type) {
return str_replace('?', $node_type->id(), $permission);
};
foreach ($config as $key => $info) {
if ($info['enabled']) {
Role::create([
'id' => $node_type->id() . '_' . $key,
'label' => str_replace('?', $node_type->label(), $info['label']),
'permissions' => array_map($permission_map, $info['permissions']),
])->save();
}
}
user_role_grant_permissions('content_manager', [
'create ' . $node_type->id() . ' content',
'delete any ' . $node_type->id() . ' content',
'edit any ' . $node_type->id() . ' content',
]);
}
/**
* Implements hook_ENTITY_TYPE_delete().
*/
function lightning_core_node_type_delete(NodeTypeInterface $node_type) {
$config = \Drupal::config('lightning_core.settings')->get('content_roles');
foreach (array_keys($config) as $key) {
$role = Role::load($node_type->id() . '_' . $key);
if ($role) {
$role->delete();
}
}
$role = Role::load('content_manager');
if ($role) {
user_role_revoke_permissions($role->id(), [
'create ' . $node_type->id() . ' content',
'delete any ' . $node_type->id() . ' content',
'edit any ' . $node_type->id() . ' content',
]);
}
}
/**
* Rebuilds the service container.
*/
function lightning_core_rebuild_container() {
require_once \Drupal::root() . '/core/includes/utility.inc';
$class_loader = \Drupal::service('class_loader');
$request = \Drupal::request();
drupal_rebuild($class_loader, $request);
}
/**
* Implements template_preprocess_block().
*/
function lightning_core_preprocess_block(array &$variables) {
$variables['attributes']['data-block-plugin-id'] = $variables['elements']['#plugin_id'];
}
/**
* Creates a config entity from default configuration.
*
* @param string $entity_type
* The config entity type ID.
* @param string $id
* The unprefixed entity ID.
* @param string $module
* (optional) The module which has the default configuration.
*/
function lightning_core_create_config($entity_type, $id, $module = 'lightning_core') {
$values = lightning_core_read_config(
\Drupal::entityTypeManager()->getDefinition($entity_type)->getConfigPrefix() . '.' . $id,
$module
);
if ($values) {
\Drupal::entityTypeManager()
->getStorage($entity_type)
->create($values)
->save();
}
}
/**
* Reads a stored config file from a module's config/install directory.
*
* @param string $id
* The config ID.
* @param string $module
* (optional) The module to search. Defaults to 'lightning_core'.
*
* @return array
* The config data.
*/
function lightning_core_read_config($id, $module = 'lightning_core') {
// Statically cache all FileStorage objects, keyed by module.
static $storage = [];
if (empty($storage[$module])) {
$dir = \Drupal::service('module_handler')->getModule($module)->getPath();
$storage[$module] = new FileStorage($dir . '/' . InstallStorage::CONFIG_INSTALL_DIRECTORY);
}
return $storage[$module]->read($id);
}