This repository has been archived by the owner on Nov 1, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathlook.module
137 lines (110 loc) · 3.36 KB
/
look.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
<?php
/**
* @file
* Look module.
*/
use Drupal\Component\Utility\Html;
use Drupal\Core\Cache\Cache;
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Routing\RouteMatchInterface;
use Drupal\look\LookConfig;
/**
* Implements hook_help().
*/
function look_help($route_name, RouteMatchInterface $route_match) {
switch ($route_name) {
case 'entity.look.collection':
$output = '<p>' . t('Table contains looks grouped under parent looks. You can reorganize the looks using their drag-and-drop handles.') . '</p>';
return $output;
}
}
/**
* Implements hook_preprocess_HOOK().
*/
function look_preprocess_html(&$variables) {
// Add look config as cache context.
$variables['#cache']['contexts'][] = 'look';
$look = \Drupal::service('look.config')->get();
if (empty($look)) {
return;
}
$variables['#look'] = $look;
$classes = [
'look',
'look--' . $look['name'],
];
foreach ($classes as $class) {
$variables['attributes']['class'][] = Html::getClass($class);
}
// Get mappings for specific theme.
$look_config = \Drupal::config('look.settings');
$theme = \Drupal::service('theme.manager')->getActiveTheme()->getName();
$mappings = $look_config->get('mapping.' . $theme);
if (empty($mappings)) {
return;
}
// Initialize modifiers service.
/** @var \Drupal\modifiers\Modifiers $modifiers */
$modifiers = \Drupal::service('modifiers');
$modifications = [];
foreach ($mappings as $name => $mapping) {
$namespace = $modifiers->getShortField($name);
if (isset($look['config'][$namespace])) {
// Fill current namespace modifications.
$modifiers->process($modifications, $look['config'][$namespace], $mapping['selector']);
}
}
if (!empty($modifications)) {
$modifiers->apply($modifications, $variables, 'look-' . $look['id']);
}
}
/**
* Implements hook_entity_update().
*/
function look_entity_update(EntityInterface $entity) {
if ($entity->getEntityTypeId() === 'look') {
Cache::invalidateTags(['look:' . $entity->id()]);
}
}
/**
* Implements hook_form_FORM_ID_alter().
*/
function look_form_look_add_form_alter(&$form, FormStateInterface $form_state, $form_id) {
_look_form_field_look_theme_alter($form);
}
/**
* Implements hook_form_FORM_ID_alter().
*/
function look_form_look_edit_form_alter(&$form, FormStateInterface $form_state, $form_id) {
_look_form_field_look_theme_alter($form);
}
/**
* Implements hook_form_FORM_ID_alter().
*/
function look_form_block_form_alter(&$form, FormStateInterface $form_state, $form_id) {
if (isset($form['visibility']['look'])) {
$form['visibility']['look']['#attached']['library'][] = 'look/block-settings';
}
}
/**
* Convert field_look_theme widget to select with all available themes.
*/
function _look_form_field_look_theme_alter(&$form) {
if (!empty($form[LookConfig::THEME_FIELD])) {
$themes = \Drupal::service('theme_handler')->listInfo();
$options = [
'' => t('- None -'),
];
foreach ($themes as $key => $theme) {
if (empty($theme->info['hidden'])) {
$options[$key] = $theme->info['name'];
}
}
$element = &$form[LookConfig::THEME_FIELD]['widget'][0]['value'];
$element['#type'] = 'select';
$element['#options'] = $options;
$element['#default_value'] = [$element['#default_value']];
unset($element['#size']);
}
}