You are here

public function AdminSettingsForm::submitForm in Node Revision Delete 8

Form submission handler.

Parameters

array $form: An associative array containing the structure of the form.

\Drupal\Core\Form\FormStateInterface $form_state: The current state of the form.

Overrides ConfigFormBase::submitForm

File

src/Form/AdminSettingsForm.php, line 370

Class

AdminSettingsForm
Class NodeRevisionDeleteAdminSettingsForm.

Namespace

Drupal\node_revision_delete\Form

Code

public function submitForm(array &$form, FormStateInterface $form_state) {
  parent::submitForm($form, $form_state);

  // Getting the values for node_revision_delete_when_to_delete_time.
  $when_to_delete_time_max_number = $form_state
    ->getValue('node_revision_delete_when_to_delete_time_max_number');
  $node_revision_delete_when_to_delete_time = [
    'max_number' => $when_to_delete_time_max_number,
    'time' => $form_state
      ->getValue('node_revision_delete_when_to_delete_time_time'),
  ];

  // Getting the values for node_revision_delete_minimum_age_to_delete_time.
  $minimum_age_to_delete_time_max_number = $form_state
    ->getValue('node_revision_delete_minimum_age_to_delete_time_max_number');
  $node_revision_delete_minimum_age_to_delete_time = [
    'max_number' => $minimum_age_to_delete_time_max_number,
    'time' => $form_state
      ->getValue('node_revision_delete_minimum_age_to_delete_time_time'),
  ];

  // We need to update the max_number in the existing content type
  // configuration if the new value is lower than the actual.
  $this->nodeRevisionDelete
    ->updateTimeMaxNumberConfig('minimum_age_to_delete', $minimum_age_to_delete_time_max_number);
  $this->nodeRevisionDelete
    ->updateTimeMaxNumberConfig('when_to_delete', $when_to_delete_time_max_number);

  // Saving the configuration.
  $this
    ->config($this
    ->getEditableConfigNames()[0])
    ->set('delete_newer', $form_state
    ->getValue('delete_newer'))
    ->set('node_revision_delete_cron', $form_state
    ->getValue('node_revision_delete_cron'))
    ->set('node_revision_delete_time', $form_state
    ->getValue('node_revision_delete_time'))
    ->set('node_revision_delete_when_to_delete_time', $node_revision_delete_when_to_delete_time)
    ->set('node_revision_delete_minimum_age_to_delete_time', $node_revision_delete_minimum_age_to_delete_time)
    ->save();

  // Checking if we need to delete revisions.
  if ($form_state
    ->getValue('run_now')) {

    // Getting the dry run value.
    $dry_run = $form_state
      ->getValue('dry_run');

    // Looking for all the configured content types.
    $content_types = $this->nodeRevisionDelete
      ->getConfiguredContentTypes();
    $candidate_revisions = [];

    // Loop over all the content types to search the revisions to delete.
    foreach ($content_types as $content_type) {

      // Getting the candidate revisions to delete.
      $candidate_revisions = array_merge($candidate_revisions, $this->nodeRevisionDelete
        ->getCandidatesRevisions($content_type
        ->id()));
    }

    // Add the batch.
    batch_set($this->nodeRevisionDelete
      ->getRevisionDeletionBatch($candidate_revisions, $dry_run));
  }
}