You are here

CommentDeleteSubmitter.php in Comment Delete 8

File

src/CommentDeleteSubmitter.php
View source
<?php

namespace Drupal\comment_delete;

use Drupal\Core\DependencyInjection\Container;
use Drupal\Core\Form\FormStateInterface;

/**
 * Submit callback handler for deleting comment.
 *
 * @package Drupal\comment_delete
 */
class CommentDeleteSubmitter extends Container {

  /**
   * Submit handler for the comment delete form.
   *
   * @param array $form
   *   The form.
   * @param \Drupal\Core\Form\FormStateInterface $form_state
   *   The form state.
   */
  public static function submitForm(array &$form, FormStateInterface $form_state) {

    /** @var \Drupal\comment_delete\CommentDeleteManager $comment_delete_manager */
    $comment_delete_manager = \Drupal::service('comment_delete.manager');

    /** @var \Drupal\comment\Form\DeleteForm $form_object */
    $form_object = $form_state
      ->getFormObject();

    /** @var \Drupal\comment\Entity\Comment $comment */
    $comment = $form_object
      ->getEntity();
    $comment_delete_manager
      ->delete($comment, $form_state
      ->getValue('delete_operation'));
    if ($comment_delete_manager
      ->isAjaxRequest()) {

      // No redirect on Ajax request. Presumably the module or custom
      // integration adding Ajax support to comments handles the Ajax response.
      $form_state
        ->disableRedirect();
    }
    else {
      $entity_type = $comment
        ->getCommentedEntityTypeId();
      $form_state
        ->setRedirect("entity.{$entity_type}.canonical", [
        $entity_type => $comment
          ->getCommentedEntityId(),
      ]);
    }
  }

}

Classes

Namesort descending Description
CommentDeleteSubmitter Submit callback handler for deleting comment.