Commit ed778e58 authored by Stephan Zeidler's avatar Stephan Zeidler Committed by Jonathan Smith
Browse files

Issue #3225695 by szeidler, jonathan1055: Support entity types whose entity...

Issue #3225695 by szeidler, jonathan1055: Support entity types whose entity bundle type does not end with `_type` and entities without created date
parent 0fb9ecd2
Loading
Loading
Loading
Loading
+50 −9
Changes for scheduler.module: 50 added lines, 9 removed lines.
Original line number Diff line number Diff line
@@ -290,8 +290,11 @@ 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 */
  $contentEntityType = \Drupal::service('entity_type.manager')->getDefinition($type->getEntityType()->getBundleOf());
  /** @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(),
@@ -332,6 +335,15 @@ function _scheduler_entity_type_form_alter(&$form, FormStateInterface $form_stat
      ],
    ],
  ];

  // Entity types that do not implement the 'getCreatedTime' method should have
  // the option set to FALSE and the field disabled.
  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'),
@@ -390,6 +402,14 @@ function _scheduler_entity_type_form_alter(&$form, FormStateInterface $form_stat
    ],
  ];

  // Entity types that do not implement the 'getCreatedTime' method should have
  // the option set to FALSE and the field disabled.
  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',
@@ -494,13 +514,16 @@ function _scheduler_form_entity_type_form_builder($entity_type, $type, &$form, F
 * 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');

  $type_table = $entityTypeId . '_types';
    $form[$type_table]['#header']['scheduler'] = t('Scheduler settings');

    foreach (array_keys($form[$type_table]['#options']) as $type) {
@@ -524,6 +547,22 @@ function _scheduler_devel_generate_form_alter(array &$form, FormStateInterface $
      }
      $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()
@@ -531,7 +570,7 @@ function _scheduler_devel_generate_form_alter(array &$form, FormStateInterface $
  $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 for creation date.'),
    '#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' => 50,
    '#required' => TRUE,
    '#min' => 0,
@@ -540,7 +579,7 @@ function _scheduler_devel_generate_form_alter(array &$form, FormStateInterface $
  $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 for creation date.'),
    '#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' => 50,
    '#required' => TRUE,
    '#min' => 0,
@@ -733,6 +772,7 @@ function scheduler_entity_presave(EntityInterface $entity) {
  if (isset($entity->devel_generate)) {
    static $publishing_percent;
    static $unpublishing_percent;
    static $entity_created;
    static $time_range;

    if (!isset($publishing_percent)) {
@@ -740,20 +780,21 @@ function scheduler_entity_presave(EntityInterface $entity) {
      $publishing_percent = @$entity->devel_generate['scheduler_publishing'] ?: 0;
      $unpublishing_percent = @$entity->devel_generate['scheduler_unpublishing'] ?: 0;
      // Reuse the selected 'creation' time range for our future date span.
      $entity_created = isset($entity->created) ? $entity->created->value : $request_time;
      $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->value + 1, $request_time + $time_range));
        $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->value, $entity->publish_on->value), $request_time + $time_range));
        $entity->set('unpublish_on', rand(max($entity_created, $entity->publish_on->value), $request_time + $time_range));
      }
    }
  }
@@ -781,7 +822,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')))) {
        ($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;
+5 −3
Changes for src/Form/SchedulerAdminForm.php: 5 added lines, 3 removed lines.
Original line number Diff line number Diff line
@@ -88,15 +88,17 @@ class SchedulerAdminForm extends ConfigFormBase {
      $publishing_enabled_types = $this->schedulerManager->getEnabledTypes($entityTypeId, 'publish');
      $unpublishing_enabled_types = $this->schedulerManager->getEnabledTypes($entityTypeId, 'unpublish');

      // When all is running normally, $plugin->getTypes() will give a non-empty
      // array of values, but we need to protect against this being empty.
      // $plugin->getTypes() will usually give a non-empty array of values, but
      // it can be empty if no default bundle type is defined, or all types have
      // been deleted.
      if (!$types = $plugin->getTypes()) {
        // When a module is enabled via drush there is no automatic clear cache.
        // Thus moduleHandler()->moduleExists({module}) can return false when
        // the module is actually enabled. This means we get nothing for
        // plugin->getTypes() and processing should stop with a useful exception
        // message, instead of letting Core give a confusing exception.
        $type_definition = $this->entityTypeManager->getDefinition($entityTypeId . '_type', FALSE);
        $bundle_type = $this->entityTypeManager->getDefinition($entityTypeId)->getBundleEntityType();
        $type_definition = $this->entityTypeManager->getDefinition($bundle_type, FALSE);
        if (!$type_definition) {
          throw new \Exception(sprintf('Invalid or empty entity type definition for %s module. Do a full cache clear via admin/config/development/performance or drush cr.', $plugin->dependency()));
        }
+6 −4
Changes for src/SchedulerManager.php: 6 added lines, 4 removed lines.
Original line number Diff line number Diff line
@@ -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')))
            ($this->getThirdPartySetting($entity, 'publish_past_date_created', $this->setting('default_publish_past_date_created')) && $entity->getCreatedTime() > $publish_on)
          ) {
            $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'),
@@ -351,7 +351,8 @@ class SchedulerManager {
          }

          // Create a set of variables for use in the log message.
          $entity_type = $this->entityTypeManager->getStorage($entityTypeId . '_type')->load($entity->bundle());
          $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()),
          ]));
@@ -543,7 +544,8 @@ class SchedulerManager {
          }

          // Create a set of variables for use in the log message.
          $entity_type = $this->entityTypeManager->getStorage($entityTypeId . '_type')->load($entity->bundle());
          $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()),
          ]));