diff --git a/src/Form/UwContentModerationForm.php b/src/Form/UwContentModerationForm.php index e1c03a08b4c77627d9eff8c4d3fbc06f64a3d15c..87c42f0e87e6637325ab37b7a37d7387ae31b4f9 100644 --- a/src/Form/UwContentModerationForm.php +++ b/src/Form/UwContentModerationForm.php @@ -64,14 +64,36 @@ class UwContentModerationForm extends ConfirmFormBase { /** * {@inheritdoc} */ - public function buildForm(array $form, FormStateInterface $form_state, $nid = NULL, $vid = NULL) { + public function buildForm(array $form, FormStateInterface $form_state, $nid = NULL, $vid = NULL, $status = NULL) { - // Set the node and version ids. + // Set the node, version id and status. $this->nid = $nid; $this->vid = $vid; - - // Return the form from the parent (confirm form). - return parent::buildForm($form, $form_state); + $this->status = $status; + + // Get the form from the parent, we need this to ensure + // that we have all the components (like confirm/cancel) + // load with this form as well. + $form = parent::buildForm($form, $form_state); + + // Unset the description, we want to replace it with our + // description based on the node status. + unset($form['description']); + + // Set the description form element. + $form['description'] = [ + '#type' => 'markup', + ]; + + // Set the description based on the node status. + if ($this->status) { + $form['description']['#markup'] = $this->t('Are you sure that you want to unpublish the live revision of this content?'); + } + else { + $form['description']['#markup'] = $this->t('Are you sure that you want to publish the current revision of this content?'); + } + + return $form; } /** @@ -79,6 +101,58 @@ class UwContentModerationForm extends ConfirmFormBase { */ public function submitForm(array &$form, FormStateInterface $form_state) { + // If we are unpublishing, load the latest revision node. + if ($this->status) { + + // Load the node with the nid. + $node = $this->entityTypeManager->getStorage('node')->load($this->nid); + + // Get all the revisions for the current node. + $revision_ids = $this->entityTypeManager->getStorage('node')->revisionIds($node); + + // Get the node object with that latest revision, which is the end + // of the revision ids list. + $node = $this->entityTypeManager->getStorage('node')->loadRevision(end($revision_ids)); + } + + // If we are publishing, load the node with the nid. + else { + + // Get the node object. + $node = $this->entityTypeManager->getStorage('node')->loadRevision($this->vid); + } + + // If the node is currently published, then we want to + // unpublish this content, which will mean to move from + // two statesL upublish and draft. + if ($this->status) { + + // First set the node to unpublished, we need to do this + // because moving it to just draft will not cause it to + // become unpublished. + $node->set('moderation_state', 'uw_wf_unpublished'); + + // Save the node to move it to unpublished. + $saved_status = $node->save(); + + // Now set the node moderation state to draft. + $node->set('moderation_state', 'draft'); + + // Save the node with the moderation state at draft. + $saved_status = $node->save(); + } + + // If the node is already unpublished, we want to move to the + // published moderation state. + else { + + // Set the moderation state to publish. + $node->set('moderation_state', 'published'); + + // Save the node with the moderation state at published. + $saved_status = $node->save(); + } + // Set the options for the URL. $options = ['absolute' => TRUE]; @@ -88,8 +162,30 @@ class UwContentModerationForm extends ConfirmFormBase { // Adding the redirect back to the node. $form_state->setRedirectUrl($url); - // Set the message that the permissions have been saved. - $this->messenger()->addStatus($this->t('There is still work to be done to unpublish, but we made it here.')); + // If the saved status is SAVED_UPDATED (2), means that we successfully + // changed the moderation state so set the message appropriately. + if ($saved_status == SAVED_UPDATED) { + + if ($this->status) { + $this->messenger()->addStatus($this->t('You have successfully unpublished this content.')); + } + else { + $this->messenger()->addStatus($this->t('You have successfully published this content.')); + } + } + + // If the saved status is anything else, there was an + // error trying to change the moderation state so set + // the message appropriately. + else { + + if ($this->status) { + $this->messenger()->addError($this->t('There was an error trying to unpublish this content.')); + } + else { + $this->messenger()->addError($this->t('There was an error trying to publish this content.')); + } + } } /** @@ -103,8 +199,17 @@ class UwContentModerationForm extends ConfirmFormBase { // 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 $this->t('Are you sure you want to unpublish %node_title?', ['%node_title' => $node->getTitle()]); + // Set the question (title of page) based on node status. + if ($this->status) { + + // Return the question to see if they want to publish the node. + return t('Unpublish %node_title?', ['%node_title' => $node->getTitle()]); + } + else { + + // Return the question to see if they want to publish the node. + return t('Publish %node_title?', ['%node_title' => $node->getTitle()]); + } } /** diff --git a/uw_cfg_common.routing.yml b/uw_cfg_common.routing.yml index ad501c17ccf2c658215c554941a3aa7a7eadadce..cd86dba516fdd844199f59060c7fa1140128fc75 100644 --- a/uw_cfg_common.routing.yml +++ b/uw_cfg_common.routing.yml @@ -6,7 +6,7 @@ uw_contact_access.form: requirements: _permission: 'access content access form' uw_content_moderation.form: - path: '/admin/uw-content-moderation/{nid}/{vid}' + path: '/admin/uw-content-moderation/{nid}/{vid}/{status}' defaults: _title: 'Content moderation' _form: '\Drupal\uw_cfg_common\Form\UwContentModerationForm'