You are here

node_revisions_autoclean.module in Node Revisions Autoclean 8

File

node_revisions_autoclean.module
View source
<?php

/**
 * @file
 * Contains node_revisions_autoclean.module.
 */
use Drupal\Core\Routing\RouteMatchInterface;
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Form\FormStateInterface;

/**
 * Implements hook_help().
 */
function node_revisions_autoclean_help($route_name, RouteMatchInterface $route_match) {
  switch ($route_name) {

    // Main module help for the node_revisions_autoclean module.
    case 'help.page.node_revisions_autoclean':
      $output = '';
      $output .= '<h3>' . t('About') . '</h3>';
      $output .= '<p>' . t('Node revisions autoclean') . '</p>';
      $output .= '<p>' . t('Drush command available only for Drupal >= 8.4 (drush 9)') . '</p>';
      return $output;
    default:
  }
}

/**
 * Implements hook_ENTITY_TYPE_update().
 */
function node_revisions_autoclean_node_update(EntityInterface $entity) {
  $config = \Drupal::config('node_revisions_autoclean.settings');
  if ($config
    ->get('enable_on_node_update')) {

    /* @var $revisionsManager \Drupal\node_revisions_autoclean\Services\RevisionsManager */
    $revisionsManager = \Drupal::service('node_revisions_autoclean.revisions_manager');
    $revisions = $revisionsManager
      ->revisionsToDelete($entity);
    $revisionsManager
      ->deleteRevisions($revisions);
  }
  if ($config
    ->get('enable_on_cron')) {
    $revisionsManager = \Drupal::service('node_revisions_autoclean.revisions_manager');
    $revisionsManager
      ->queueNodeForCronJob($entity);
  }
}

/**
 * Implements hook_form_FORM_ID_alter().
 */
function node_revisions_autoclean_form_node_type_edit_form_alter(&$form, FormStateInterface $form_state, $form_id) {
  node_revisions_autoclean_alter_type_form($form);
}

/**
 * Implements hook_form_FORM_ID_alter().
 */
function node_revisions_autoclean_form_node_type_add_form_alter(&$form, FormStateInterface $form_state, $form_id) {
  node_revisions_autoclean_alter_type_form($form);
}

/**
 * Alters form node type.
 *
 * @param array $form
 *   Form.
 */
function node_revisions_autoclean_alter_type_form(array &$form) {
  $config = \Drupal::config('node_revisions_autoclean.settings');
  $machine_name = isset($form['type']['#default_value']) ? $form['type']['#default_value'] : '';
  $form['node_revisions_autoclean'] = [
    '#type' => 'details',
    '#title' => t('Node revisions autoclean'),
    '#group' => 'additional_settings',
    'node__nra' => [
      '#type' => 'number',
      '#title' => t('Revisions limit'),
      '#description' => t('Max revisions, "-1" means unlimited number of revisions, "0" keeps only the last.'),
      '#default_value' => $config
        ->get("node.{$machine_name}") ? $config
        ->get("node.{$machine_name}") : -1,
      '#required' => TRUE,
    ],
    'node__enable_date_nra' => [
      '#type' => 'checkbox',
      '#title' => t("Keep latest revisions based on date"),
      '#return_value' => 1,
      '#default_value' => $config
        ->get("interval.{$machine_name}") ? 1 : 0,
    ],
    'interval__nra' => [
      '#type' => 'select',
      '#title' => t("Keep latests revisions"),
      '#states' => [
        'visible' => [
          ':input[name="node__enable_date_nra"]' => [
            'checked' => TRUE,
          ],
        ],
      ],
      '#options' => [
        '0' => t('Choose value'),
        'P1W' => t('1 week'),
        'P2W' => t('2 weeks'),
        'P3W' => t('3 weeks'),
        'P1M' => t('1 month'),
        'P2M' => t('2 months'),
        'P3M' => t('3 months'),
        'P4M' => t('4 months'),
        'P5M' => t('5 months'),
        'P6M' => t('6 months'),
        'P1Y' => t('1 year'),
      ],
      '#default_value' => $config
        ->get("interval.{$machine_name}") ? $config
        ->get("interval.{$machine_name}") : 0,
    ],
  ];
  $form['actions']['submit']['#submit'][] = 'node_revisions_autoclean_form_node_type_edit_form_submit';
  if (isset($form['actions']['save_continue'])) {
    $form['actions']['save_continue']['#submit'][] = 'node_revisions_autoclean_form_node_type_edit_form_submit';
  }
}

/**
 * Submits node type edit form.
 *
 * @param array $form
 *   Form.
 * @param \Drupal\Core\Form\FormStateInterface $form_state
 *   Form State.
 */
function node_revisions_autoclean_form_node_type_edit_form_submit(array &$form, FormStateInterface $form_state) {
  $config = \Drupal::configFactory()
    ->getEditable('node_revisions_autoclean.settings');
  $machine_name = $form_state
    ->getValue('type');
  $maxRevisions = $form_state
    ->getValue('node__nra');
  $enableDate = $form_state
    ->getValue('node__enable_date_nra');
  $interval = $enableDate ? $form_state
    ->getValue('interval__nra') : 0;
  $config
    ->set("node.{$machine_name}", $maxRevisions);
  $config
    ->set("interval.{$machine_name}", $interval);
  $config
    ->save(TRUE);
}