-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathsync.module
More file actions
173 lines (156 loc) · 5.06 KB
/
sync.module
File metadata and controls
173 lines (156 loc) · 5.06 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
<?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() {
$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_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) {
foreach (\Drupal::service('plugin.manager.sync_resource')->getDefinitions() as $key => $definition) {
$definitions['sync_' . $key] = [
'id' => 'sync_' . $key,
'title' => $definition['label'],
'cron' => [
'time' => 180,
],
'class' => 'Drupal\sync\Plugin\QueueWorker\Sync',
'provider' => 'sync',
];
}
}
/**
* 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;
}
}