diff --git a/src/Form/UwContentModerationForm.php b/src/Form/UwContentModerationForm.php new file mode 100644 index 0000000000000000000000000000000000000000..d7923dfc42ba6cf3c16353184cc04a2dadb9296d --- /dev/null +++ b/src/Form/UwContentModerationForm.php @@ -0,0 +1,125 @@ +<?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); + } +} diff --git a/uw_cfg_common.module b/uw_cfg_common.module index 9edb90c3a7c3adf129209e3e724da0b6e070ddd0..a632c42ec4bd06469e8e98ce935d84db39823dbf 100644 --- a/uw_cfg_common.module +++ b/uw_cfg_common.module @@ -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; + } +} + diff --git a/uw_cfg_common.routing.yml b/uw_cfg_common.routing.yml index e64d3636cd0feba0b8c7cde126ee7bc4cd3ca1da..c22fe91f9ccaa3d712f7c8f48cd618cc2e4dc70f 100644 --- a/uw_cfg_common.routing.yml +++ b/uw_cfg_common.routing.yml @@ -5,3 +5,10 @@ uw_contact_access.form: _form: '\Drupal\uw_cfg_common\Form\UwContentAccessForm' requirements: _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