diff --git a/src/Form/UwContentModerationForm.php b/src/Form/UwContentModerationForm.php index ca6c5a3bffee704837277031c08610f34a75c126..70852806d2aca1e33911c2a5a16c5d425c89251c 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; + } + } } }