View source
<?php
namespace Drupal\webform\Controller;
use Drupal\Component\Render\FormattableMarkup;
use Drupal\Core\Ajax\AjaxResponse;
use Drupal\Core\Ajax\AnnounceCommand;
use Drupal\Core\Ajax\HtmlCommand;
use Drupal\Core\Controller\ControllerBase;
use Drupal\Core\Render\RendererInterface;
use Drupal\webform\Element\WebformHtmlEditor;
use Drupal\webform\WebformInterface;
use Drupal\webform\WebformSubmissionInterface;
use Drupal\webform\WebformRequestInterface;
use Drupal\webform\WebformTokenManagerInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
class WebformSubmissionController extends ControllerBase {
protected $renderer;
protected $requestHandler;
protected $tokenManager;
public function __construct(RendererInterface $renderer, WebformRequestInterface $request_handler, WebformTokenManagerInterface $token_manager) {
$this->renderer = $renderer;
$this->requestHandler = $request_handler;
$this->tokenManager = $token_manager;
}
public static function create(ContainerInterface $container) {
return new static($container
->get('renderer'), $container
->get('webform.request'), $container
->get('webform.token_manager'));
}
public function sticky(WebformSubmissionInterface $webform_submission) {
$webform_submission
->setSticky(!$webform_submission
->isSticky())
->save();
$selector = '#webform-submission-' . $webform_submission
->id() . '-sticky';
$response = new AjaxResponse();
$response
->addCommand(new HtmlCommand($selector, static::buildSticky($webform_submission)));
$t_args = [
'@label' => $webform_submission
->label(),
];
$text = $webform_submission
->isSticky() ? $this
->t('@label flagged/starred.', $t_args) : $this
->t('@label unflagged/unstarred.', $t_args);
$response
->addCommand(new AnnounceCommand($text));
return $response;
}
public function locked(WebformSubmissionInterface $webform_submission) {
$webform_submission
->setLocked(!$webform_submission
->isLocked())
->save();
$selector = '#webform-submission-' . $webform_submission
->id() . '-locked';
$response = new AjaxResponse();
$response
->addCommand(new HtmlCommand($selector, static::buildLocked($webform_submission)));
$t_args = [
'@label' => $webform_submission
->label(),
];
$text = $webform_submission
->isLocked() ? $this
->t('@label locked.', $t_args) : $this
->t('@label unlocked.', $t_args);
$response
->addCommand(new AnnounceCommand($text));
return $response;
}
public static function buildSticky(WebformSubmissionInterface $webform_submission) {
$t_args = [
'@label' => $webform_submission
->label(),
];
$args = [
'@state' => $webform_submission
->isSticky() ? 'on' : 'off',
'@label' => $webform_submission
->isSticky() ? t('Unstar/Unflag @label', $t_args) : t('Star/flag @label', $t_args),
];
return new FormattableMarkup('<span class="webform-icon webform-icon-sticky webform-icon-sticky--@state"></span><span class="visually-hidden">@label</span>', $args);
}
public static function buildLocked(WebformSubmissionInterface $webform_submission) {
$t_args = [
'@label' => $webform_submission
->label(),
];
$args = [
'@state' => $webform_submission
->isLocked() ? 'on' : 'off',
'@label' => $webform_submission
->isLocked() ? t('Unlock @label', $t_args) : t('Lock @label', $t_args),
];
return new FormattableMarkup('<span class="webform-icon webform-icon-lock webform-icon-locked--@state"></span><span class="visually-hidden">@label</span>', $args);
}
public function accessDenied(WebformInterface $webform, WebformSubmissionInterface $webform_submission) {
$config = $this
->config('webform.settings');
$message = $webform
->getSetting('submission_access_denied_message') ?: $config
->get('settings.default_submission_access_denied_message');
$message = $this->tokenManager
->replace($message, $webform_submission);
$attributes = $webform
->getSetting('submission_access_denied_attributes');
$attributes['class'][] = 'webform-submission-access-denied';
$build = [
'#type' => 'container',
'#attributes' => $attributes,
'message' => WebformHtmlEditor::checkMarkup($message),
];
$this->renderer
->addCacheableDependency($build, $config);
$this->renderer
->addCacheableDependency($build, $webform);
return $build;
}
public function accessDeniedTitle(WebformInterface $webform) {
return $webform
->getSetting('submission_access_denied_title') ?: $this
->t('Access denied');
}
}