-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathscheduler.module
1427 lines (1303 loc) · 65.8 KB
/
scheduler.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
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
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<?php
/**
* @file
* Scheduler publishes and unpublishes entities on dates specified by the user.
*/
use Drupal\Component\Plugin\Exception\PluginException;
use Drupal\Component\Utility\Xss;
use Drupal\Core\Action\Plugin\Action\UnpublishAction;
use Drupal\Core\Entity\Display\EntityViewDisplayInterface;
use Drupal\Core\Entity\EntityChangedInterface;
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Entity\EntityTypeInterface;
use Drupal\Core\Field\BaseFieldDefinition;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Routing\RouteMatchInterface;
use Drupal\Core\Url;
use Drupal\migrate_drupal\Plugin\migrate\source\DrupalSqlBase;
use Drupal\migrate\Exception\RequirementsException;
use Drupal\migrate\Plugin\MigrateSourceInterface;
use Drupal\migrate\Plugin\MigrationInterface;
use Drupal\migrate\Row;
use Drupal\workbench_moderation\Plugin\Action\ModerationOptOutPublishNode;
use Drupal\workbench_moderation\Plugin\Action\ModerationOptOutUnpublishNode;
/**
* Implements hook_help().
*/
function scheduler_help($route_name, RouteMatchInterface $route_match) {
$output = '';
switch ($route_name) {
case 'help.page.scheduler':
$output = '<h3>' . t('About') . '</h3>';
$output .= '<p>' . t('The Scheduler module provides the functionality for automatic publishing and unpublishing of entities, such and nodes and media items, at specified future dates.') . '</p>';
$output .= '<p>' . t('You can read more in the <a href="@readme">readme</a> file or our <a href="@project">project page on Drupal.org</a>.', [
'@readme' => $GLOBALS['base_url'] . '/' . \Drupal::service('extension.list.module')->getPath('scheduler') . '/README.md',
'@project' => 'https://drupal.org/project/scheduler',
]) . '</p>';
break;
case 'scheduler.cron_form':
$base_url = $GLOBALS['base_url'];
$access_key = \Drupal::config('scheduler.settings')->get('lightweight_cron_access_key');
$cron_url = $base_url . '/scheduler/cron/' . $access_key;
$output = '<p>' . t("When you have set up Drupal's standard crontab job cron.php then Scheduler will be executed during each cron run. However, if you would like finer granularity to scheduler, but don't want to run Drupal's cron more often then you can use the lightweight cron handler provided by Scheduler. This is an independent cron job which only runs the scheduler process and does not execute any cron tasks defined by Drupal core or any other modules.") . '</p>';
$output .= '<p>' . t("Scheduler's cron is at /scheduler/cron/{access-key} and a sample crontab entry to run scheduler every minute might look like:") . '</p>';
$output .= '<code>* * * * * wget -q -O /dev/null "' . $cron_url . '"</code>';
$output .= '<p>' . t('or') . '</p>';
$output .= '<code>* * * * * curl -s -o /dev/null "' . $cron_url . '"</code><br/><br/>';
break;
default:
}
return $output;
}
/**
* Implements hook_form_alter().
*/
function scheduler_form_alter(&$form, FormStateInterface $form_state, $form_id) {
$scheduler_manager = \Drupal::service('scheduler.manager');
if (in_array($form_id, $scheduler_manager->getEntityFormIds())) {
/** @var \Drupal\Core\Entity\EntityInterface $entity */
$entity = $form_state->getFormObject()->getEntity();
_scheduler_entity_form_alter($form, $form_state, $form_id, $entity);
}
elseif (in_array($form_id, $scheduler_manager->getEntityTypeFormIds())) {
_scheduler_entity_type_form_alter($form, $form_state, $form_id);
}
elseif ($entityTypeId = array_search($form_id, $scheduler_manager->getDevelGenerateFormIds())) {
// Devel Generate forms are different from the other types above. There is
// only one form id per entity type, but also no direct way to get the
// entity from the form. Hence we add the entityTypeId as a key in the array
// of returned possible form ids, and pass that on to the helper function.
_scheduler_devel_generate_form_alter($form, $form_state, $form_id, $entityTypeId);
}
elseif (in_array($form_id, ['media_library_add_form_oembed', 'media_library_add_form_upload'])) {
if (isset($form['media'])) {
$media = $form_state->get('media');
// Call the entity form alter function for each of the new media items
// being uploaded.
foreach ($media as $key => $entity) {
_scheduler_entity_form_alter($form['media'][$key]['fields'], $form_state, $form_id, $entity);
}
}
}
}
/**
* Form alter handling for entity forms - add and edit.
*/
function _scheduler_entity_form_alter(&$form, FormStateInterface $form_state, $form_id, $entity) {
// When a content type, such as a page, has an entity reference field linked
// to media items the Media Library module allows new media to be uploaded and
// inserted when adding/editing the page. The media upload form is not an
// 'entity' form and does not have a getFormDisplay() method. Hence we cannot
// properly amend the form in this scenario and the best we can do is remove
// the Scheduler fields for safety. The Scheduler issue discussing this is
// https://www.drupal.org/project/scheduler/issues/2916730
if (!method_exists($form_state->getFormObject(), 'getFormDisplay')) {
unset($form['publish_on']);
unset($form['unpublish_on']);
return;
}
// Get the form display object. If this does not exist because the form is
// prevented from displaying, such as in Commerce Add Product before any store
// has been created, we can not (and do not need to) do anything, so exit.
$param = ($form_id == 'media_library_add_form_upload') ? $entity : $form_state;
if (!$display = $form_state->getFormObject()->getFormDisplay($param)) {
return;
}
$config = \Drupal::config('scheduler.settings');
$scheduler_manager = \Drupal::service('scheduler.manager');
$entityTypeId = $entity->getEntityTypeId();
$publishing_enabled = $scheduler_manager->getThirdPartySetting($entity, 'publish_enable', $config->get('default_publish_enable'));
$unpublishing_enabled = $scheduler_manager->getThirdPartySetting($entity, 'unpublish_enable', $config->get('default_unpublish_enable'));
// If neither publishing nor unpublishing are enabled then there is nothing to
// do so remove the fields from the form and exit early.
if (!$publishing_enabled && !$unpublishing_enabled) {
unset($form['publish_on']);
unset($form['unpublish_on']);
return;
}
// Determine if the scheduler fields have been set to hidden (disabled).
$publishing_displayed = !empty($display->getComponent('publish_on'));
$unpublishing_displayed = !empty($display->getComponent('unpublish_on'));
// Invoke all implementations of hook_scheduler_hide_publish_date() and
// hook_scheduler_{type}_hide_publish_date() to allow other modules to hide
// the field on the entity edit form.
if ($publishing_enabled && $publishing_displayed) {
$hook_implementations = $scheduler_manager->getHookImplementations('hide_publish_date', $entity);
foreach ($hook_implementations as $function) {
$publishing_displayed = ($function($form, $form_state, $entity) !== TRUE) && $publishing_displayed;
}
}
// Invoke all implementations of hook_scheduler_hide_unpublish_date() and
// hook_scheduler_{type}_hide_unpublish_date() to allow other modules to hide
// the field on the entity edit form.
if ($unpublishing_enabled && $unpublishing_displayed) {
$hook_implementations = $scheduler_manager->getHookImplementations('hide_unpublish_date', $entity);
foreach ($hook_implementations as $function) {
$unpublishing_displayed = ($function($form, $form_state, $entity) !== TRUE) && $unpublishing_displayed;
}
}
// If both publishing and unpublishing are either not enabled or are hidden
// for this entity type then the only thing to do is remove the fields from
// the form, then exit.
if ((!$publishing_enabled || !$publishing_displayed) && (!$unpublishing_enabled || !$unpublishing_displayed)) {
unset($form['publish_on']);
unset($form['unpublish_on']);
return;
}
$allow_date_only = $config->get('allow_date_only');
// A publish_on date is required if the content type option is set and the
// entity is being created or it is currently not published but has a
// scheduled publishing date.
$publishing_required = $publishing_enabled
&& $scheduler_manager->getThirdPartySetting($entity, 'publish_required', $config->get('default_publish_required'))
&& ($entity->isNew() || (!$entity->isPublished() && !empty($entity->publish_on->value)));
// An unpublish_on date is required if the content type option is set and the
// entity is being created or the current status is published or the entity is
// scheduled to be published.
$unpublishing_required = $unpublishing_enabled
&& $scheduler_manager->getThirdPartySetting($entity, 'unpublish_required', $config->get('default_unpublish_required'))
&& ($entity->isNew() || $entity->isPublished() || !empty($entity->publish_on->value));
// Create a 'details' field group to wrap the scheduling fields, and expand it
// if publishing or unpublishing is required, if a date already exists or the
// fieldset is configured to be always expanded.
$has_data = isset($entity->publish_on->value) || isset($entity->unpublish_on->value);
$always_expand = $scheduler_manager->getThirdPartySetting($entity, 'expand_fieldset', $config->get('default_expand_fieldset')) === 'always';
$expand_details = $publishing_required || $unpublishing_required || $has_data || $always_expand;
// Create the group for the fields. The array key has to be distinct when more
// than one $entity appears in the form, for example in Media Library uploads.
// Keep the first key the same as before, without any suffix, as this is used
// in drupal.behaviors javascript and could be used by third-party modules.
static $group_number;
$group_number += 1;
$scheduler_field_group = ($group_number == 1) ? 'scheduler_settings' : "scheduler_settings_{$group_number}";
$form[$scheduler_field_group] = [
'#type' => 'details',
'#title' => t('Scheduling options'),
'#open' => $expand_details,
'#weight' => 35,
'#attributes' => ['class' => ['scheduler-form']],
'#optional' => FALSE,
];
// Attach the fields to group.
$form['publish_on']['#group'] = $form['unpublish_on']['#group'] = $scheduler_field_group;
// Show the field group as a vertical tab if this option is enabled.
$use_vertical_tabs = $scheduler_manager->getThirdPartySetting($entity, 'fields_display_mode', $config->get('default_fields_display_mode')) === 'vertical_tab';
if ($use_vertical_tabs) {
$form[$scheduler_field_group]['#group'] = 'advanced';
// Attach the javascript for the vertical tabs.
$form[$scheduler_field_group]['#attached']['library'][] = 'scheduler/vertical-tabs';
}
// The 'once' library was moved from jQuery into core at 9.2. The original js
// library file is kept to maintain compatibility with Drupal 8.9.
// @see https://www.drupal.org/project/scheduler/issues/3314158
$default_time_library = version_compare(\Drupal::VERSION, '9.2', '>=') ? 'scheduler/default-time' : 'scheduler/default-time-8x';
// Define the descriptions depending on whether the time can be skipped.
$descriptions = [];
if ($allow_date_only) {
$descriptions['format'] = t('Enter a date. The time part is optional.');
// Show the default time so users know what they will get if they do not
// enter a time.
$descriptions['default'] = t('The default time is @default_time.', [
'@default_time' => $config->get('default_time'),
]);
// Use javascript to pre-fill the time parts if the dates are required.
// See js/scheduler_default_time.js for more details.
if ($publishing_required || $unpublishing_required) {
$form[$scheduler_field_group]['#attached']['library'][] = $default_time_library;
$form[$scheduler_field_group]['#attached']['drupalSettings']['schedulerDefaultTime'] = $config->get('default_time');
}
}
else {
$descriptions['format'] = t('Enter a date and time.');
}
if (!$publishing_required) {
$descriptions['blank'] = t('Leave the date blank for no scheduled publishing.');
}
$form['publish_on']['#access'] = $publishing_enabled && $publishing_displayed;
$form['publish_on']['widget'][0]['value']['#required'] = $publishing_required;
$form['publish_on']['widget'][0]['value']['#description'] = Xss::filter(implode(' ', $descriptions));
if (!$unpublishing_required) {
$descriptions['blank'] = t('Leave the date blank for no scheduled unpublishing.');
}
else {
unset($descriptions['blank']);
}
$form['unpublish_on']['#access'] = $unpublishing_enabled && $unpublishing_displayed;
$form['unpublish_on']['widget'][0]['value']['#required'] = $unpublishing_required;
$form['unpublish_on']['widget'][0]['value']['#description'] = Xss::filter(implode(' ', $descriptions));
// When hiding the seconds on time input, we need to remove the seconds from
// the form value, as some browsers HTML5 rendering still show the seconds.
// We can use the same jQuery drupal behaviors file as for default time.
// This functionality is not covered by tests.
if ($config->get('hide_seconds')) {
// If there is a publish_on time, then use jQuery to remove the seconds.
if (isset($entity->publish_on->value)) {
$form[$scheduler_field_group]['#attached']['library'][] = $default_time_library;
$form[$scheduler_field_group]['#attached']['drupalSettings']['schedulerHideSecondsPublishOn'] = date('H:i', $entity->publish_on->value);
}
// Likewise for the unpublish_on time.
if (isset($entity->unpublish_on->value)) {
$form[$scheduler_field_group]['#attached']['library'][] = $default_time_library;
$form[$scheduler_field_group]['#attached']['drupalSettings']['schedulerHideSecondsUnpublishOn'] = date('H:i', $entity->unpublish_on->value);
}
}
// Check the permission for entering scheduled dates.
$permission = $scheduler_manager->permissionName($entityTypeId, 'schedule');
if (!\Drupal::currentUser()->hasPermission($permission)) {
// Do not show the scheduler fields for users who do not have permission.
// Setting #access to FALSE for the group fieldset is enough to hide the
// fields. Setting FALSE for the individual fields is necessary to keep any
// existing scheduled dates preserved and remain unchanged on saving.
$form[$scheduler_field_group]['#access'] = FALSE;
$form['publish_on']['#access'] = FALSE;
$form['unpublish_on']['#access'] = FALSE;
// @todo Find a more elegant solution for bypassing the validation of
// scheduler fields when the user does not have permission.
// Note: This scenario is NOT yet covered by any tests, neither in
// SchedulerPermissionsTest.php nor SchedulerRequiredTest.php
// @see https://www.drupal.org/node/2651448
$form['publish_on']['widget'][0]['value']['#required'] = FALSE;
$form['unpublish_on']['widget'][0]['value']['#required'] = FALSE;
}
// Check which widget is set for the scheduler fields, and give a warning and
// provide a hint and link for how to fix it. Allow third-party modules to
// provide their own custom widget, we are only interested in checking that it
// has not reverted back to the core 'datetime_timestamp' widget.
$pluginDefinitions = $display->get('pluginManager')->getDefinitions();
$fields_to_check = [];
if ($publishing_enabled && $publishing_displayed) {
$fields_to_check[] = 'publish_on';
}
if ($unpublishing_enabled && $unpublishing_displayed) {
$fields_to_check[] = 'unpublish_on';
}
$correct_widget_id = 'datetime_timestamp_no_default';
foreach ($fields_to_check as $field) {
$actual_widget_id = $display->getComponent($field)['type'];
if ($actual_widget_id == 'datetime_timestamp') {
$link = \Drupal::moduleHandler()->moduleExists('field_ui') ?
Url::fromRoute("entity.entity_form_display.$entityTypeId.default", ["{$entityTypeId}_type" => $entity->bundle()])->toString()
: '#';
\Drupal::messenger()->addMessage(t('The widget for field %field is incorrectly set to %wrong. This should be changed to %correct by an admin user via the <a href="@link">Field UI form display</a> :not_available', [
'%field' => (string) $form[$field]['widget']['#title'],
'%correct' => (string) $pluginDefinitions[$correct_widget_id]['label'],
'%wrong' => (string) $pluginDefinitions[$actual_widget_id]['label'],
'@link' => $link,
':not_available' => \Drupal::moduleHandler()->moduleExists('field_ui') ? '' : ('(' . t('not available') . ')'),
]), 'warning', FALSE);
}
}
}
/**
* Form alter handling for entity type forms.
*/
function _scheduler_entity_type_form_alter(&$form, FormStateInterface $form_state, $form_id) {
$config = \Drupal::config('scheduler.settings');
/** @var \Drupal\Core\Entity\EntityTypeInterface $type */
$type = $form_state->getFormObject()->getEntity();
/** @var Drupal\Core\Entity\ContentEntityTypeInterface $contentEntityType */
$contentEntityType = \Drupal::entityTypeManager()->getDefinition($type->getEntityType()->getBundleOf());
/** @var \Drupal\Core\Entity\ContentEntityInterface $contentEntity */
$contentEntity = \Drupal::entityTypeManager()->getStorage($contentEntityType->id())->create([$contentEntityType->getKey('bundle') => 'scaffold']);
$params = [
'@type' => $type->label() ?? '',
'%type' => strtolower($type->label() ?? ''),
'@singular' => $contentEntityType->getSingularLabel(),
'@plural' => $contentEntityType->getPluralLabel(),
];
$form['#attached']['library'][] = 'scheduler/vertical-tabs';
$form['scheduler'] = [
'#type' => 'details',
'#title' => t('Scheduler'),
'#weight' => 35,
'#group' => 'additional_settings',
];
// Publishing options.
$form['scheduler']['publish'] = [
'#type' => 'details',
'#title' => t('Publishing'),
'#weight' => 1,
'#group' => 'scheduler',
'#open' => TRUE,
];
$form['scheduler']['publish']['scheduler_publish_enable'] = [
'#type' => 'checkbox',
'#title' => t('Enable scheduled publishing for %type @plural', $params),
'#default_value' => $type->getThirdPartySetting('scheduler', 'publish_enable', $config->get('default_publish_enable')),
];
$form['scheduler']['publish']['scheduler_publish_touch'] = [
'#type' => 'checkbox',
'#title' => t('Change %type creation time to match the scheduled publish time', $params),
'#default_value' => $type->getThirdPartySetting('scheduler', 'publish_touch', $config->get('default_publish_touch')),
'#states' => [
'visible' => [
':input[name="scheduler_publish_enable"]' => ['checked' => TRUE],
],
],
];
// Entity types that do not implement the 'getCreatedTime' method should have
// the option set to FALSE and the field disabled and hidden.
if (!method_exists($contentEntity, 'getCreatedTime')) {
$form['scheduler']['publish']['scheduler_publish_touch']['#disabled'] = TRUE;
$form['scheduler']['publish']['scheduler_publish_touch']['#description'] = t('The entity type does not support the change of creation time.');
$form['scheduler']['publish']['scheduler_publish_touch']['#default_value'] = FALSE;
$form['scheduler']['publish']['scheduler_publish_touch']['#access'] = FALSE;
}
$form['scheduler']['publish']['scheduler_publish_required'] = [
'#type' => 'checkbox',
'#title' => t('Require scheduled publishing'),
'#default_value' => $type->getThirdPartySetting('scheduler', 'publish_required', $config->get('default_publish_required')),
'#states' => [
'visible' => [
':input[name="scheduler_publish_enable"]' => ['checked' => TRUE],
],
],
];
if ($contentEntityType->isRevisionable()) {
$form['scheduler']['publish']['scheduler_publish_revision'] = [
'#type' => 'checkbox',
'#title' => t('Create a new revision on publishing'),
'#default_value' => $type->getThirdPartySetting('scheduler', 'publish_revision', $config->get('default_publish_revision')),
'#states' => [
'visible' => [
':input[name="scheduler_publish_enable"]' => ['checked' => TRUE],
],
],
];
}
$form['scheduler']['publish']['advanced'] = [
'#type' => 'details',
'#title' => t('Advanced options'),
'#open' => FALSE,
'#states' => [
'visible' => [
':input[name="scheduler_publish_enable"]' => ['checked' => TRUE],
],
],
];
$form['scheduler']['publish']['advanced']['scheduler_publish_past_date'] = [
'#type' => 'radios',
'#title' => t('Action to be taken for publication dates in the past'),
'#default_value' => $type->getThirdPartySetting('scheduler', 'publish_past_date', $config->get('default_publish_past_date')),
'#options' => [
'error' => t('Display an error message - do not allow dates in the past'),
'publish' => t('Publish the %type @singular immediately after saving', $params),
'schedule' => t('Schedule the %type @singular for publication on the next cron run', $params),
],
];
$form['scheduler']['publish']['advanced']['scheduler_publish_past_date_created'] = [
'#type' => 'checkbox',
'#title' => t('Change %type creation time to match the published time, for dates before the %type was created', $params),
'#description' => t("The created time will only be altered when the scheduled publishing time is earlier than the existing creation time"),
'#default_value' => $type->getThirdPartySetting('scheduler', 'publish_past_date_created', $config->get('default_publish_past_date_created')),
// This option is not relevant if the full 'change creation time' option is
// selected, or when past dates are not allowed. Hence only show it when
// the main option is not checked and the past dates option is not 'error'.
'#states' => [
'visible' => [
':input[name="scheduler_publish_touch"]' => ['checked' => FALSE],
':input[name="scheduler_publish_past_date"]' => ['!value' => 'error'],
],
],
];
// Entity types that do not implement the 'getCreatedTime' method should have
// the option set to FALSE and the field disabled. It will be hidden due to
// the #states setting above, as scheduler_publish_touch has #access = FALSE.
if (!method_exists($contentEntity, 'getCreatedTime')) {
$form['scheduler']['publish']['advanced']['scheduler_publish_past_date_created']['#disabled'] = TRUE;
$form['scheduler']['publish']['advanced']['scheduler_publish_past_date_created']['#description'] = t('The entity type does not support the change of creation time.');
$form['scheduler']['publish']['advanced']['scheduler_publish_past_date_created']['#default_value'] = FALSE;
}
// Unpublishing options.
$form['scheduler']['unpublish'] = [
'#type' => 'details',
'#title' => t('Unpublishing'),
'#weight' => 2,
'#group' => 'scheduler',
'#open' => TRUE,
];
$form['scheduler']['unpublish']['scheduler_unpublish_enable'] = [
'#type' => 'checkbox',
'#title' => t('Enable scheduled unpublishing for %type @plural', $params),
'#default_value' => $type->getThirdPartySetting('scheduler', 'unpublish_enable', $config->get('default_unpublish_enable')),
];
$form['scheduler']['unpublish']['scheduler_unpublish_required'] = [
'#type' => 'checkbox',
'#title' => t('Require scheduled unpublishing'),
'#default_value' => $type->getThirdPartySetting('scheduler', 'unpublish_required', $config->get('default_unpublish_required')),
'#states' => [
'visible' => [
':input[name="scheduler_unpublish_enable"]' => ['checked' => TRUE],
],
],
];
if ($contentEntityType->isRevisionable()) {
$form['scheduler']['unpublish']['scheduler_unpublish_revision'] = [
'#type' => 'checkbox',
'#title' => t('Create a new revision on unpublishing'),
'#default_value' => $type->getThirdPartySetting('scheduler', 'unpublish_revision', $config->get('default_unpublish_revision')),
'#states' => [
'visible' => [
':input[name="scheduler_unpublish_enable"]' => ['checked' => TRUE],
],
],
];
}
// The 'entity_edit_layout' fieldset contains options to alter the layout of
// entity edit pages.
$form['scheduler']['entity_edit_layout'] = [
'#type' => 'details',
'#title' => t('@type edit page', $params),
'#weight' => 3,
'#group' => 'scheduler',
// The #states processing only caters for AND and does not do OR. So to set
// the state to visible if either of the boxes are ticked we use the fact
// that logical 'X = A or B' is equivalent to 'not X = not A and not B'.
'#states' => [
'!visible' => [
':input[name="scheduler_publish_enable"]' => ['!checked' => TRUE],
':input[name="scheduler_unpublish_enable"]' => ['!checked' => TRUE],
],
],
];
$form['scheduler']['entity_edit_layout']['scheduler_fields_display_mode'] = [
'#type' => 'radios',
'#title' => t('Display scheduling date input fields in'),
'#default_value' => $type->getThirdPartySetting('scheduler', 'fields_display_mode', $config->get('default_fields_display_mode')),
'#options' => [
'vertical_tab' => t('Vertical tab'),
'fieldset' => t('Separate fieldset'),
],
'#description' => t('Use this option to specify how the scheduler fields are displayed when editing %type @plural', $params),
];
$form['scheduler']['entity_edit_layout']['scheduler_expand_fieldset'] = [
'#type' => 'radios',
'#title' => t('Expand fieldset or vertical tab'),
'#default_value' => $type->getThirdPartySetting('scheduler', 'expand_fieldset', $config->get('default_expand_fieldset')),
'#options' => [
'when_required' => t('Expand only when a scheduled date exists or when a date is required'),
'always' => t('Always open the fieldset or vertical tab'),
],
];
$form['scheduler']['entity_edit_layout']['scheduler_show_message_after_update'] = [
'#type' => 'checkbox',
'#prefix' => '<strong>' . t('Show message') . '</strong>',
'#title' => t('Show a confirmation message when a scheduled %type @singular is saved', $params),
'#default_value' => $type->getThirdPartySetting('scheduler', 'show_message_after_update', $config->get('default_show_message_after_update')),
];
$form['#entity_builders'][] = '_scheduler_form_entity_type_form_builder';
// Add a custom submit handler to adjust the fields in the form displays.
$form['actions']['submit']['#submit'][] = '_scheduler_form_entity_type_submit';
}
/**
* Entity builder for the entity type form with scheduler options.
*/
function _scheduler_form_entity_type_form_builder($entity_type, $type, &$form, FormStateInterface $form_state) {
$type->setThirdPartySetting('scheduler', 'expand_fieldset', $form_state->getValue('scheduler_expand_fieldset'));
$type->setThirdPartySetting('scheduler', 'fields_display_mode', $form_state->getValue('scheduler_fields_display_mode'));
$type->setThirdPartySetting('scheduler', 'publish_enable', $form_state->getValue('scheduler_publish_enable'));
$type->setThirdPartySetting('scheduler', 'publish_past_date', $form_state->getValue('scheduler_publish_past_date'));
$type->setThirdPartySetting('scheduler', 'publish_past_date_created', $form_state->getValue('scheduler_publish_past_date_created'));
$type->setThirdPartySetting('scheduler', 'publish_required', $form_state->getValue('scheduler_publish_required'));
$type->setThirdPartySetting('scheduler', 'publish_revision', $form_state->getValue('scheduler_publish_revision'));
$type->setThirdPartySetting('scheduler', 'publish_touch', $form_state->getValue('scheduler_publish_touch'));
$type->setThirdPartySetting('scheduler', 'show_message_after_update', $form_state->getValue('scheduler_show_message_after_update'));
$type->setThirdPartySetting('scheduler', 'unpublish_enable', $form_state->getValue('scheduler_unpublish_enable'));
$type->setThirdPartySetting('scheduler', 'unpublish_required', $form_state->getValue('scheduler_unpublish_required'));
$type->setThirdPartySetting('scheduler', 'unpublish_revision', $form_state->getValue('scheduler_unpublish_revision'));
}
/**
* Entity type form submit handler.
*/
function _scheduler_form_entity_type_submit($form, FormStateInterface $form_state) {
// Get the entity type id (node, media, taxonomy_term, etc.)
$entity_type_id = $form_state->getFormObject()->getEntity()->getEntityType()->getBundleOf();
// Get the entity bundle id (page, article, image, etc.)
$bundle_id = $form_state->getFormObject()->getEntity()->id();
/** @var \Drupal\Core\Entity\EntityDisplayRepositoryInterface $display_repository */
$display_repository = \Drupal::service('entity_display.repository');
// Get all active display modes. getFormModes() returns the additional modes
// then add the default.
$all_display_modes = array_keys($display_repository->getFormModes($entity_type_id));
$all_display_modes[] = $display_repository::DEFAULT_DISPLAY_MODE;
$supported_display_modes = \Drupal::service('scheduler.manager')->getPlugin($entity_type_id)->entityFormDisplayModes();
// Each of the active form display modes may need to be adjusted to add or
// remove the scheduler fields depending on the 'enable' setting.
foreach ($all_display_modes as $display_mode) {
$form_display = $display_repository->getFormDisplay($entity_type_id, $bundle_id, $display_mode);
// If this bundle is not enabled for scheduled publishing or the form
// display mode is not supported then make sure the publish_on field is
// disabled in the form display.
if (!$form_state->getValue('scheduler_publish_enable') || !in_array($display_mode, $supported_display_modes)) {
$form_display->removeComponent('publish_on')->save();
}
// @todo Find a more robust way to detect that the checkbox was off before.
elseif (!$form['scheduler']['publish']['scheduler_publish_enable']['#default_value']) {
// If the entity bundle is now enabled for scheduled publishing but was
// not enabled before then set the publish_on field to be displayed and
// set the widget type to 'datetime_timestamp_no_default'. Only do this
// when the checkbox has been changed, because the widget could be altered
// or the field moved by a subsequent process or manually by admin.
$form_display->setComponent('scheduler_settings', ['weight' => 50])
->setComponent('publish_on', ['type' => 'datetime_timestamp_no_default', 'weight' => 52])->save();
}
// Do the same for the unpublish_on field.
if (!$form_state->getValue('scheduler_unpublish_enable') || !in_array($display_mode, $supported_display_modes)) {
$form_display->removeComponent('unpublish_on')->save();
}
elseif (!$form['scheduler']['unpublish']['scheduler_unpublish_enable']['#default_value']) {
$form_display->setComponent('scheduler_settings', ['weight' => 50])
->setComponent('unpublish_on', ['type' => 'datetime_timestamp_no_default', 'weight' => 54])->save();
}
// If the display mode is not supported then also remove the
// scheduler_settings group fieldset.
if (!in_array($display_mode, $supported_display_modes)) {
$form_display->removeComponent('scheduler_settings')->save();
}
}
}
/**
* Form alter handling for Devel Generate forms.
*/
function _scheduler_devel_generate_form_alter(array &$form, FormStateInterface $form_state, $form_id, $entityTypeId) {
// Show which types are enabled for scheduled publishing and unpublishing. If
// the form does not have a table but has a selection list instead, then
// nothing is added here.
$type_table = $entityTypeId . '_types';
if (isset($form[$type_table]['#header']) && isset($form[$type_table]['#options'])) {
// Add an extra column to the table to show which types are enabled for
// scheduled publishing and unpublishing.
$scheduler_manager = \Drupal::service('scheduler.manager');
$publishing_enabled_types = $scheduler_manager->getEnabledTypes($entityTypeId, 'publish');
$unpublishing_enabled_types = $scheduler_manager->getEnabledTypes($entityTypeId, 'unpublish');
$form[$type_table]['#header']['scheduler'] = t('Scheduler settings');
foreach (array_keys($form[$type_table]['#options']) as $type) {
$items = [];
if (in_array($type, $publishing_enabled_types)) {
$items[] = t('Enabled for publishing');
}
if (in_array($type, $unpublishing_enabled_types)) {
$items[] = t('Enabled for unpublishing');
}
if (empty($items)) {
$scheduler_settings = t('None');
}
else {
$scheduler_settings = [
'data' => [
'#theme' => 'item_list',
'#items' => $items,
],
];
}
$form[$type_table]['#options'][$type]['scheduler'] = $scheduler_settings;
}
}
if (!isset($form['time_range'])) {
// Add the time range field if it was not added by Devel Generate.
$options = [1 => t('Now')];
foreach ([3600, 86400, 604800, 2592000, 31536000] as $interval) {
$options[$interval] = \Drupal::service('date.formatter')->formatInterval($interval, 1);
}
$form['time_range'] = [
'#type' => 'select',
'#title' => t('How far into the future should the items be scheduled?'),
'#description' => t('Scheduled dates will be set randomly within the selected time span.'),
'#options' => $options,
'#default_value' => 86400,
];
}
// Add form items to specify what proportion of generated entities should have
// a publish-on and/or unpublish-on date assigned. See hook_entity_presave()
// for the code that sets these values in the generated entity. Allow for any
// previously-executed form_alter to have already set the percentages.
$form['scheduler_publishing'] = [
'#type' => 'number',
'#title' => t('Publishing date for Scheduler'),
'#description' => t('Enter a percentage for randomly selecting Scheduler-enabled entities to be given a publish-on date. Enter 0 for none, 100 for all. The date and time will be random within the range starting at entity creation date, up to a time in the future matching the same span as selected above.'),
'#default_value' => $form['scheduler_publishing']['#default_value'] ?? 50,
'#required' => TRUE,
'#min' => 0,
'#max' => 100,
];
$form['scheduler_unpublishing'] = [
'#type' => 'number',
'#title' => t('Unpublishing date for Scheduler'),
'#description' => t('Enter a percentage for randomly selecting Scheduler-enabled entities to be given an unpublish-on date. Enter 0 for none, 100 for all. The date and time will be random within the range starting at the later of entity creation date and publish-on date, up to a time in the future matching the same span as selected above.'),
'#default_value' => $form['scheduler_unpublishing']['#default_value'] ?? 50,
'#required' => TRUE,
'#min' => 0,
'#max' => 100,
];
}
/**
* Implements hook_form_FORM_ID_alter() for language_content_settings_form.
*/
function scheduler_form_language_content_settings_form_alter(array &$form, FormStateInterface $form_state) {
// Add our validation function for the translation field settings form at
// admin/config/regional/content-language
// This hook function caters for all entity types, not just nodes.
$form['#validate'][] = '_scheduler_translation_validate';
}
/**
* Validation handler for language_content_settings_form.
*
* For each entity type, if it is translatable and also enabled for Scheduler,
* but the translation setting for the publish_on / unpublish_on field does not
* match the 'published status' field setting then throw a validation error.
*
* @see https://www.drupal.org/project/scheduler/issues/2871164
*/
function _scheduler_translation_validate($form, FormStateInterface $form_state) {
$settings = $form_state->getValues()['settings'];
/** @var \Drupal\scheduler\SchedulerManager $scheduler_manager */
$scheduler_manager = \Drupal::service('scheduler.manager');
foreach ($settings as $entity_type => $content_types) {
$publishing_enabled_types = $scheduler_manager->getEnabledTypes($entity_type, 'publish');
$unpublishing_enabled_types = $scheduler_manager->getEnabledTypes($entity_type, 'unpublish');
if (empty($publishing_enabled_types) && empty($publishing_enabled_types)) {
continue;
}
$enabled = [];
foreach ($content_types as $name => $options) {
$enabled['publish_on'] = in_array($name, $publishing_enabled_types);
$enabled['unpublish_on'] = in_array($name, $unpublishing_enabled_types);
if ($options['translatable'] && ($enabled['publish_on'] || $enabled['unpublish_on'])) {
$params = [
'@entity' => $form['settings'][$entity_type]['#bundle_label'],
'@type' => $form['settings'][$entity_type][$name]['settings']['#label'],
'%status' => $form['settings'][$entity_type][$name]['fields']['status']['#label'],
];
foreach (['publish_on', 'unpublish_on'] as $var) {
$mismatch = $enabled[$var] && ($options['fields'][$var] <> $options['fields']['status']);
if ($mismatch) {
$params['%scheduler_field'] = $form['settings'][$entity_type][$name]['fields'][$var]['#label'];
$message = t("There is a problem with @entity '@type' - The translatable settings for status field '%status' and Scheduler field '%scheduler_field' should match, either both on or both off", $params);
$form_state->setErrorByName("settings][$entity_type][$name][fields][status", $message);
$form_state->setErrorByName("settings][$entity_type][$name][fields][$var", $message);
}
}
}
}
}
}
/**
* Implements hook_entity_base_field_info().
*/
function scheduler_entity_base_field_info(EntityTypeInterface $entity_type) {
$fields = [];
$entity_types = \Drupal::service('scheduler.manager')->getPluginEntityTypes();
if (in_array($entity_type->id(), $entity_types)) {
$fields['publish_on'] = BaseFieldDefinition::create('timestamp')
->setLabel(t('Publish on'))
->setDisplayOptions('form', [
'type' => 'datetime_timestamp_no_default',
'region' => 'hidden',
])
->setDisplayConfigurable('form', TRUE)
->setTranslatable(TRUE)
->setRevisionable(TRUE)
->addConstraint('SchedulerPublishOn');
$fields['unpublish_on'] = BaseFieldDefinition::create('timestamp')
->setLabel(t('Unpublish on'))
->setDisplayOptions('form', [
'type' => 'datetime_timestamp_no_default',
'region' => 'hidden',
])
->setDisplayConfigurable('form', TRUE)
->setTranslatable(TRUE)
->setRevisionable(TRUE)
->addConstraint('SchedulerUnpublishOn');
}
return $fields;
}
/**
* Implements hook_action_info_alter().
*/
function scheduler_action_info_alter(&$definitions) {
// Workbench Moderation has a bug where the wrong actions are assigned which
// causes scheduled publishing of non-moderated content to fail. This fix will
// work regardless of the relative weights of the two modules, and will
// continue to work even if WBM is fixed before this code is removed.
// See https://www.drupal.org/project/workbench_moderation/issues/3238576
if (\Drupal::moduleHandler()->moduleExists('workbench_moderation')) {
if (isset($definitions['entity:publish_action:node']['class']) && $definitions['entity:publish_action:node']['class'] == ModerationOptOutUnpublishNode::class) {
$definitions['entity:publish_action:node']['class'] = ModerationOptOutPublishNode::class;
}
if (isset($definitions['entity:unpublish_action:node']['class']) && $definitions['entity:unpublish_action:node']['class'] == UnpublishAction::class) {
$definitions['entity:unpublish_action:node']['class'] = ModerationOptOutUnpublishNode::class;
}
}
}
/**
* Implements hook_views_data_alter().
*/
function scheduler_views_data_alter(array &$data) {
// By default the 'is null' and 'is not null' operators are only added to the
// list of filter options if the view contains a relationship. We want them to
// be always available for the scheduler date fields.
$entity_types = \Drupal::service('scheduler.manager')->getPluginEntityTypes();
foreach ($entity_types as $entityTypeId) {
// Not every entity that has a plugin will have these tables, so only set
// the allow_empty filter if the top-level key exists.
if (isset($data["{$entityTypeId}_field_data"])) {
$data["{$entityTypeId}_field_data"]['publish_on']['filter']['allow empty'] = TRUE;
$data["{$entityTypeId}_field_data"]['unpublish_on']['filter']['allow empty'] = TRUE;
}
if (isset($data["{$entityTypeId}_field_revision"])) {
$data["{$entityTypeId}_field_revision"]['publish_on']['filter']['allow empty'] = TRUE;
$data["{$entityTypeId}_field_revision"]['unpublish_on']['filter']['allow empty'] = TRUE;
}
}
// Add a relationship from Media Field Revision back to Media Field Data.
// @todo This can be removed when the relationship is added to core.
// @see https://www.drupal.org/project/drupal/issues/3036192
// Replace the existing 'argument' item.
$data['media_field_revision']['mid']['argument'] = [
'id' => 'media_mid',
'numeric' => TRUE,
];
// Add a 'relationship' item.
$data['media_field_revision']['mid']['relationship'] = [
'id' => 'standard',
'base' => 'media_field_data',
'field' => 'mid',
'base field' => 'mid',
'title' => t('Media Field Data'),
'help' => t('Relationship to access the Media fields that are not on Media Revision.'),
'label' => t('Media Field'),
'extra' => [
[
'field' => 'langcode',
'left_field' => 'langcode',
],
],
];
}
/**
* Implements hook_entity_view().
*/
function scheduler_entity_view(array &$build, EntityInterface $entity, EntityViewDisplayInterface $display, string $view_mode) {
// If the entity is going to be unpublished, then add this information to the
// http header for search engines. Only do this when the current page is the
// full-page view of the entity.
// @see https://googleblog.blogspot.be/2007/07/robots-exclusion-protocol-now-with-even.html
if ($view_mode == 'full' && isset($entity->unpublish_on->value)) {
$unavailable_after = date(DATE_RFC850, $entity->unpublish_on->value);
$build['#attached']['http_header'][] = ['X-Robots-Tag', 'unavailable_after: ' . $unavailable_after];
// Also add the information as a meta tag in the html head section.
$unavailable_meta_tag = [
'#tag' => 'meta',
'#attributes' => [
'name' => 'robots',
'content' => 'unavailable_after: ' . $unavailable_after,
],
];
// Any value seems to be OK for the second item, but it must not be omitted.
$build['#attached']['html_head'][] = [$unavailable_meta_tag, 'robots_unavailable_date'];
}
}
/**
* Implements hook_entity_presave().
*/
function scheduler_entity_presave(EntityInterface $entity) {
$config = \Drupal::config('scheduler.settings');
$scheduler_manager = \Drupal::service('scheduler.manager');
$request_time = \Drupal::time()->getRequestTime();
$publishing_enabled_types = $scheduler_manager->getEnabledTypes($entity->getEntityTypeId(), 'publish');
$unpublishing_enabled_types = $scheduler_manager->getEnabledTypes($entity->getEntityTypeId(), 'unpublish');
$publishing_enabled = in_array($entity->bundle(), $publishing_enabled_types);
$unpublishing_enabled = in_array($entity->bundle(), $unpublishing_enabled_types);
if (!$publishing_enabled && !$unpublishing_enabled) {
// Neither scheduled publishing nor unpublishing are enabled for this
// specific bundle/type, so end here.
return;
}
// If this entity is being created via Devel Generate then set values for the
// publish_on and unpublish_on dates as specified in the devel_generate form.
if (isset($entity->devel_generate)) {
static $publishing_percent;
static $unpublishing_percent;
static $entity_created;
static $time_range;
if (!isset($publishing_percent)) {
// The values may not be set if calling via drush, so default to zero.
$publishing_percent = @$entity->devel_generate['scheduler_publishing'] ?: 0;
$unpublishing_percent = @$entity->devel_generate['scheduler_unpublishing'] ?: 0;
$entity_created = isset($entity->created) ? $entity->created->value : $request_time;
// Reuse the selected 'creation' time range for our future date span.
$time_range = $entity->devel_generate['time_range'];
}
if ($publishing_percent && $publishing_enabled) {
if (rand(1, 100) <= $publishing_percent) {
// Randomly assign a publish_on value in the range starting with the
// created date and up to the selected time range in the future.
$entity->set('publish_on', rand($entity_created, $request_time + $time_range));
}
}
if ($unpublishing_percent && $unpublishing_enabled) {
if (rand(1, 100) <= $unpublishing_percent) {
// Randomly assign an unpublish_on value in the range from the later of
// created date/publish_on date up to the time range in the future.
$entity->set('unpublish_on', rand(max($entity_created, $entity->publish_on->value), $request_time + $time_range));
}
}
}
$publish_message = FALSE;
$unpublish_message = FALSE;
// If the entity type is enabled for scheduled publishing and has a publish_on
// date then check if publishing is allowed and if the content needs to be
// published immediately.
if ($publishing_enabled && !empty($entity->publish_on->value)) {
// Check that other modules allow the action on this entity.
$publication_allowed = $scheduler_manager->isAllowed($entity, 'publish');
// Publish the entity immediately if the publication date is in the past.
$publish_immediately = $scheduler_manager->getThirdPartySetting($entity, 'publish_past_date', $config->get('default_publish_past_date')) == 'publish';
if ($publication_allowed && $publish_immediately && $entity->publish_on->value <= $request_time) {
// Trigger the PRE_PUBLISH_IMMEDIATELY event so that modules can react
// before the entity has been published.
$scheduler_manager->dispatchSchedulerEvent($entity, 'PRE_PUBLISH_IMMEDIATELY');
// Set the 'changed' timestamp to match what would have been done had this
// content been published via cron.
if ($entity instanceof EntityChangedInterface) {
$entity->setChangedTime($entity->publish_on->value);
}
// If required, set the created date to match published date.
if ($scheduler_manager->getThirdPartySetting($entity, 'publish_touch', $config->get('default_publish_touch')) ||
($scheduler_manager->getThirdPartySetting($entity, 'publish_past_date_created', $config->get('default_publish_past_date_created')) && $entity->getCreatedTime() > $entity->publish_on->value)) {
$entity->setCreatedTime($entity->publish_on->value);
}
$entity->publish_on->value = NULL;
$entity->setPublished();
// Trigger the PUBLISH_IMMEDIATELY event so that modules can react after
// the entity has been published.
$scheduler_manager->dispatchSchedulerEvent($entity, 'PUBLISH_IMMEDIATELY');
}
else {
// Ensure the entity is unpublished as it will be published by cron later.
$entity->setUnpublished();
// Only inform the user that the entity is scheduled if publication has
// not been prevented by other modules. Those modules have to display a
// message themselves explaining why publication is denied.
$publish_message = ($publication_allowed && $scheduler_manager->getThirdPartySetting($entity, 'show_message_after_update', $config->get('default_show_message_after_update')));
}
} // Entity has a publish_on date.
if ($unpublishing_enabled && !empty($entity->unpublish_on->value)) {
// Scheduler does not do the same 'immediate' processing for unpublishing.
// However, the api hook should still be called during presave as there may
// be messages to be displayed if the unpublishing will be disallowed later.
$unpublication_allowed = $scheduler_manager->isAllowed($entity, 'unpublish');
$unpublish_message = ($unpublication_allowed && $scheduler_manager->getThirdPartySetting($entity, 'show_message_after_update', $config->get('default_show_message_after_update')));
}
// Give one message, which will include the publish_on date, the unpublish_on
// date or both dates. Cannot make the title into a link here when the entity
// is being created. But core provides the link in the subsequent message.
$date_formatter = \Drupal::service('date.formatter');
if ($publish_message && $unpublish_message) {
\Drupal::messenger()->addMessage(t('%title is scheduled to be published @publish_time and unpublished @unpublish_time.', [
'%title' => $entity->label(),
'@publish_time' => $date_formatter->format($entity->publish_on->value, 'long'),
'@unpublish_time' => $date_formatter->format($entity->unpublish_on->value, 'long'),
]), 'status', FALSE);
}
elseif ($publish_message) {
\Drupal::messenger()->addMessage(t('%title is scheduled to be published @publish_time.', [
'%title' => $entity->label(),
'@publish_time' => $date_formatter->format($entity->publish_on->value, 'long'),
]), 'status', FALSE);
}
elseif ($unpublish_message) {