View source
<?php
namespace Drupal\webform_share\Controller;
use Drupal\Core\Cache\CacheableResponse;
use Drupal\Core\Controller\ControllerBase;
use Drupal\Core\Render\RendererInterface;
use Drupal\Core\Url;
use Drupal\webform\Element\WebformMessage;
use Drupal\webform\WebformMessageManagerInterface;
use Drupal\webform\WebformRequestInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\HttpFoundation\Request;
class WebformShareController extends ControllerBase {
protected $renderer;
protected $messageManager;
protected $requestHandler;
public function __construct(RendererInterface $renderer, WebformMessageManagerInterface $message_manager, WebformRequestInterface $request_handler) {
$this->renderer = $renderer;
$this->messageManager = $message_manager;
$this->requestHandler = $request_handler;
}
public static function create(ContainerInterface $container) {
return new static($container
->get('renderer'), $container
->get('webform.message_manager'), $container
->get('webform.request'));
}
public function page(Request $request, $library = NULL, $version = NULL) {
$webform = $this->requestHandler
->getCurrentWebform();
$source_entity = $this->requestHandler
->getCurrentSourceEntity([
'webform',
]);
$build = [];
$build['webform'] = [
'#type' => 'webform',
'#webform' => $webform,
'#source_entity' => $source_entity,
'#prefix' => '<div class="webform-share-submission-form">',
'#suffix' => '</div>',
];
$build['#attached']['library'][] = 'webform_share/webform_share.page';
if ($library && $version) {
$build['#attached']['library'][] = "webform_share/libraries.{$library}.{$version}";
}
$build['#attached']['drupalSettings']['webform_share']['page'] = TRUE;
return $build;
}
public function script(Request $request, $library = NULL, $version = NULL) {
$webform = $this->requestHandler
->getCurrentWebform();
$source_entity = $this->requestHandler
->getCurrentSourceEntity([
'webform',
]);
$build = [
'#type' => 'webform_share_iframe',
'#webform' => $webform,
'#source_entity' => $source_entity,
'#javascript' => TRUE,
'#query' => $request->query
->all(),
];
$iframe = $this->renderer
->renderPlain($build);
$iframe_script = json_encode($iframe);
$iframe_script = str_replace('src=\\"\\/\\/', 'src=\\"' . $request
->getScheme() . ':\\/\\/', $iframe_script);
$content = 'document.write(' . $iframe_script . ');';
$response = new CacheableResponse($content, 200, [
'Content-Type' => 'text/javascript',
]);
$response
->addCacheableDependency($webform);
if ($source_entity) {
$response
->addCacheableDependency($source_entity);
}
return $response;
}
public function preview(Request $request) {
$webform = $this->requestHandler
->getCurrentWebform();
$source_entity = $this->requestHandler
->getCurrentSourceEntity([
'webform',
]);
$build = [];
if ($this
->currentUser()
->isAuthenticated()) {
$build['message'] = [
'#type' => 'webform_message',
'#message_message' => [
'message' => [
'#markup' => $this
->t('To test anonymous user access to the below embedded webform, please log out or open the below link in a new private or incognito window.'),
'#suffix' => '<br/>',
],
'link' => [
'#type' => 'link',
'#url' => Url::fromRoute('<current>'),
'#title' => Url::fromRoute('<current>')
->setAbsolute()
->toString(),
],
],
'#message_type' => 'info',
'#message_close' => TRUE,
'#message_storage' => WebformMessage::STORAGE_SESSION,
];
}
$build['preview'] = [
'#type' => 'container',
'#attributes' => [
'class' => [
'webform-share-iframe-container',
],
],
];
$build['preview']['iframe'] = [
'#type' => 'webform_share_iframe',
'#webform' => $webform,
'#source_entity' => $source_entity,
'#javascript' => TRUE,
'#options' => [
'log' => TRUE,
],
'#query' => $request->query
->all(),
];
$build['#attached']['library'][] = 'webform_share/webform_share.admin';
return $build;
}
public function test(Request $request) {
$webform = $this->requestHandler
->getCurrentWebform();
$source_entity = $this->requestHandler
->getCurrentSourceEntity([
'webform',
]);
$build = [];
$build['message'] = [
'#type' => 'webform_message',
'#message_message' => $this->messageManager
->get(WebformMessageManagerInterface::SUBMISSION_TEST),
'#message_type' => 'warning',
'#message_close' => TRUE,
'#message_storage' => WebformMessage::STORAGE_SESSION,
];
$build['test'] = [
'#type' => 'container',
'#attributes' => [
'class' => [
'webform-share-iframe-container',
],
],
];
$build['test']['iframe'] = [
'#type' => 'webform_share_iframe',
'#webform' => $webform,
'#source_entity' => $source_entity,
'#test' => TRUE,
'#javascript' => TRUE,
'#options' => [
'log' => TRUE,
],
'#query' => $request->query
->all(),
];
$build['#attached']['library'][] = 'webform_share/webform_share.admin';
return $build;
}
}