You are here

function comment_delete_form_alter in Comment Delete 8

Implements hook_form_alter().

Alter the comment_*.delete_form for each entity.

File

./comment_delete.module, line 61
comment_delete.module

Code

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

  /** @var \Drupal\Core\Routing\AdminContext $admin_context */
  $admin_context = \Drupal::service('router.admin_context');
  if ($admin_context
    ->isAdminRoute()) {

    // Do nothing on administrative routes.
    return;
  }
  if (preg_match('/comment_(.*)_delete_form/i', $form_id)) {

    /** @var \Drupal\comment_delete\CommentDeleteManager $comment_delete_manager */
    $comment_delete_manager = \Drupal::service('comment_delete.manager');
    $config = \Drupal::config('comment_delete.config');
    $default_selection = $config
      ->get('default_selection');
    $form['description']['#markup'] = t('This action cannot be undone.');

    // Defines operation types.
    $options = $comment_delete_manager
      ->getOperations();
    $form['delete_operation'] = [
      '#type' => 'radios',
      '#title' => t('How should replies to this comment be handled?'),
      '#options' => $options,
      '#required' => TRUE,
      '#default_value' => isset($options[$default_selection]) ? $default_selection : 2,
    ];
    if (count($options) === 1) {
      $form['delete_operation']['#disabled'] = TRUE;
    }
    $form['actions']['submit']['#submit'] = [
      [
        CommentDeleteSubmitter::class,
        'submitForm',
      ],
    ];
  }
}