-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathscheduler.api.php
342 lines (322 loc) · 12.5 KB
/
scheduler.api.php
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
<?php
/**
* @file
* API documentation for the Scheduler module.
*
* Each of these hook functions has a general version which is invoked for all
* entity types, and a specific variant with _{type}_ in the name, invoked when
* processing that specific entity type.
*
* phpcs:disable DrupalPractice.CodeAnalysis.VariableAnalysis.UndefinedVariable
*/
/**
* @addtogroup hooks
* @{
*/
/**
* Hook function to add entity ids to the list being processed.
*
* This hook allows modules to add more entity ids into the list being processed
* in the current cron run. It is invoked during cron runs only. This function
* is retained for backwards compatibility but is superceded by the more
* flexible hook_scheduler_list_alter().
*
* @param string $process
* The process being done - 'publish' or 'unpublish'.
* @param string $entityTypeId
* The type of the entity being processed, for example 'node' or 'media'.
*
* @return array
* Array of ids to add to the existing list to be processed. Duplicates are
* removed when all hooks have been invoked.
*/
function hook_scheduler_list($process, $entityTypeId) {
$ids = [];
// Do some processing to add ids to the $ids array.
return $ids;
}
/**
* Entity-type specific version of hook_scheduler_list().
*
* The parameters and return value match the general variant of this hook. The
* $entityTypeId parameter is included for ease and consistency, but is not
* strictly necessary as it will always match the TYPE in the function name.
*/
function hook_scheduler_TYPE_list($process, $entityTypeId) {
}
/**
* Hook function to manipulate the list of entity ids being processed.
*
* This hook allows modules to add or remove entity ids from the list being
* processed in the current cron run. It is invoked during cron runs only.
*
* @param array $ids
* The array of entity ids being processed.
* @param string $process
* The process being done - 'publish' or 'unpublish'.
* @param string $entityTypeId
* The type of the entity being processed, for example 'node' or 'media'.
*/
function hook_scheduler_list_alter(array &$ids, $process, $entityTypeId) {
if ($process == 'publish' && $some_condition) {
// Set a publish_on date and add the id.
$entity->set('publish_on', \Drupal::time()->getRequestTime())->save();
$ids[] = $id;
}
if ($process == 'unpublish' && $some_other_condition) {
// Remove the id.
$ids = array_diff($ids, [$id]);
}
// No return is necessary because $ids is passed by reference. Duplicates are
// removed when all hooks have been invoked.
}
/**
* Entity-type specific version of hook_scheduler_list_alter().
*
* The parameters match the general variant of this hook.
*/
function hook_scheduler_TYPE_list_alter(array &$ids, $process, $entityTypeId) {
}
/**
* Hook function to deny publishing of an entity.
*
* This hook gives modules the ability to prevent publication of an entity. The
* entity may be scheduled, and an attempt to publish it will be made during the
* first cron run after the publishing time. If any implementation of this hook
* function returns FALSE the entity will not be published. Attempts to publish
* will continue on each subsequent cron run, and the entity will be published
* when no hook prevents it.
*
* @param \Drupal\Core\Entity\EntityInterface $entity
* The scheduled entity that is about to be published.
*
* @return bool|null
* FALSE if the entity should not be published. TRUE or NULL will not affect
* the outcome.
*/
function hook_scheduler_publishing_allowed(EntityInterface $entity) {
// Do some logic here ...
$allowed = !empty($entity->field_approved->value);
// If publication is denied then inform the user why. This message will be
// displayed during entity edit and save.
if (!$allowed) {
\Drupal::messenger()->addMessage(t('The content will only be published after approval.'), 'status', FALSE);
// If the time is in the past it means that the action has been prevented,
// so write a dblog message to show this.
if ($entity->publish_on->value <= \Drupal::time()->getRequestTime()) {
if ($entity->id() && $entity->hasLinkTemplate('canonical')) {
$link = $entity->toLink(t('View'))->toString();
}
\Drupal::logger('scheduler_api_test')->warning('Publishing of "%title" is prevented until approved.', [
'%title' => $entity->label(),
'link' => $link ?? NULL,
]);
}
}
return $allowed;
}
/**
* Entity-type specific version of hook_scheduler_publishing_allowed().
*
* The parameters and return match the general variant of this hook.
*/
function hook_scheduler_TYPE_publishing_allowed(EntityInterface $entity) {
}
/**
* Hook function to deny unpublishing of an entity.
*
* This hook gives modules the ability to prevent unpublication of an entity.
* The entity may be scheduled, and an attempt to unpublish it will be made
* during the first cron run after the unpublishing time. If any implementation
* of this hook function returns FALSE the entity will not be unpublished.
* Attempts to unpublish will continue on each subsequent cron run, and the
* entity will be unpublished when no hook prevents it.
*
* @param \Drupal\Core\Entity\EntityInterface $entity
* The scheduled entity that is about to be unpublished.
*
* @return bool|null
* FALSE if the entity should not be unpublished. TRUE or NULL will not affect
* the outcome.
*/
function hook_scheduler_unpublishing_allowed(EntityInterface $entity) {
$allowed = TRUE;
// Prevent unpublication of competitions if not all prizes have been claimed.
if ($entity->getEntityTypeId() == 'competition' && $items = $entity->field_competition_prizes->getValue()) {
$allowed = (bool) count($items);
// If unpublication is denied then inform the user why. This message will be
// displayed during entity edit and save.
if (!$allowed) {
\Drupal::messenger()->addMessage(t('The competition will only be unpublished after all prizes have been claimed.'));
}
}
return $allowed;
}
/**
* Entity-type specific version of hook_scheduler_unpublishing_allowed().
*
* The parameters and return match the general variant of this hook.
*/
function hook_scheduler_TYPE_unpublishing_allowed(EntityInterface $entity) {
}
/**
* Hook function to hide the Publish On field.
*
* This hook is called from scheduler_form_alter() when adding or editing an
* entity. It gives modules the ability to hide the scheduler publish_on input
* field so that a date may not be entered or changed. Note that it does not
* give the ability to force the field to be displayed, as that could override a
* more significant setting. It can only be used to hide the field.
*
* This hook was introduced for scheduler_content_moderation_integration.
* See https://www.drupal.org/project/scheduler/issues/2798689
*
* @param array $form
* An associative array containing the structure of the form, as used in
* hook_form_alter().
* @param \Drupal\Core\Form\FormStateInterface $form_state
* The current state of the form, as used in hook_form_alter().
* @param \Drupal\Core\Entity\EntityInterface $entity
* The entity object being added or edited.
*
* @return bool
* TRUE to hide the publish_on field.
* FALSE or NULL to leave the setting unchanged.
*/
function hook_scheduler_hide_publish_date(array $form, FormStateInterface $form_state, EntityInterface $entity) {
if ($some_condition) {
return TRUE;
}
}
/**
* Entity-type specific version of hook_scheduler_hide_publish_date().
*
* The parameters and return match the general variant of this hook.
*/
function hook_scheduler_TYPE_hide_publish_date(array $form, FormStateInterface $form_state, EntityInterface $entity) {
}
/**
* Hook function to hide the Unpublish On field.
*
* This hook is called from scheduler_form_alter() when adding or editing an
* entity. It gives modules the ability to hide the scheduler unpublish_on input
* field so that a date may not be entered or changed. Note that it does not
* give the ability to force the field to be displayed, as that could override a
* more significant setting. It can only be used to hide the field.
*
* This hook was introduced for scheduler_content_moderation_integration.
* See https://www.drupal.org/project/scheduler/issues/2798689
*
* @param array $form
* An associative array containing the structure of the form, as used in
* hook_form_alter().
* @param \Drupal\Core\Form\FormStateInterface $form_state
* The current state of the form, as used in hook_form_alter().
* @param \Drupal\Core\Entity\EntityInterface $entity
* The entity object being added or edited.
*
* @return bool
* TRUE to hide the unpublish_on field.
* FALSE or NULL to leave the setting unchanged.
*/
function hook_scheduler_hide_unpublish_date(array $form, FormStateInterface $form_state, EntityInterface $entity) {
if ($some_condition) {
return TRUE;
}
}
/**
* Entity-type specific version of hook_scheduler_hide_unpublish_date().
*
* The parameters and return match the general variant of this hook.
*/
function hook_scheduler_TYPE_hide_unpublish_date(array $form, FormStateInterface $form_state, EntityInterface $entity) {
}
/**
* Hook function to process the publish action for an entity.
*
* This hook is called from schedulerManger::publish() and allows other modules
* to process the publish action on the entity during a cron run. That module
* may require different functionality to be executed instead of the default
* publish action. If all of the invoked hook functions return 0 then Scheduler
* will process the entity using the default publish action, just as if no hook
* functions had been called.
*
* This hook was introduced for scheduler_content_moderation_integration.
* See https://www.drupal.org/project/scheduler/issues/2798689
*
* @param \Drupal\Core\Entity\EntityInterface $entity
* The scheduled entity that is about to be published.
*
* @return int
* 1 if this function has published the entity or performed other such action
* meaning that Scheduler should NOT process the default publish action.
* 0 if nothing has been done and Scheduler should process the default publish
* action just as if this hook function did not exist.
* -1 if an error has occurred and Scheduler should abandon processing this
* entity with no further action and move on to the next one.
*/
function hook_scheduler_publish_process(EntityInterface $entity) {
if ($big_problem) {
// Throw an exception here.
return -1;
}
if ($some_condition) {
// Do the publish processing here on the $entity.
$entity->setSomeValue();
return 1;
}
return 0;
}
/**
* Entity-type specific version of hook_scheduler_publish_process().
*
* The parameters and return match the general variant of this hook.
*/
function hook_scheduler_TYPE_publish_process(EntityInterface $entity) {
}
/**
* Hook function to process the unpublish action for an entity.
*
* This hook is called from schedulerManger::unpublish() and allows other
* modules to process the unpublish action on the entity during a cron run. That
* module may require different functionality to be executed instead of the
* default unpublish action. If all of the invoked hook functions return 0 then
* Scheduler will process the entity using the default unpublish action, just as
* if no hook functions had been called.
*
* This hook was introduced for scheduler_content_moderation_integration.
* See https://www.drupal.org/project/scheduler/issues/2798689
*
* @param \Drupal\Core\Entity\EntityInterface $entity
* The scheduled entity that is about to be unpublished.
*
* @return int
* 1 if this function has unpublished the entity or performed other actions
* meaning that Scheduler should NOT process the default unpublish action.
* 0 if nothing has been done and Scheduler should process the default
* unpublish action just as if this hook function did not exist.
* -1 if an error has occurred and Scheduler should abandon processing this
* entity with no further action and move on to the next one.
*/
function hook_scheduler_unpublish_process(EntityInterface $entity) {
if ($big_problem) {
// Throw an exception here.
return -1;
}
if ($some_condition) {
// Do the unpublish processing here on the $entity.
$entity->setSomeValue();
return 1;
}
return 0;
}
/**
* Entity-type specific version of hook_scheduler_unpublish_process().
*
* The parameters and return match the general variant of this hook.
*/
function hook_scheduler_TYPE_unpublish_process(EntityInterface $entity) {
}
/**
* @} End of "addtogroup hooks".
*/