Skip to content
Snippets Groups Projects
Commit 9d319bd6 authored by Eric Bremner's avatar Eric Bremner Committed by Igor Biki
Browse files

ISTWCMS-5506: using the require on publish module to look for nodes that can not be published

parent d9cff0a3
No related branches found
No related tags found
1 merge request!245ISTWCMS-5506: adding check that description of content is filled in before...
...@@ -10,9 +10,12 @@ use Drupal\Core\Messenger\MessengerInterface; ...@@ -10,9 +10,12 @@ use Drupal\Core\Messenger\MessengerInterface;
use Drupal\Core\Session\AccountInterface; use Drupal\Core\Session\AccountInterface;
use Drupal\Core\Session\AccountProxyInterface; use Drupal\Core\Session\AccountProxyInterface;
use Drupal\Core\Url; use Drupal\Core\Url;
use Drupal\field\FieldConfigInterface;
use Drupal\uw_cfg_common\Service\UWService; use Drupal\uw_cfg_common\Service\UWService;
use Symfony\Component\DependencyInjection\ContainerInterface; use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\HttpFoundation\RedirectResponse; use Symfony\Component\HttpFoundation\RedirectResponse;
use Drupal\require_on_publish\Plugin\Validation\Constraint\RequireOnPublish;
/** /**
* Form class for the content access form. * Form class for the content access form.
...@@ -138,28 +141,59 @@ class UwContentModerationForm extends ConfirmFormBase { ...@@ -138,28 +141,59 @@ class UwContentModerationForm extends ConfirmFormBase {
// content description is set. // content description is set.
if (!$this->status) { 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. // 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 // Get the URL to the node.
// redirect back to node page. $url = Url::fromRoute('entity.node.canonical', ['node' => $this->nid]);
if (!$node->field_uw_meta_description->getValue()) {
// Get the URL to the node. // Setup the redirect.
$url = Url::fromRoute('entity.node.canonical', ['node' => $this->nid]); $redirect = new RedirectResponse($url->toString());
// Setup the redirect. // Send the redirect.
$redirect = new RedirectResponse($url->toString()); $redirect->send();
// Send the redirect. // Get the message to be dispalyed, which comes from the require
$redirect->send(); // on publish class.
$message = $this->t($constraint->message, ['%field_label' => $field_config->getLabel()]);
// Add the message that the description for content is required. // Add the message that the description for content is required.
$this->messenger->addError('Field "Description of content" is required when publishing.'); $this->messenger->addError($message);
// We need to exit the code so that redirect and the message // We need to exit the code so that redirect and the message
// work correctly, without this the message does not display. // work correctly, without this the message does not display.
exit; exit;
}
}
} }
} }
......
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