-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathewp_core.module
More file actions
230 lines (215 loc) · 7.05 KB
/
ewp_core.module
File metadata and controls
230 lines (215 loc) · 7.05 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
220
221
222
223
224
225
226
227
228
229
230
<?php
/**
* @file
* Contains ewp_core.module.
*/
use Drupal\Core\Entity\Sql\SqlContentEntityStorageException;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Link;
use Drupal\Core\Routing\RouteMatchInterface;
use Drupal\Core\Url;
/**
* Implements hook_help().
*/
function ewp_core_help($route_name, RouteMatchInterface $route_match) {
switch ($route_name) {
// Main module help for the ewp_core module.
case 'help.page.ewp_core':
$url = 'https://developers.erasmuswithoutpaper.eu';
$link = Link::fromTextAndUrl($url, Url::fromUri($url, [
'attributes' => [
'target' => '_blank',
],
]));
$output = '';
$output .= '<h3>' . t('About') . '</h3>';
$output .= '<p>' . t('EWP core data types and utilities') . '</p>';
$output .= '<p>' . t('Learn more at @link', [
'@link' => $link->toString(),
]) . '</p>';
return $output;
default:
}
}
/**
* Implements hook_element_info_alter().
*/
function ewp_core_element_info_alter(array &$types) {
// Attach our extra CSS for toolbar icons.
if (isset($types['toolbar'])) {
$types['toolbar']['#attached']['library'][] = 'ewp_core/toolbar';
}
}
/**
* Implements hook_field_widget_info_alter().
*/
function ewp_core_field_widget_info_alter(array &$info) {
// Anything that supports link fields should also work for HTTPS.
foreach ($info as $key => $widget_info) {
if (in_array('link', $widget_info['field_types'], TRUE)) {
$info[$key]['field_types'][] = 'ewp_https';
}
}
}
/**
* Implements hook_field_formatter_info_alter().
*/
function ewp_core_field_formatter_info_alter(array &$info) {
// Anything that supports link fields should also work for HTTPS.
foreach ($info as $key => $formatter_info) {
if (in_array('link', $formatter_info['field_types'], TRUE)) {
$info[$key]['field_types'][] = 'ewp_https';
}
}
}
/**
* Implements hook_theme().
*/
function ewp_core_theme() {
return [
'ewp_ascii_identifier_default' => [
'variables' => [
'value' => NULL,
],
],
'ewp_cefr_level_default' => [
'variables' => [
'value' => NULL,
'parent' => NULL,
],
],
'ewp_eqf_level_default' => [
'variables' => [
'value' => NULL,
],
],
'ewp_gender_code_default' => [
'variables' => [
'value' => NULL,
],
],
'ewp_https_default' => [
'variables' => [
'url' => NULL,
'title' => NULL,
],
],
'ewp_http_lang_default' => [
'variables' => [
'url' => NULL,
'title' => NULL,
'langtag' => NULL,
'langname' => NULL,
],
],
'ewp_multiline_lang_default' => [
'variables' => [
'multiline' => NULL,
'langtag' => NULL,
'langname' => NULL,
],
],
'ewp_string_lang_default' => [
'variables' => [
'string' => NULL,
'langtag' => NULL,
'langname' => NULL,
],
],
];
}
/**
* Implements hook_form_alter().
*/
function ewp_core_form_alter(&$form, FormStateInterface $form_state, $form_id) {
// Any necessary changes here.
}
/**
* Helper function to update the field schema to current.
*
* Source: https://gist.github.com/JPustkuchen/ce53d40303a51ca5f17ce7f48c363b9b
*
* Updates the field schema of all fields from the given $field_type_id
* to the current schema, preserving existing data by recreating the
* field tables & field revision tables.
* Hopefully this will one day be replaced by a similar core helper function.
* See https://www.drupal.org/project/entity_access_by_role_field/issues/336357
* core issue for future replacement field schema helper implementation.
*
* @param string $field_type_id
* The @FieldType id, e.g. "entity_access_by_role_field".
*/
function _ewp_core_field_type_schema_column_spec_change_helper(string $field_type_id): void {
$entity_field_manager = \Drupal::service('entity_field.manager');
$entity_field_map = $entity_field_manager->getFieldMapByFieldType($field_type_id);
$entity_type_manager = \Drupal::entityTypeManager();
foreach ($entity_field_map as $entity_type_id => $field_map) {
// This is always an SQL Entity Storage Interface:
/** @var \Drupal\Core\Entity\Sql\SqlEntityStorageInterface $entity_storage*/
$entity_storage = $entity_type_manager->getStorage($entity_type_id);
$entity_type = $entity_type_manager->getDefinition($entity_type_id);
$field_storage_definitions = $entity_field_manager->getFieldStorageDefinitions($entity_type_id);
/** @var Drupal\Core\Entity\Sql\DefaultTableMapping $table_mapping */
$table_mapping = $entity_storage->getTableMapping($field_storage_definitions);
// Only need field storage definitions of our field type:
/** @var \Drupal\Core\Field\FieldStorageDefinitionInterface $field_storage_definition */
foreach (array_intersect_key($field_storage_definitions, $field_map) as $field_storage_definition) {
$field_name = $field_storage_definition->getName();
$tables = [];
try {
$table = $table_mapping->getFieldTableName($field_name);
$tables[] = $table;
}
catch (SqlContentEntityStorageException $e) {
// Custom storage? Broken site? No matter what, if there is no table
// there's little we can do.
continue;
}
// See if the field has a revision table.
$revision_table = NULL;
if ($entity_type->isRevisionable() && $field_storage_definition->isRevisionable()) {
if ($table_mapping->requiresDedicatedTableStorage($field_storage_definition)) {
$revision_table = $table_mapping->getDedicatedRevisionTableName($field_storage_definition);
$tables[] = $revision_table;
}
elseif ($table_mapping->allowsSharedTableStorage($field_storage_definition)) {
$revision_table = $entity_type->getRevisionDataTable() ?: $entity_type->getRevisionTable();
$tables[] = $revision_table;
}
}
$database = \Drupal::database();
$existing_data = [];
foreach ($tables as $table) {
// Get the old data.
$existing_data[$table] = $database->select($table)
->fields($table)
->execute()
->fetchAll(PDO::FETCH_ASSOC);
// Wipe it.
$database->truncate($table)->execute();
}
$manager = \Drupal::entityDefinitionUpdateManager();
$manager->updateFieldStorageDefinition($manager->getFieldStorageDefinition($field_name, $entity_type_id));
// Restore the data.
foreach ($tables as $table) {
if (empty($existing_data[$table])) {
// Skip if there are no rows:
continue;
}
$first_row = reset($existing_data[$table]);
if (empty($first_row)) {
// Skip if there is no data:
continue;
}
$fields = array_keys($first_row);
$insert_query = $database
->insert($table)
->fields($fields);
foreach ($existing_data[$table] as $row) {
$insert_query->values(array_values($row));
}
$insert_query->execute();
}
}
}
}