Skip to content
Snippets Groups Projects
Commit 1a03da1e authored by Eric Bremner's avatar Eric Bremner
Browse files

ISTWCMS-4208: fixing publish/unpublish on view tab

parent 1b9e760e
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). // Get the form from the parent, we need this to ensure
return parent::buildForm($form, $form_state); // 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 unpublish the live revision of this content?');
}
return $form;
} }
/** /**
...@@ -79,31 +101,57 @@ class UwContentModerationForm extends ConfirmFormBase { ...@@ -79,31 +101,57 @@ class UwContentModerationForm extends ConfirmFormBase {
*/ */
public function submitForm(array &$form, FormStateInterface $form_state) { public function submitForm(array &$form, FormStateInterface $form_state) {
// Get the node object. // If we are unpublishing, load the latest revision node.
$node = $this->entityTypeManager->getStorage('node')->loadRevision($this->vid); if ($this->status) {
// Load the node with the nid.
$node = $this->entityTypeManager->getStorage('node')->load($this->nid);
$node->set('moderation_state', 'uw_wf_archived'); // Get all the revisions for the current node.
$revision_ids = $this->entityTypeManager->getStorage('node')->revisionIds($node);
//$node->setTitle('testing2'); // Get the node object with that latest revision, which is the end
//$node->setPublished(0); // of the revision ids list.
//$node->setPublished(FALSE); $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);
}
$saved_status = $node->save(); // 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
if ($node->isDefaultRevision()) { // 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'); $node->set('moderation_state', 'draft');
if ($node instanceof RevisionLogInterface) {
$node->setRevisionLogMessage($form_state->getValue('revision_log'));
$node->setRevisionUserId($this->currentUser()->id());
}
$node->status = 0; // Save the node with the moderation state at draft.
//$node->setPublished(FALSE); $saved_status = $node->save();
$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];
...@@ -114,8 +162,30 @@ class UwContentModerationForm extends ConfirmFormBase { ...@@ -114,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 2, means that we successfully changed
$this->messenger()->addStatus($this->t('There is still work to be done to unpublish, but we made it here.')); // the moderation state so set the message appropriately.
if ($saved_status == 2) {
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.'));
}
}
} }
/** /**
...@@ -129,8 +199,17 @@ class UwContentModerationForm extends ConfirmFormBase { ...@@ -129,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 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