View source
<?php
namespace Drupal\webform;
use Drupal\Component\Render\FormattableMarkup;
use Drupal\Core\Messenger\MessengerInterface;
use Drupal\Core\Session\AccountInterface;
use Drupal\Component\Utility\Xss;
use Drupal\Core\Config\ConfigFactoryInterface;
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Render\RendererInterface;
use Drupal\Core\StringTranslation\StringTranslationTrait;
use Drupal\webform\Element\WebformHtmlEditor;
use Drupal\webform\EntityStorage\WebformEntityStorageTrait;
use Psr\Log\LoggerInterface;
class WebformMessageManager implements WebformMessageManagerInterface {
use StringTranslationTrait;
use WebformEntityStorageTrait;
protected $currentUser;
protected $configFactory;
protected $token;
protected $logger;
protected $renderer;
protected $requestHandler;
protected $tokenManager;
protected $webform;
protected $sourceEntity;
protected $webformSubmission;
protected $messenger;
public function __construct(AccountInterface $current_user, ConfigFactoryInterface $config_factory, EntityTypeManagerInterface $entity_type_manager, LoggerInterface $logger, RendererInterface $renderer, MessengerInterface $messenger, WebformRequestInterface $request_handler, WebformTokenManagerInterface $token_manager) {
$this->currentUser = $current_user;
$this->configFactory = $config_factory;
$this->entityTypeManager = $entity_type_manager;
$this->logger = $logger;
$this->renderer = $renderer;
$this->messenger = $messenger;
$this->requestHandler = $request_handler;
$this->tokenManager = $token_manager;
}
public function setWebformSubmission(WebformSubmissionInterface $webform_submission = NULL) {
$this->webformSubmission = $webform_submission;
if ($webform_submission) {
$this->webform = $webform_submission
->getWebform();
$this->sourceEntity = $webform_submission
->getSourceEntity();
}
}
public function setWebform(WebformInterface $webform = NULL) {
$this->webform = $webform;
}
public function setSourceEntity(EntityInterface $entity = NULL) {
$this->sourceEntity = $entity;
}
public function append(array $build, $key, $type = 'status') {
$message = $this
->build($key);
if ($message) {
$build['webform_message'][] = [
'#type' => 'webform_message',
'#message_message' => $message,
'#message_type' => $type,
'#weight' => -100,
];
}
return $build;
}
public function display($key, $type = 'status') {
if ($build = $this
->build($key)) {
$this->messenger
->addMessage($this->renderer
->renderPlain($build), $type);
}
}
public function render($key) {
$build = $this
->build($key);
return $build ? $this->renderer
->renderPlain($build) : NULL;
}
public function build($key) {
if ($message = $this
->get($key)) {
if (!is_array($message)) {
$message = [
'#markup' => $message,
'#allowed_tags' => Xss::getAdminTagList(),
];
}
$setting_message = $this
->getSetting($key);
if ($setting_message && strpos($setting_message, '[') !== FALSE) {
$message['#cache']['max-age'] = 0;
}
return $message;
}
else {
return [];
}
}
public function get($key) {
if ($custom_message = $this
->getCustomMessage($key)) {
return $custom_message;
}
$webform = $this->webform;
$source_entity = $this->sourceEntity;
switch ($key) {
case WebformMessageManagerInterface::DRAFT_PENDING_SINGLE:
$webform_draft = $this
->getSubmissionStorage()
->loadDraft($webform, $source_entity, $this->currentUser);
$args = [
':href' => $webform_draft
->getTokenUrl()
->toString(),
];
return $this
->getCustomMessage('draft_pending_single_message', $args);
case WebformMessageManagerInterface::DRAFT_PENDING_MULTIPLE:
$args = [
':href' => $this->requestHandler
->getUrl($webform, $source_entity, 'webform.user.drafts')
->toString(),
];
return $this
->getCustomMessage('draft_pending_multiple_message', $args);
case WebformMessageManagerInterface::PREVIOUS_SUBMISSION:
$webform_submission = $this
->getSubmissionStorage()
->getLastSubmission($webform, $source_entity, $this->currentUser);
$args = [
':href' => $this->requestHandler
->getUrl($webform_submission, $source_entity, 'webform.user.submission')
->toString(),
];
return $this
->getCustomMessage('previous_submission_message', $args);
case WebformMessageManagerInterface::PREVIOUS_SUBMISSIONS:
$args = [
':href' => $this->requestHandler
->getUrl($webform, $source_entity, 'webform.user.submissions')
->toString(),
];
return $this
->getCustomMessage('previous_submissions_message', $args);
}
switch ($key) {
case WebformMessageManagerInterface::ADMIN_PAGE:
return $this
->t('Only webform administrators are allowed to access this page and create new submissions.');
case WebformMessageManagerInterface::ADMIN_CLOSED:
$t_args = [
':href' => $webform
->toUrl('settings-form')
->toString(),
];
return $this
->t('This webform is <a href=":href">closed</a>. Only submission administrators are allowed to access this webform and create new submissions.', $t_args);
case WebformMessageManagerInterface::ADMIN_ARCHIVED:
$t_args = [
':href' => $webform
->toUrl('settings')
->toString(),
];
return $this
->t('This webform is <a href=":href">archived</a>. Only submission administrators are allowed to access this webform and create new submissions.', $t_args);
case WebformMessageManagerInterface::SUBMISSION_DEFAULT_CONFIRMATION:
$t_args = [
'%form' => $source_entity ? $source_entity
->label() : $webform
->label(),
];
return $this
->t('New submission added to %form.', $t_args);
case WebformMessageManagerInterface::FORM_SAVE_EXCEPTION:
$t_args = [
':handlers_href' => $webform
->toUrl('handlers')
->toString(),
':settings_href' => $webform
->toUrl('settings')
->toString(),
];
return $this
->t('This webform is currently not saving any submitted data. Please enable the <a href=":settings_href">saving of results</a> or add a <a href=":handlers_href">submission handler</a> to the webform.', $t_args);
case WebformMessageManagerInterface::HANDLER_SUBMISSION_REQUIRED:
$t_args = [
':href' => $webform
->toUrl('handlers')
->toString(),
];
return $this
->t('This webform\'s <a href=":href">submission handlers</a> requires submissions to be saved to the database.', $t_args);
case WebformMessageManagerInterface::SUBMISSION_UPDATED:
$t_args = [
'%form' => $source_entity ? $source_entity
->label() : $webform
->label(),
];
return $this
->t('Submission updated in %form.', $t_args);
case WebformMessageManagerInterface::SUBMISSION_TEST:
return $this
->t("The below webform has been prepopulated with custom/random test data. When submitted, this information <strong>will still be saved</strong> and/or <strong>sent to designated recipients</strong>.");
case WebformMessageManagerInterface::TEMPLATE_PREVIEW:
$t_args = [
':href' => $webform
->toUrl('duplicate-form')
->toString(),
];
return $this
->t('You are previewing the below template, which can be used to <a href=":href">create a new webform</a>. <strong>Submitted data will be ignored</strong>.', $t_args);
case WebformMessageManagerInterface::PREPOPULATE_SOURCE_ENTITY_TYPE:
case WebformMessageManagerInterface::PREPOPULATE_SOURCE_ENTITY_REQUIRED:
return $this
->t('This webform is not available. Please contact the site administrator.');
case WebformMessageManagerInterface::PREVIOUS_SUBMISSION:
$webform_submission = $this
->getSubmissionStorage()
->getLastSubmission($webform, $source_entity, $this->currentUser);
$args = [
':href' => $this->requestHandler
->getUrl($webform_submission, $source_entity, 'webform.user.submission')
->toString(),
];
return $this
->getCustomMessage('previous_submission_message', $args);
case WebformMessageManagerInterface::PREVIOUS_SUBMISSIONS:
$args = [
':href' => $this->requestHandler
->getUrl($webform, $source_entity, 'webform.user.submissions')
->toString(),
];
return $this
->getCustomMessage('previous_submissions_message', $args);
}
return FALSE;
}
public function log($key, $type = 'warning') {
$webform = $this->webform;
$context = [
'link' => $webform
->toLink($this
->t('Edit'), 'settings')
->toString(),
];
switch ($key) {
case WebformMessageManagerInterface::FORM_FILE_UPLOAD_EXCEPTION:
$message = 'To support file uploads the saving of submission must be enabled. <strong>All uploaded load files would be lost</strong> Please either uncheck \'Disable saving of submissions\' or remove all the file upload elements.';
break;
case WebformMessageManagerInterface::FORM_SAVE_EXCEPTION:
$context['%form'] = $webform
->label();
$message = '%form is not saving any submitted data and has been disabled.';
break;
case WebformMessageManagerInterface::PREPOPULATE_SOURCE_ENTITY_TYPE:
$context['%form'] = $webform
->label();
$message = '%form prepopulated source entity is not valid.';
break;
case WebformMessageManagerInterface::PREPOPULATE_SOURCE_ENTITY_REQUIRED:
$context['%form'] = $webform
->label();
$message = '%form prepopulated source entity is required.';
break;
}
$this->logger
->{$type}($message, $context);
}
protected function getSetting($key) {
$webform_settings = $this->webform ? $this->webform
->getSettings() : [];
if (!empty($webform_settings[$key])) {
$value = $webform_settings[$key];
if ($value === '[none]' || $value === (string) $this
->t('[none]')) {
return FALSE;
}
else {
return $value;
}
}
$default_settings = $this->configFactory
->get('webform.settings')
->get('settings');
if (!empty($default_settings['default_' . $key])) {
return $default_settings['default_' . $key];
}
return FALSE;
}
protected function getCustomMessage($key, array $arguments = []) {
$setting = $this
->getSetting($key);
if (!$setting) {
return FALSE;
}
$entity = $this->webformSubmission ?: $this->webform;
$message = $this->tokenManager
->replace($setting, $entity);
if ($arguments) {
$message = str_replace('href="#"', 'href=":href"', $message);
$message = new FormattableMarkup($message, $arguments);
}
return WebformHtmlEditor::checkMarkup($message);
}
}