Skip to content

Instantly share code, notes, and snippets.

@szeidler
Created July 28, 2021 16:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save szeidler/e7f3b4332f1084a4e30f869a2d6f4d71 to your computer and use it in GitHub Desktop.
Save szeidler/e7f3b4332f1084a4e30f869a2d6f4d71 to your computer and use it in GitHub Desktop.
diff --git a/scheduler.module b/scheduler.module
index 4e959da..98c1ee1 100644
--- a/scheduler.module
+++ b/scheduler.module
@@ -287,9 +287,12 @@ function _scheduler_entity_type_form_alter(&$form, FormStateInterface $form_stat
/** @var \Drupal\Core\Entity\EntityTypeInterface $type */
$type = $form_state->getFormObject()->getEntity();
- /** @var Drupal\Core\Entity\ContentEntityTypeInterface $ContentEntityType */
+ /** @var Drupal\Core\Entity\ContentEntityTypeInterface $contentEntityType */
$contentEntityType = \Drupal::service('entity_type.manager')->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()),
@@ -330,6 +333,15 @@ function _scheduler_entity_type_form_alter(&$form, FormStateInterface $form_stat
],
],
];
+
+ // Entity types that do not implement the `getCreatedTime` method should
+ // be disabled by default.
+ 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_required'] = [
'#type' => 'checkbox',
'#title' => t('Require scheduled publishing'),
@@ -388,6 +400,14 @@ function _scheduler_entity_type_form_alter(&$form, FormStateInterface $form_stat
],
];
+ // Entity types that do not implement the `getCreatedTime` method should
+ // be disabled by default.
+ 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',
@@ -779,7 +799,7 @@ function scheduler_entity_presave(EntityInterface $entity) {
$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')) ||
- ($entity->getCreatedTime() > $entity->publish_on->value && $scheduler_manager->getThirdPartySetting($entity, 'publish_past_date_created', $config->get('default_publish_past_date_created')))) {
+ (method_exists($entity, 'getCreatedTime') && $entity->getCreatedTime() > $entity->publish_on->value && $scheduler_manager->getThirdPartySetting($entity, 'publish_past_date_created', $config->get('default_publish_past_date_created')))) {
$entity->setCreatedTime($entity->publish_on->value);
}
$entity->publish_on->value = NULL;
diff --git a/src/SchedulerManager.php b/src/SchedulerManager.php
index 5498505..0642be1 100644
--- a/src/SchedulerManager.php
+++ b/src/SchedulerManager.php
@@ -311,13 +311,13 @@ class SchedulerManager {
// Update 'changed' timestamp.
$entity->setChangedTime($publish_on);
- $old_creation_date = $entity->getCreatedTime();
$msg_extra = '';
// If required, set the created date to match published date.
if ($this->getThirdPartySetting($entity, 'publish_touch', $this->setting('default_publish_touch')) ||
- ($entity->getCreatedTime() > $publish_on && $this->getThirdPartySetting($entity, 'publish_past_date_created', $this->setting('default_publish_past_date_created')))
+ (method_exists($entity, 'getCreatedTime') && $entity->getCreatedTime() > $publish_on && $this->getThirdPartySetting($entity, 'publish_past_date_created', $this->setting('default_publish_past_date_created')))
) {
+ $old_creation_date = $entity->getCreatedTime();
$entity->setCreatedTime($publish_on);
$msg_extra = $this->t('The previous creation date was @old_creation_date, now updated to match the publishing date.', [
'@old_creation_date' => $this->dateFormatter->format($old_creation_date, 'short'),
@@ -350,8 +350,9 @@ class SchedulerManager {
$failed = $failed || ($return === -1);
}
- // Log the fact that a scheduled publication is about to take place.
- $entity_type = $this->entityTypeManager->getStorage($entityTypeId . '_type')->load($entity->bundle());
+ // Create a set of variables for use in the log message.
+ $bundle_type = $entity->getEntityType()->getBundleEntityType();
+ $entity_type = $this->entityTypeManager->getStorage($bundle_type)->load($entity->bundle());
$view_link = $entity->toLink($this->t('View @type', [
'@type' => strtolower($entity_type->label()),
]));
@@ -538,8 +539,9 @@ class SchedulerManager {
$failed = $failed || ($return === -1);
}
- // Log the fact that a scheduled unpublication is about to take place.
- $entity_type = $this->entityTypeManager->getStorage($entityTypeId . '_type')->load($entity->bundle());
+ // Create a set of variables for use in the log message.
+ $bundle_type = $entity->getEntityType()->getBundleEntityType();
+ $entity_type = $this->entityTypeManager->getStorage($bundle_type)->load($entity->bundle());
$view_link = $entity->toLink($this->t('View @type', [
'@type' => strtolower($entity_type->label()),
]));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment