Updated Drupal to 8.6. This goes with the following updates because it's possible...
[yaffs-website] / web / core / modules / comment / src / Form / ConfirmDeleteMultiple.php
index 590624831632a47541b5d7c3f394bc90cf727533..825b0af6905504da10d26efdbaa982652009a7a0 100644 (file)
@@ -2,63 +2,21 @@
 
 namespace Drupal\comment\Form;
 
-use Drupal\comment\CommentStorageInterface;
-use Drupal\Component\Utility\Html;
-use Drupal\Core\Form\ConfirmFormBase;
-use Drupal\Core\Form\FormStateInterface;
+use Drupal\Core\Entity\Form\DeleteMultipleForm as EntityDeleteMultipleForm;
 use Drupal\Core\Url;
-use Symfony\Component\DependencyInjection\ContainerInterface;
 
 /**
  * Provides the comment multiple delete confirmation form.
+ *
+ * @internal
  */
-class ConfirmDeleteMultiple extends ConfirmFormBase {
-
-  /**
-   * The comment storage.
-   *
-   * @var \Drupal\comment\CommentStorageInterface
-   */
-  protected $commentStorage;
-
-  /**
-   * An array of comments to be deleted.
-   *
-   * @var \Drupal\comment\CommentInterface[]
-   */
-  protected $comments;
-
-  /**
-   * Creates an new ConfirmDeleteMultiple form.
-   *
-   * @param \Drupal\comment\CommentStorageInterface $comment_storage
-   *   The comment storage.
-   */
-  public function __construct(CommentStorageInterface $comment_storage) {
-    $this->commentStorage = $comment_storage;
-  }
-
-  /**
-   * {@inheritdoc}
-   */
-  public static function create(ContainerInterface $container) {
-    return new static(
-      $container->get('entity.manager')->getStorage('comment')
-    );
-  }
-
-  /**
-   * {@inheritdoc}
-   */
-  public function getFormId() {
-    return 'comment_multiple_delete_confirm';
-  }
+class ConfirmDeleteMultiple extends EntityDeleteMultipleForm {
 
   /**
    * {@inheritdoc}
    */
   public function getQuestion() {
-    return $this->t('Are you sure you want to delete these comments and all their children?');
+    return $this->formatPlural(count($this->selection), 'Are you sure you want to delete this comment and all its children?', 'Are you sure you want to delete these comments and all their children?');
   }
 
   /**
@@ -71,55 +29,15 @@ class ConfirmDeleteMultiple extends ConfirmFormBase {
   /**
    * {@inheritdoc}
    */
-  public function getConfirmText() {
-    return $this->t('Delete comments');
-  }
-
-  /**
-   * {@inheritdoc}
-   */
-  public function buildForm(array $form, FormStateInterface $form_state) {
-    $edit = $form_state->getUserInput();
-
-    $form['comments'] = [
-      '#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] = [
-        '#type' => 'hidden',
-        '#value' => $cid,
-        '#prefix' => '<li>',
-        '#suffix' => Html::escape($comment->label()) . '</li>'
-      ];
-      $comment_counter++;
-    }
-    $form['operation'] = ['#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->setRedirect('comment.admin');
-    }
-
-    return parent::buildForm($form, $form_state);
+  protected function getDeletedMessage($count) {
+    return $this->formatPlural($count, 'Deleted @count comment.', 'Deleted @count comments.');
   }
 
   /**
    * {@inheritdoc}
    */
-  public function submitForm(array &$form, FormStateInterface $form_state) {
-    if ($form_state->getValue('confirm')) {
-      $this->commentStorage->delete($this->comments);
-      $count = count($form_state->getValue('comments'));
-      $this->logger('comment')->notice('Deleted @count comments.', ['@count' => $count]);
-      drupal_set_message($this->formatPlural($count, 'Deleted 1 comment.', 'Deleted @count comments.'));
-    }
-    $form_state->setRedirectUrl($this->getCancelUrl());
+  protected function getInaccessibleMessage($count) {
+    return $this->formatPlural($count, "@count comment has not been deleted because you do not have the necessary permissions.", "@count comments have not been deleted because you do not have the necessary permissions.");
   }
 
 }