diff --git a/config/schema/node_revision_delete.plugin.schema.yml b/config/schema/node_revision_delete.plugin.schema.yml index 22c0719..507a219 100644 --- a/config/schema/node_revision_delete.plugin.schema.yml +++ b/config/schema/node_revision_delete.plugin.schema.yml @@ -21,6 +21,7 @@ plugin.plugin_configuration.node_revision_delete.amount: type: integer label: 'Minimum number of revisions to keep (per language)' + plugin.plugin_configuration.node_revision_delete.created: type: node_revision_delete.default_plugin_configuration label: 'Created plugin configuration' @@ -44,3 +45,15 @@ plugin.plugin_configuration.node_revision_delete.drafts: age: type: integer label: 'The minimum amount of months a new revision must be kept for' + +plugin.plugin_configuration.node_revision_delete.only_drafts: + type: node_revision_delete.default_plugin_configuration + label: 'Only Drafts plugin configuration' + mapping: + settings: + type: mapping + label: 'Only delete revisions that use draft state.' + mapping: + age: + type: integer + label: 'The minimum amount of months a draft revision must be kept for' diff --git a/src/Plugin/NodeRevisionDelete/OnlyDrafts.php b/src/Plugin/NodeRevisionDelete/OnlyDrafts.php new file mode 100644 index 0000000..82a94e1 --- /dev/null +++ b/src/Plugin/NodeRevisionDelete/OnlyDrafts.php @@ -0,0 +1,73 @@ +entityTypeManager->getStorage('node')->loadRevision($revision_id); + $can_delete = NULL; + + $age = strtotime('-' . $this->configuration['age'] . 'months'); + + // The timestamp of the created revision is stored in the changed field. + $creation_time = $revision->getChangedTime(); + + // Explicitely keep revisions for the configured minimum age. We only have + // an opinion on revisions created before the active revision. + if ($revision_id < $active_vid && $creation_time >= $age) { + $can_delete = FALSE; + } + elseif ($revision_id < $active_vid && $creation_time < $age) { + $revision_state = $revision->get('moderation_state')->getString(); + $expected_state = 'draft'; + if (str_contains($revision_state, $expected_state)) + $can_delete = TRUE; + } + } + + $revision_statuses[$revision_id] = $can_delete; + } + + return $revision_statuses; + } + + /** + * {@inheritdoc} + */ + public function buildConfigurationForm(array $form, FormStateInterface $form_state): array { + $options = [1 => '1 ' . $this->t('month')]; + for ($i = 2; $i <= 24; $i++) { + $options[$i] = $i . ' ' . $this->t('months'); + } + $form['age'] = [ + '#type' => 'select', + '#title' => $this->t('The minimum amount of months a new revision must be kept for'), + '#description' => $this->t('After this time, revisions newer than the active revision (like draft revisions) can be deleted. The minimum age of revisions is always respected, regardless of other settings. Only revisions created after the active revision will be deleted.'), + '#options' => $options, + '#required' => TRUE, + '#default_value' => $this->configuration['age'] ?? 0, + ]; + return $form; + } + +}