-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathsync.module
More file actions
233 lines (215 loc) · 7.18 KB
/
Copy pathsync.module
File metadata and controls
233 lines (215 loc) · 7.18 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
231
232
233
<?php
/**
* @file
* Contains sync.module.
*/
use Drupal\Core\Entity\EntityChangedInterface;
use Drupal\Core\Routing\RouteMatchInterface;
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Entity\EntityFormInterface;
use Drupal\Core\Entity\FieldableEntityInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Url;
use Drupal\sync\Plugin\Field\FieldWidget\ReadonlyFieldWidget;
use Drupal\views\Plugin\views\query\QueryPluginBase;
use Drupal\views\ViewExecutable;
/**
* Implements hook_help().
*/
function sync_help($route_name, RouteMatchInterface $route_match) {
switch ($route_name) {
// Main module help for the sync module.
case 'help.page.sync':
$output = '';
$output .= '<h3>' . t('About') . '</h3>';
$output .= '<p>' . t('Data migration suite.') . '</p>';
return $output;
default:
}
}
/**
* Implements hook_cron().
*/
function sync_cron() {
// Sites driving sync from a dedicated runner (drush sync:cron) turn this off
// so that core cron does not build a competing run.
if (\Drupal::config('sync.settings')->get('cron_build') === FALSE) {
return;
}
$manager = \Drupal::service('plugin.manager.sync_resource');
foreach ($manager->getForCron() as $instance) {
/** @var \Drupal\sync\Plugin\SyncResourceInterface $instance */
$instance->build(['%sync_as_cron' => TRUE]);
}
}
/**
* Implements hook_entity_delete().
*/
function sync_entity_delete(EntityInterface $entity) {
\Drupal::service('sync.storage')->deleteByProperties([
'entity_id' => $entity->id(),
'entity_type' => $entity->getEntityTypeId(),
]);
}
/**
* Implements hook_entity_presave().
*/
function sync_entity_presave(EntityInterface $entity) {
if (isset($entity->__sync_changed) && $entity instanceof EntityChangedInterface) {
$entity->setChangedTime(\Drupal::time()->getRequestTime());
}
}
/**
* Implements hook_entity_insert().
*/
function sync_entity_insert(EntityInterface $entity) {
sync_entity_update($entity);
}
/**
* Implements hook_entity_update().
*/
function sync_entity_update(EntityInterface $entity) {
if (isset($entity->__sync_id)) {
\Drupal::service('sync.storage')->saveEntity($entity);
}
}
/**
* Implements hook_entity_operation().
*
* Offers a Lock/Unlock operation on any entity sync tracks. Locking is how an
* entity that has been edited locally is protected from being overwritten by
* the next run.
*/
function sync_entity_operation(EntityInterface $entity) {
// Locking is opt-in. Both of these are far cheaper than the storage lookup,
// which would otherwise run for every row of every entity listing.
if (!\Drupal::config('sync.settings')->get('lock_enabled')) {
return [];
}
if (!\Drupal::currentUser()->hasPermission('sync lock')) {
return [];
}
if ($entity->isNew()) {
return [];
}
$records = \Drupal::service('sync.storage')->loadByEntity($entity);
if (!$records) {
return [];
}
$locked = FALSE;
foreach ($records as $record) {
if (!empty($record->locked)) {
$locked = TRUE;
break;
}
}
return [
'sync_lock' => [
'title' => $locked ? t('Unlock from sync') : t('Lock from sync'),
'weight' => 50,
'url' => Url::fromRoute('sync.entity_lock', [
'sync_entity_type' => $entity->getEntityTypeId(),
'sync_entity_id' => $entity->id(),
], [
'query' => \Drupal::destination()->getAsArray(),
]),
],
];
}
/**
* Implements hook_field_widget_info_alter().
*/
function sync_field_widget_info_alter(array &$info) {
// Let the readonly field widget be useable on all field types.
$formatters = \Drupal::service('plugin.manager.field.formatter')->getDefinitions();
$field_types = [];
foreach ($formatters as $formatter) {
$field_types = array_merge($field_types, $formatter['field_types']);
}
$info['sync_readonly']['field_types'] = array_unique($field_types);
}
/**
* Implements hook_form_alter().
*/
function sync_form_alter(&$form, FormStateInterface $form_state, $form_id) {
// Check that this is an entity form.
$form_object = $form_state->getFormObject();
if ($form_object instanceof EntityFormInterface) {
$entity = $form_object->getEntity();
// Set access to readonly widget items based on their view access.
$storage = $form_state->getStorage();
if ($entity instanceof FieldableEntityInterface && isset($storage['form_display'])) {
/** @var \Drupal\Core\Entity\Display\EntityFormDisplayInterface $form_display */
$form_display = $storage['form_display'];
}
else {
return;
}
foreach ($form_display->getComponents() as $name => $options) {
$widget = $form_display->getRenderer($name);
if ($widget instanceof ReadonlyFieldWidget && $entity instanceof FieldableEntityInterface) {
$items = $entity->get($name);
$items->filterEmptyItems();
$form[$name]['#access'] = $items->access('view');
}
}
}
if ($form_id === 'system_cron_settings') {
$cron_url = Url::fromRoute('sync.cron', ['key' => \Drupal::state()->get('system.cron_key')], ['absolute' => TRUE])->toString();
$form['cron_url'] = [
'drupal' => $form['cron_url'],
'sync' => [
'#markup' => '<p>' . t('For long-running tasks that will immediately return a 204 response, go to <a href=":cron" class="system-cron-settings__link">@cron</a>', [':cron' => $cron_url, '@cron' => $cron_url]) . '</p>',
],
];
}
}
/**
* Implements hook_queue_info_alter().
*/
function sync_queue_info_alter(&$definitions) {
$config = \Drupal::config('sync.settings');
// Sites driving sync from a dedicated runner (drush sync:cron) set this to
// FALSE so core cron does not claim items out from under it. Omitting the
// 'cron' key entirely makes Cron::processQueues() skip the queue.
$cron_queue = $config->get('cron_queue');
$cron_queue = $cron_queue === NULL ? TRUE : (bool) $cron_queue;
// Bounds the per-queue processing budget so the inline-cron fallback in
// SyncSubscriber (used when exec() is disabled or drush isn't available)
// can't park an FPM worker for too long.
$cron_queue_time = (int) ($config->get('cron_queue_time') ?: 30);
foreach (\Drupal::service('plugin.manager.sync_resource')->getDefinitions() as $key => $definition) {
$info = [
'id' => 'sync_' . $key,
'title' => $definition['label'],
'class' => 'Drupal\sync\Plugin\QueueWorker\Sync',
'provider' => 'sync',
];
if ($cron_queue) {
$info['cron'] = ['time' => $cron_queue_time];
}
$definitions['sync_' . $key] = $info;
}
}
/**
* Implements hook_views_query_alter().
*/
function sync_views_query_alter(ViewExecutable $view, QueryPluginBase $query) {
if ($view->id() == 'sync_watchdog' && !empty($view->args[1])) {
// Show only logs since start of most recent run.
$query->addWhere('', 'watchdog.timestamp', $view->args[1], '>');
}
}
/**
* Implements hook_mail().
*/
function sync_mail($key, &$message, $params) {
switch ($key) {
case 'end_fail':
$message['headers']['Content-Type'] = 'text/html; charset=UTF-8; format=flowed';
$message['from'] = \Drupal::config('system.site')->get('mail');
$message['subject'] = $params['%subject'];
$message['body'][] = $params['%message'];
break;
}
}