ConfirmDeleteMultiple.php 3.51 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12
<?php

/**
 * @file
 * Contains \Drupal\comment\Form\DeleteConfirmMultiple.
 */

namespace Drupal\comment\Form;

use Drupal\comment\CommentStorageControllerInterface;
use Drupal\Component\Utility\String;
use Drupal\Core\Cache\Cache;
13
use Drupal\Core\DependencyInjection\ContainerInjectionInterface;
14 15 16 17 18 19 20
use Drupal\Core\Form\ConfirmFormBase;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\HttpFoundation\Request;

/**
 * Provides the comment multiple delete confirmation form.
 */
21
class ConfirmDeleteMultiple extends ConfirmFormBase implements ContainerInjectionInterface {
22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132

  /**
   * The comment storage.
   *
   * @var \Drupal\comment\CommentStorageControllerInterface
   */
  protected $commentStorage;

  /**
   * An array of comments to be deleted.
   *
   * @var \Drupal\comment\CommentInterface[]
   */
  protected $comments;

  /**
   * Creates an new ConfirmDeleteMultiple form.
   *
   * @param \Drupal\comment\CommentStorageControllerInterface $comment_storage
   *   The comment storage.
   */
  public function __construct(CommentStorageControllerInterface $comment_storage) {
    $this->commentStorage = $comment_storage;
  }

  /**
   * {@inheritdoc}
   */
  public static function create(ContainerInterface $container) {
    return new static(
      $container->get('plugin.manager.entity')->getStorageController('comment')
    );
  }

  /**
   * {@inheritdoc}
   */
  public function getFormID() {
    return 'comment_multiple_delete_confirm';
  }

  /**
   * {@inheritdoc}
   */
  public function getQuestion() {
    return $this->t('Are you sure you want to delete these comments and all their children?');
  }

  /**
   * {@inheritdoc}
   */
  public function getCancelPath() {
    return 'admin/content/comment';
  }

  /**
   * {@inheritdoc}
   */
  public function getConfirmText() {
    return $this->t('Delete comments');
  }

  /**
   * {@inheritdoc}
   */
  public function buildForm(array $form, array &$form_state, Request $request = NULL) {
    $edit = $form_state['input'];

    $form['comments'] = array(
      '#prefix' => '<ul>',
      '#suffix' => '</ul>',
      '#tree' => TRUE,
    );
    // array_filter() returns only elements with actual values.
    $comment_counter = 0;
    $this->comments = $this->commentStorage->loadMultiple(array_keys(array_filter($edit['comments'])));
    foreach ($this->comments as $comment) {
      $cid = $comment->id();
      $form['comments'][$cid] = array(
        '#type' => 'hidden',
        '#value' => $cid,
        '#prefix' => '<li>',
        '#suffix' => String::checkPlain($comment->label()) . '</li>'
      );
      $comment_counter++;
    }
    $form['operation'] = array('#type' => 'hidden', '#value' => 'delete');

    if (!$comment_counter) {
      drupal_set_message($this->t('There do not appear to be any comments to delete, or your selected comment was deleted by another administrator.'));
      $form_state['redirect'] = 'admin/content/comment';
    }

    return parent::buildForm($form, $form_state, $request);
  }

  /**
   * {@inheritdoc}
   */
  public function submitForm(array &$form, array &$form_state) {
    if ($form_state['values']['confirm']) {
      $this->commentStorage->delete($this->comments);
      Cache::invalidateTags(array('content' => TRUE));
      $count = count($form_state['values']['comments']);
      watchdog('content', 'Deleted @count comments.', array('@count' => $count));
      drupal_set_message(format_plural($count, 'Deleted 1 comment.', 'Deleted @count comments.'));
    }
    $form_state['redirect'] = 'admin/content/comment';
  }

}