You are here

function node_revision_delete_form_node_type_form_alter in Node Revision Delete 7.3

Same name and namespace in other branches
  1. 8 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 77

Code

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

  // Getting the content types to track variable.
  $node_revision_delete_track = variable_get('node_revision_delete_track');

  // Getting the content type machine name from the actual form.
  $content_type = $form['#node_type']->type;

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

  // Element to track the content type.
  $form['workflow']['section']['node_revision_delete_track'] = array(
    '#type' => 'checkbox',
    '#title' => t('Limit the amount of revisions'),
    '#default_value' => $track,
  );

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

  // Getting the max number for node_revision_delete_minimum_age_to_delete_time.
  $node_revision_delete_minimum_age_to_delete_time_max_number = variable_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] = _node_revision_delete_time_string('minimum_age_to_delete', $i);
  }

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

  // Getting the max number for node_revision_delete_when_to_delete_time.
  $node_revision_delete_when_to_delete_time_max_number = variable_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] = _node_revision_delete_time_string('when_to_delete', $i);
  }

  // Element to know when to delete the revisions.
  $form['workflow']['section']['when_to_delete'] = array(
    '#type' => 'select',
    '#title' => t('When to delete'),
    '#description' => t('If the current revision is not older than specified here, 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' => array(
      // Show the field when the checkbox is checked.
      'visible' => array(
        ':input[name="node_revision_delete_track"]' => array(
          'checked' => TRUE,
        ),
      ),
    ),
  );

  // If we are creating the content type. Putting the custom submit handler
  // first to use the Entity->isNew() function, if the custom submit handler is
  // the last function called always the Entity is created first.
  array_unshift($form['#submit'], '_node_revision_delete_form_node_type_submit');
}