From a59e1940f05b6639f76ef12e62b8bc08f9f878ce Mon Sep 17 00:00:00 2001
From: ebremner <ebremner@uwaterloo.ca>
Date: Mon, 26 Oct 2020 16:17:13 -0400
Subject: [PATCH] ISTWCMS-4166: adding preprocess node to place either content
 moderation block

---
 uw_cfg_common.module | 39 +++++++++++++++++++++++++++++++++++++++
 1 file changed, 39 insertions(+)

diff --git a/uw_cfg_common.module b/uw_cfg_common.module
index 9edb90c3..a632c42e 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;
+  }
+}
+
-- 
GitLab