Skip to content
Snippets Groups Projects
Commit 36001057 authored by Kevin Paxman's avatar Kevin Paxman
Browse files

Merge branch 'feature/ISTWCMS-4208-ebremner-unpublishing' into '8.x-1.x'

Feature/istwcms 4208 ebremner unpublishing

See merge request !19
parents 23311836 9041cf56
No related branches found
No related tags found
1 merge request!19Feature/istwcms 4208 ebremner unpublishing
...@@ -64,14 +64,36 @@ class UwContentModerationForm extends ConfirmFormBase { ...@@ -64,14 +64,36 @@ class UwContentModerationForm extends ConfirmFormBase {
/** /**
* {@inheritdoc} * {@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->nid = $nid;
$this->vid = $vid; $this->vid = $vid;
$this->status = $status;
// Return the form from the parent (confirm form).
return parent::buildForm($form, $form_state); // 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 { ...@@ -79,6 +101,58 @@ class UwContentModerationForm extends ConfirmFormBase {
*/ */
public function submitForm(array &$form, FormStateInterface $form_state) { 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. // Set the options for the URL.
$options = ['absolute' => TRUE]; $options = ['absolute' => TRUE];
...@@ -88,8 +162,30 @@ class UwContentModerationForm extends ConfirmFormBase { ...@@ -88,8 +162,30 @@ class UwContentModerationForm extends ConfirmFormBase {
// Adding the redirect back to the node. // Adding the redirect back to the node.
$form_state->setRedirectUrl($url); $form_state->setRedirectUrl($url);
// Set the message that the permissions have been saved. // If the saved status is SAVED_UPDATED (2), means that we successfully
$this->messenger()->addStatus($this->t('There is still work to be done to unpublish, but we made it here.')); // 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 { ...@@ -103,8 +199,17 @@ class UwContentModerationForm extends ConfirmFormBase {
// Get the node object. // Get the node object.
$node = $this->entityTypeManager->getStorage('node')->load($this->nid); $node = $this->entityTypeManager->getStorage('node')->load($this->nid);
// Return the question to see if they want to publish the node. // Set the question (title of page) based on node status.
return $this->t('Are you sure you want to unpublish %node_title?', ['%node_title' => $node->getTitle()]); 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()]);
}
} }
/** /**
......
...@@ -6,7 +6,7 @@ uw_contact_access.form: ...@@ -6,7 +6,7 @@ uw_contact_access.form:
requirements: requirements:
_permission: 'access content access form' _permission: 'access content access form'
uw_content_moderation.form: uw_content_moderation.form:
path: '/admin/uw-content-moderation/{nid}/{vid}' path: '/admin/uw-content-moderation/{nid}/{vid}/{status}'
defaults: defaults:
_title: 'Content moderation' _title: 'Content moderation'
_form: '\Drupal\uw_cfg_common\Form\UwContentModerationForm' _form: '\Drupal\uw_cfg_common\Form\UwContentModerationForm'
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment