diff --git a/src/Form/UwContentModerationForm.php b/src/Form/UwContentModerationForm.php index 5615ea8e9dcf806fac03247f6251274b236ef9b4..47014ef222c43a651d6152dd5dc870ecbb6f0f20 100644 --- a/src/Form/UwContentModerationForm.php +++ b/src/Form/UwContentModerationForm.php @@ -6,11 +6,13 @@ use Drupal\Core\Access\AccessResult; use Drupal\Core\Entity\EntityTypeManagerInterface; use Drupal\Core\Form\ConfirmFormBase; use Drupal\Core\Form\FormStateInterface; +use Drupal\Core\Messenger\MessengerInterface; use Drupal\Core\Session\AccountInterface; use Drupal\Core\Session\AccountProxyInterface; use Drupal\Core\Url; use Drupal\uw_cfg_common\Service\UWService; use Symfony\Component\DependencyInjection\ContainerInterface; +use Symfony\Component\HttpFoundation\RedirectResponse; /** * Form class for the content access form. @@ -45,6 +47,13 @@ class UwContentModerationForm extends ConfirmFormBase { */ protected $currentUser; + /** + * The drupal messaging. + * + * @var \Drupal\Core\Messenger\MessengerInterface + */ + protected $messenger; + /** * Class constructor. * @@ -52,10 +61,17 @@ class UwContentModerationForm extends ConfirmFormBase { * The entity type manager. * @param \Drupal\Core\Session\AccountProxyInterface $currentUser * The entity type manager. + * @param \Drupal\Core\Messenger\MessengerInterface $messenger + * The drupal messaging. */ - public function __construct(EntityTypeManagerInterface $entity_type_manager, AccountProxyInterface $currentUser) { + public function __construct( + EntityTypeManagerInterface $entity_type_manager, + AccountProxyInterface $currentUser, + MessengerInterface $messenger + ) { $this->entityTypeManager = $entity_type_manager; $this->currentUser = $currentUser; + $this->messenger = $messenger; } /** @@ -65,7 +81,8 @@ class UwContentModerationForm extends ConfirmFormBase { // Instantiates this form class. return new static( $container->get('entity_type.manager'), - $container->get('current_user') + $container->get('current_user'), + $container->get('messenger') ); } @@ -117,6 +134,35 @@ class UwContentModerationForm extends ConfirmFormBase { $this->vid = $vid; $this->status = $status; + // If we are publishing this node, ensure that the meta + // content description is set. + if (!$this->status) { + + // Load the node of this revision. + $node = $this->entityTypeManager->getStorage('node')->loadRevision($this->vid); + + // 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]); + + // Setup the redirect. + $redirect = new RedirectResponse($url->toString()); + + // Send the redirect. + $redirect->send(); + + // Add the message that the description for content is required. + $this->messenger->addError('Field "Description of content" is required when publishing.'); + + // We need to exit the code so that redirect and the message + // work correctly, without this the message does not display. + exit; + } + } + // Get the form from the parent, we need this to ensure // that we have all the components (like confirm/cancel) // load with this form as well. @@ -142,7 +188,7 @@ class UwContentModerationForm extends ConfirmFormBase { return $form; } - /** + /** * {@inheritdoc} */ public function submitForm(array &$form, FormStateInterface $form_state) {