You are here

function node_revision_delete_form_node_type_form_alter in Node Revision Delete 8

Same name and namespace in other branches
  1. 7.3 node_revision_delete.module \node_revision_delete_form_node_type_form_alter()
  2. 7.2 node_revision_delete.module \node_revision_delete_form_node_type_form_alter()

Implements hook_form_BASE_FORM_ID_alter().

File

./node_revision_delete.module, line 50
Contains node_revision_delete.module.

Code

function node_revision_delete_form_node_type_form_alter(&$form, FormStateInterface &$form_state, $form_id) {

  // Getting the config.
  $config = \Drupal::config('node_revision_delete.settings');

  // Getting the content type entity.
  $content_type = $form_state
    ->getFormObject()
    ->getEntity();

  // Getting the content type settings.
  $content_type_settings = $content_type
    ->getThirdPartySettings('node_revision_delete');

  // Looking if the config exists for the content type.
  if (!empty($content_type_settings)) {
    $track = TRUE;
    $minimum_revisions_to_keep = $content_type_settings['minimum_revisions_to_keep'];
    $when_to_delete = $content_type_settings['when_to_delete'];
    $minimum_age_to_delete = $content_type_settings['minimum_age_to_delete'];
  }
  else {
    $track = FALSE;
    $minimum_revisions_to_keep = 1;
    $when_to_delete = 0;
    $minimum_age_to_delete = 0;
  }
  $form['workflow']['section'] = [
    '#type' => 'fieldset',
    '#title' => t("Node revision delete"),
    '#attributes' => [
      'class' => [
        'fieldgroup',
        'form-composite',
      ],
    ],
    '#description' => t('The settings defined in this section will be applied to each node of this content type.'),
  ];

  // Element to track the content type.
  $form['workflow']['section']['node_revision_delete_track'] = [
    '#type' => 'checkbox',
    '#title' => t('Limit the number of revisions'),
    '#default_value' => $track,
    '#attached' => [
      'library' => [
        'node_revision_delete/content_types',
        'node_revision_delete/admin_settings',
      ],
    ],
  ];

  // Element for the minimum number of revisions to keep.
  $form['workflow']['section']['minimum_revisions_to_keep'] = [
    '#type' => 'number',
    '#title' => t('Minimum number of revisions to keep'),
    '#description' => t('The oldest revisions will be deleted when the total amount surpasses this value. Set it to 1 to remove all revisions (except the current revision).'),
    '#default_value' => $minimum_revisions_to_keep,
    '#min' => 1,
    '#states' => [
      // Hide the settings when the cancel notify checkbox is disabled.
      'visible' => [
        ':input[name="node_revision_delete_track"]' => [
          'checked' => TRUE,
        ],
      ],
    ],
  ];

  // Getting the max number for node_revision_delete_minimum_age_to_delete_time.
  $node_revision_delete_minimum_age_to_delete_time_max_number = $config
    ->get('node_revision_delete_minimum_age_to_delete_time')['max_number'];

  // Available options for minimum_age_to_delete.
  $options_minimum_age_to_delete[0] = t('None');
  for ($i = 1; $i <= $node_revision_delete_minimum_age_to_delete_time_max_number; $i++) {
    $options_minimum_age_to_delete[$i] = \Drupal::service('node_revision_delete')
      ->getTimeString('minimum_age_to_delete', $i);
  }

  // Element to know when to delete the revisions.
  $form['workflow']['section']['minimum_age_to_delete'] = [
    '#type' => 'select',
    '#title' => t('Minimum age of revisions to delete'),
    '#description' => t('Revisions older than this age will be deleted, but only after the "Minimum number of revisions to keep" has been reached. If you are not concerned with the age of the revisions, set to "None".'),
    '#options' => $options_minimum_age_to_delete,
    '#size' => 1,
    '#default_value' => $minimum_age_to_delete,
    '#states' => [
      // Show the field when the checkbox is checked.
      'visible' => [
        ':input[name="node_revision_delete_track"]' => [
          'checked' => TRUE,
        ],
      ],
    ],
  ];

  // Getting the max number for node_revision_delete_when_to_delete_time.
  $node_revision_delete_when_to_delete_time_max_number = $config
    ->get('node_revision_delete_when_to_delete_time')['max_number'];

  // Available options for when_to_delete variable.
  $options_when_to_delete[0] = t('Always');
  for ($i = 1; $i <= $node_revision_delete_when_to_delete_time_max_number; $i++) {

    // Creating the time string.
    $options_when_to_delete[$i] = \Drupal::service('node_revision_delete')
      ->getTimeString('when_to_delete', $i);
  }

  // Element to know when to delete the revisions.
  $form['workflow']['section']['when_to_delete'] = [
    '#type' => 'select',
    '#title' => t('When to delete'),
    '#description' => t('If the current revision is not older than this, its older revisions will not be deleted, even if they are old enough. If set to "Always", older revisions will be deleted regardless of the age of the current revision.'),
    '#options' => $options_when_to_delete,
    '#size' => 1,
    '#default_value' => $when_to_delete,
    '#states' => [
      // Show the field when the checkbox is checked.
      'visible' => [
        ':input[name="node_revision_delete_track"]' => [
          'checked' => TRUE,
        ],
      ],
    ],
  ];
  $form['#entity_builders'][] = 'node_revision_delete_form_node_type_form_builder';
}