View source
<?php
namespace Drupal\webform\Controller;
use Drupal\Component\Render\FormattableMarkup;
use Drupal\Core\Controller\ControllerBase;
use Drupal\Core\DependencyInjection\ContainerInjectionInterface;
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Serialization\Yaml;
use Drupal\webform\Element\Webform as WebformElement;
use Drupal\webform\Routing\WebformUncacheableResponse;
use Drupal\webform\WebformInterface;
use Drupal\webform\WebformSubmissionInterface;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Drupal\Core\Cache\CacheableResponse;
class WebformEntityController extends ControllerBase implements ContainerInjectionInterface {
protected $renderer;
protected $requestHandler;
protected $tokenManager;
protected $webformEntityReferenceManager;
public static function create(ContainerInterface $container) {
$instance = parent::create($container);
$instance->renderer = $container
->get('renderer');
$instance->requestHandler = $container
->get('webform.request');
$instance->tokenManager = $container
->get('webform.token_manager');
$instance->webformEntityReferenceManager = $container
->get('webform.entity_reference_manager');
return $instance;
}
public function addForm(Request $request, WebformInterface $webform) {
return $webform
->getSubmissionForm();
}
public function css(Request $request, WebformInterface $webform) {
$assets = $webform
->getAssets();
if ($webform
->access('update')) {
return new WebformUncacheableResponse($assets['css'], 200, [
'Content-Type' => 'text/css',
]);
}
else {
return new CacheableResponse($assets['css'], 200, [
'Content-Type' => 'text/css',
]);
}
}
public function javascript(Request $request, WebformInterface $webform) {
$assets = $webform
->getAssets();
if ($webform
->access('update')) {
return new WebformUncacheableResponse($assets['javascript'], 200, [
'Content-Type' => 'text/javascript',
]);
}
else {
return new CacheableResponse($assets['javascript'], 200, [
'Content-Type' => 'text/javascript',
]);
}
}
public function confirmation(Request $request, WebformInterface $webform = NULL, WebformSubmissionInterface $webform_submission = NULL) {
if (!$webform) {
list($webform, $source_entity) = $this->requestHandler
->getWebformEntities();
}
else {
$source_entity = $this->requestHandler
->getCurrentSourceEntity('webform');
}
if ($token = $request
->get('token')) {
$webform_submission_storage = $this
->entityTypeManager()
->getStorage('webform_submission');
if ($entities = $webform_submission_storage
->loadByProperties([
'token' => $token,
])) {
$webform_submission = reset($entities);
}
}
if ($webform_submission) {
$webform_submission
->getWebform()
->invokeHandlers('overrideSettings', $webform_submission);
}
if ($webform
->hasVariants()) {
if ($webform_submission) {
$webform
->applyVariants($webform_submission);
}
else {
$variants = $this
->getVariants($request, $webform, $source_entity);
$webform
->applyVariants(NULL, $variants);
}
}
$title = $webform
->getSetting('confirmation_title') ?: ($source_entity ? $source_entity
->label() : $webform
->label());
$title = $this->tokenManager
->replace($title, $webform_submission ?: $webform);
$build = [
'#title' => $title,
'#theme' => 'webform_confirmation',
'#webform' => $webform,
'#source_entity' => $source_entity,
'#webform_submission' => $webform_submission,
];
$this->renderer
->addCacheableDependency($build, $webform);
if ($webform_submission) {
$this->renderer
->addCacheableDependency($build, $webform_submission);
}
if ($source_entity) {
$this->renderer
->addCacheableDependency($build, $source_entity);
}
return $build;
}
protected function getVariants(Request $request, WebformInterface $webform, EntityInterface $source_entity = NULL) {
$webform_variant = $request->query
->get('_webform_variant');
if ($webform_variant && ($webform
->access('update') || $webform
->access('test'))) {
return $webform_variant;
}
$field_name = $this->webformEntityReferenceManager
->getFieldName($source_entity);
if (!$field_name) {
return [];
}
$default_data = $source_entity->{$field_name}->default_data;
$default_data = $default_data ? Yaml::decode($default_data) : [];
$query = $request->query
->all();
$variants = [];
$element_keys = $webform
->getElementsVariant();
foreach ($element_keys as $element_key) {
$element = $webform
->getElement($element_key);
if (!empty($query[$element_key]) && !empty($element['#prepopulate'])) {
$variants[$element_key] = $query[$element_key];
}
elseif (!empty($default_data[$element_key])) {
$variants[$element_key] = $default_data[$element_key];
}
}
return $variants;
}
public function autocomplete(Request $request, $templates = FALSE, $archived = FALSE) {
$q = $request->query
->get('q');
$webform_storage = $this
->entityTypeManager()
->getStorage('webform');
$query = $webform_storage
->getQuery()
->range(0, 10)
->sort('title');
$or = $query
->orConditionGroup()
->condition('id', $q, 'CONTAINS')
->condition('title', $q, 'CONTAINS');
$query
->condition($or);
if ($templates) {
$query
->condition('template', TRUE);
}
elseif ($this
->moduleHandler()
->moduleExists('webform_templates')) {
$query
->condition('template', FALSE);
}
$query
->condition('archive', $archived);
$entity_ids = $query
->execute();
if (empty($entity_ids)) {
return new JsonResponse([]);
}
$webforms = $webform_storage
->loadMultiple($entity_ids);
$matches = [];
foreach ($webforms as $webform) {
if ($webform
->access('view')) {
$value = new FormattableMarkup('@label (@id)', [
'@label' => $webform
->label(),
'@id' => $webform
->id(),
]);
$matches[] = [
'value' => $value,
'label' => $value,
];
}
}
return new JsonResponse($matches);
}
public function accessDenied(WebformInterface $webform) {
return WebformElement::buildAccessDenied($webform);
}
public function accessDeniedTitle(WebformInterface $webform) {
return $webform
->getSetting('form_access_denied_title') ?: $this
->t('Access denied');
}
public function title(WebformInterface $webform = NULL) {
if (!$webform) {
list($webform, $source_entity) = $this->requestHandler
->getWebformEntities();
}
else {
$source_entity = $this->requestHandler
->getCurrentSourceEntity('webform');
}
if (!$source_entity || !method_exists($source_entity, 'label')) {
return $webform
->label();
}
if ($source_entity
->label() === $webform
->label()) {
return $webform
->label();
}
switch ($webform
->getSetting('form_title')) {
case WebformInterface::TITLE_SOURCE_ENTITY:
return $source_entity
->label();
case WebformInterface::TITLE_WEBFORM:
return $webform
->label();
case WebformInterface::TITLE_WEBFORM_SOURCE_ENTITY:
$t_args = [
'@source_entity' => $source_entity
->label(),
'@webform' => $webform
->label(),
];
return $this
->t('@webform: @source_entity', $t_args);
case WebformInterface::TITLE_SOURCE_ENTITY_WEBFORM:
default:
$t_args = [
'@source_entity' => $source_entity
->label(),
'@webform' => $webform
->label(),
];
return $this
->t('@source_entity: @webform', $t_args);
}
}
}