From cbcdbcd4e26178bea22db8be086446ebb0ad686c Mon Sep 17 00:00:00 2001
From: ebremner <ebremner@uwaterloo.ca>
Date: Mon, 9 Nov 2020 15:41:47 -0500
Subject: [PATCH] ISTWCMS-4166: adding the confirm form for content moderation

---
 src/Form/UwContentModerationForm.php | 87 ++++++++++++++++++++++++++--
 1 file changed, 82 insertions(+), 5 deletions(-)

diff --git a/src/Form/UwContentModerationForm.php b/src/Form/UwContentModerationForm.php
index f730b4f4..380c2c04 100644
--- a/src/Form/UwContentModerationForm.php
+++ b/src/Form/UwContentModerationForm.php
@@ -2,14 +2,58 @@
 
 namespace Drupal\uw_cfg_common\Form;
 
-use Drupal\Core\Form\FormBase;
+use Drupal\Core\Entity\EntityTypeManagerInterface;
+use Drupal\Core\Form\ConfirmFormBase;
 use Drupal\Core\Form\FormStateInterface;
-use Drupal\uw_cfg_common\UwPermissions\UwPermissions;
+use Drupal\Core\Url;
+use Symfony\Component\DependencyInjection\ContainerInterface;
 
 /**
  * Form class for the content access form.
  */
-class UwContentModerationForm extends FormBase {
+class UwContentModerationForm extends ConfirmFormBase {
+
+  /**
+   * The entity type manager.
+   *
+   * @var \Drupal\Core\Entity\EntityTypeManagerInterface
+   */
+  protected $entityTypeManager;
+
+  /**
+   * The nid (node id).
+   *
+   * @var int
+   */
+  protected $nid;
+
+  /**
+   * The vid (version id).
+   *
+   * @var int
+   */
+  protected $vid;
+
+  /**
+   * Class constructor.
+   *
+   * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
+   *   The entity type manager.
+   */
+  public function __construct(EntityTypeManagerInterface $entity_type_manager) {
+    $this->entityTypeManager = $entity_type_manager;
+  }
+
+  /**
+   * {@inheritdoc}
+   */
+  public static function create(ContainerInterface $container) {
+    // Instantiates this form class.
+    return new static(
+      $container->get('entity_type.manager')
+    );
+  }
+
 
   /**
    * {@inheritdoc}
@@ -23,9 +67,12 @@ class UwContentModerationForm extends FormBase {
    */
   public function buildForm(array $form, FormStateInterface $form_state, $nid = NULL, $vid = NULL) {
 
-    $form = [];
+    // Set the node and version ids.
+    $this->nid = $nid;
+    $this->vid = $vid;
 
-    return $form;
+    // Return the form from the parent (confirm form).
+    return parent::buildForm($form, $form_state);
   }
 
   /**
@@ -36,4 +83,34 @@ class UwContentModerationForm extends FormBase {
     // Set the message that the permissions have been saved.
     $this->messenger()->addStatus($this->t('The changes have been saved.'));
   }
+
+  /**
+   * Returns the question to ask the user.
+   *
+   * @return \Drupal\Core\StringTranslation\TranslatableMarkup
+   *   The form question. The page title will be set to this value.
+   */
+  public function getQuestion() {
+
+    // Get the node object.
+    $node = $this->entityTypeManager->getStorage('node')->load($this->nid);
+
+    // Return the question to see if they want to publish the node.
+    return t('Are you sure you want to unpublish %node_title?', ['%node_title' => $node->getTitle()]);
+  }
+
+  /**
+   * Returns the route to go to if the user cancels the action.
+   *
+   * @return \Drupal\Core\Url
+   *   A URL object.
+   */
+  public function getCancelUrl() {
+
+    // Set the options for the URL.
+    $options = ['absolute' => TRUE];
+
+    // Return the URL back to the node.
+    return Url::fromRoute('entity.node.canonical', ['node' => $this->nid], $options);
+  }
 }
-- 
GitLab