Skip to content
Snippets Groups Projects
Commit 15048a68 authored by Igor Biki's avatar Igor Biki
Browse files

Merge branch 'feature/ISTWCMS-4166-publish-unpublish' into '8.x-1.x'

Feature/istwcms 4166 publish unpublish

See merge request !8
parents f928a3df 77a27dd8
No related branches found
No related tags found
1 merge request!8Feature/istwcms 4166 publish unpublish
<?php
namespace Drupal\uw_cfg_common\Form;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Form\ConfirmFormBase;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Url;
use Symfony\Component\DependencyInjection\ContainerInterface;
/**
* Form class for the content access form.
*/
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}
*/
public function getFormId() {
return 'uw_content_moderation_form';
}
/**
* {@inheritdoc}
*/
public function buildForm(array $form, FormStateInterface $form_state, $nid = NULL, $vid = NULL) {
// Set the node and version ids.
$this->nid = $nid;
$this->vid = $vid;
// Return the form from the parent (confirm form).
return parent::buildForm($form, $form_state);
}
/**
* {@inheritdoc}
*/
public function submitForm(array &$form, FormStateInterface $form_state) {
// Set the options for the URL.
$options = ['absolute' => TRUE];
// Return the URL back to the node.
$url = Url::fromRoute('entity.node.canonical', ['node' => $this->nid], $options);
// 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.'));
}
/**
* 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);
}
}
...@@ -173,3 +173,42 @@ function uw_cfg_common_toolbar_alter(&$items) { ...@@ -173,3 +173,42 @@ function uw_cfg_common_toolbar_alter(&$items) {
} }
} }
} }
/**
* Implements hook_preprocess_node().
*/
function uw_cfg_common_preprocess_node(&$variables) {
// Get the current path.
$path = explode('/', \Drupal::service('path.current')->getPath());
// The paths to place the content moderation block on. Made this
// an array to future proof, if there are more pages later.
$paths_for_content_moderation = ['latest'];
// Check if we are to add the content moderation place.
if (in_array(end($path), $paths_for_content_moderation)) {
// Add the content moderation block.
$variables['uw_content_moderation_form'] = \Drupal::formBuilder()->getForm('Drupal\content_moderation\Form\EntityModerationForm', $variables['node']);
}
else {
$block_manager = \Drupal::service('plugin.manager.block');
$plugin_block = $block_manager->createInstance('uw_cbl_content_moderation', []);
$access_result = $plugin_block->access(\Drupal::currentUser());
// Return empty render array if user doesn't have access.
// $access_result can be boolean or an AccessResult class
if (is_object($access_result) && $access_result->isForbidden() || is_bool($access_result) && !$access_result) {
return [];
}
$render = $plugin_block->build();
$variables['uw_content_moderation_form'] = $render;
}
}
...@@ -5,3 +5,10 @@ uw_contact_access.form: ...@@ -5,3 +5,10 @@ uw_contact_access.form:
_form: '\Drupal\uw_cfg_common\Form\UwContentAccessForm' _form: '\Drupal\uw_cfg_common\Form\UwContentAccessForm'
requirements: requirements:
_permission: 'access content access form' _permission: 'access content access form'
uw_content_moderation.form:
path: '/admin/uw-content-moderation/{nid}/{vid}'
defaults:
_title: 'Content moderation'
_form: '\Drupal\uw_cfg_common\Form\UwContentModerationForm'
requirements:
_permission: 'access content'
\ No newline at end of file
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