You are here

public function NodeRevisionsByBundleForm::buildForm in Node Revisions Autoclean 8

Form constructor.

Parameters

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

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

Return value

array The form structure.

Overrides FormInterface::buildForm

File

src/Form/NodeRevisionsByBundleForm.php, line 67

Class

NodeRevisionsByBundleForm
Class NodeRevisionsByBundleForm.

Namespace

Drupal\node_revisions_autoclean\Form

Code

public function buildForm(array $form, FormStateInterface $form_state) {
  $types = $this->entityTypeBundleInfo
    ->getBundleInfo('node');
  $config = $this->configFactory
    ->get('node_revisions_autoclean.settings');
  $form['enable_on_cron'] = [
    '#type' => 'checkbox',
    '#title' => $this
      ->t('Enable old revisions deletion during cronjobs'),
    '#return_value' => '1',
    '#default_value' => $config
      ->get('enable_on_cron') ? $config
      ->get('enable_on_cron') : '0',
    '#description' => $this
      ->t('Cronjobs will delete old revisions according your parameters.'),
  ];
  $form['enable_on_node_update'] = [
    '#type' => 'checkbox',
    '#title' => $this
      ->t('Enable old revisions deletion on node update'),
    '#description' => $this
      ->t("Each node's revisions will be autoclean on node update"),
    '#return_value' => '1',
    '#default_value' => $config
      ->get('enable_on_node_update') ? $config
      ->get('enable_on_node_update') : '0',
  ];
  $form['explain'] = [
    '#markup' => '<p><i>' . $this
      ->t('You can select none of the above if you wish to delete old revisions using a drush command (drush nra:dor).') . '</i></p>',
  ];
  foreach ($types as $machine_name => $arr) {
    $form['fs_' . $machine_name] = [
      '#type' => 'fieldset',
      '#title' => $this
        ->t('Content type : @content_type', [
        '@content_type' => $arr['label'],
      ]),
      '#collapsible' => TRUE,
      '#collapsed' => FALSE,
    ];
    $form['fs_' . $machine_name]["node__{$machine_name}"] = [
      '#type' => 'number',
      '#title' => $this
        ->t('Limit revisions for node type @label', [
        '@label' => $arr['label'],
      ]),
      '#description' => $this
        ->t('Max revisions for @label type, "-1" means unlimited number of revisions, "0" keeps only the last.', [
        '@label' => $arr['label'],
      ]),
      '#default_value' => $config
        ->get("node.{$machine_name}") ? $config
        ->get("node.{$machine_name}") : -1,
      '#required' => TRUE,
    ];
    $val = $config
      ->get("interval.{$machine_name}");
    $form['fs_' . $machine_name]['node_enable_date_' . $machine_name] = [
      '#type' => 'checkbox',
      '#title' => $this
        ->t("Keep latest revisions based on date"),
      '#return_value' => 1,
      '#default_value' => isset($val) && $val ? 1 : 0,
    ];
    $form['fs_' . $machine_name]['interval__' . $machine_name] = [
      '#type' => 'select',
      '#title' => $this
        ->t("Keep latests revisions"),
      '#states' => [
        'visible' => [
          ':input[name="node_enable_date_' . $machine_name . '"]' => [
            'checked' => TRUE,
          ],
        ],
      ],
      '#options' => [
        '0' => $this
          ->t('Choose value'),
        'P1W' => $this
          ->t('1 week'),
        'P2W' => $this
          ->t('2 weeks'),
        'P3W' => $this
          ->t('3 weeks'),
        'P1M' => $this
          ->t('1 month'),
        'P2M' => $this
          ->t('2 months'),
        'P3M' => $this
          ->t('3 months'),
        'P4M' => $this
          ->t('4 months'),
        'P5M' => $this
          ->t('5 months'),
        'P6M' => $this
          ->t('6 months'),
        'P1Y' => $this
          ->t('1 year'),
      ],
      '#default_value' => isset($val) && $val ? $val : 0,
    ];
  }
  $form['submit'] = [
    '#type' => 'submit',
    '#value' => $this
      ->t('Submit'),
  ];
  return $form;
}