From 9d319bd6aef4445abc5aa465f88db3609481e01c Mon Sep 17 00:00:00 2001
From: Eric Bremner <ebremner@uwaterloo.ca>
Date: Tue, 7 Jun 2022 14:09:29 -0400
Subject: [PATCH] ISTWCMS-5506: using the require on publish module to look for
 nodes that can not be published

---
 src/Form/UwContentModerationForm.php | 64 +++++++++++++++++++++-------
 1 file changed, 49 insertions(+), 15 deletions(-)

diff --git a/src/Form/UwContentModerationForm.php b/src/Form/UwContentModerationForm.php
index ca6c5a3b..70852806 100644
--- a/src/Form/UwContentModerationForm.php
+++ b/src/Form/UwContentModerationForm.php
@@ -10,9 +10,12 @@ use Drupal\Core\Messenger\MessengerInterface;
 use Drupal\Core\Session\AccountInterface;
 use Drupal\Core\Session\AccountProxyInterface;
 use Drupal\Core\Url;
+use Drupal\field\FieldConfigInterface;
 use Drupal\uw_cfg_common\Service\UWService;
 use Symfony\Component\DependencyInjection\ContainerInterface;
 use Symfony\Component\HttpFoundation\RedirectResponse;
+use Drupal\require_on_publish\Plugin\Validation\Constraint\RequireOnPublish;
+
 
 /**
  * Form class for the content access form.
@@ -138,28 +141,59 @@ class UwContentModerationForm extends ConfirmFormBase {
     // content description is set.
     if (!$this->status) {
 
+      /**
+       * This code is taken from the require_on_publish
+       * module, there was no easy way to use the validator
+       * inside this class.
+       */
+
       // Load the node of this revision.
-      $node = $this->entityTypeManager->getStorage('node')->loadRevision($this->vid);
+      $entity = $this->entityTypeManager->getStorage('node')->loadRevision($this->vid);
+
+      /** @var \Drupal\Core\Field\FieldItemListInterface $field */
+      foreach ($entity->getFields() as $field) {
+
+        /** @var \Drupal\Core\Field\FieldConfigInterface $field_config */
+        $field_config = $field->getFieldDefinition();
+        if (!($field_config instanceof FieldConfigInterface)) {
+          continue;
+        }
+
+        if (!$field->isEmpty()) {
+          continue;
+        }
+
+        // If the field has require on publish, check that it has a value.
+        if ($field_config->getThirdPartySetting('require_on_publish', 'require_on_publish', FALSE)) {
+
+          // If the field does not have a value, set the message and redirect.
+          if (!$field->getValue()) {
+
+            // Load the Require on publish class that contains
+            // the message to be displayed.
+            $constraint = new RequireOnPublish();
 
-      // If there is no meta content description, set message and
-      // redirect back to node page.
-      if (!$node->field_uw_meta_description->getValue()) {
+            // Get the URL to the node.
+            $url = Url::fromRoute('entity.node.canonical', ['node' => $this->nid]);
 
-        // Get the URL to the node.
-        $url = Url::fromRoute('entity.node.canonical', ['node' => $this->nid]);
+            // Setup the redirect.
+            $redirect = new RedirectResponse($url->toString());
 
-        // Setup the redirect.
-        $redirect = new RedirectResponse($url->toString());
+            // Send the redirect.
+            $redirect->send();
 
-        // Send the redirect.
-        $redirect->send();
+            // Get the message to be dispalyed, which comes from the require
+            // on publish class.
+            $message = $this->t($constraint->message, ['%field_label' => $field_config->getLabel()]);
 
-        // Add the message that the description for content is required.
-        $this->messenger->addError('Field "Description of content" is required when publishing.');
+            // Add the message that the description for content is required.
+            $this->messenger->addError($message);
 
-        // We need to exit the code so that redirect and the message
-        // work correctly, without this  the message does not display.
-        exit;
+            // We need to exit the code so that redirect and the message
+            // work correctly, without this  the message does not display.
+            exit;
+          }
+        }
       }
     }
 
-- 
GitLab