class WebformShareController in Webform 6.x
Same name and namespace in other branches
- 8.5 modules/webform_share/src/Controller/WebformShareController.php \Drupal\webform_share\Controller\WebformShareController
Provides route responses for webform share page, script, and embed.
Hierarchy
- class \Drupal\Core\Controller\ControllerBase implements ContainerInjectionInterface uses LoggerChannelTrait, MessengerTrait, RedirectDestinationTrait, StringTranslationTrait
- class \Drupal\webform_share\Controller\WebformShareController
Expanded class hierarchy of WebformShareController
File
- modules/
webform_share/ src/ Controller/ WebformShareController.php, line 16
Namespace
Drupal\webform_share\ControllerView source
class WebformShareController extends ControllerBase {
/**
* The renderer.
*
* @var \Drupal\Core\Render\RendererInterface
*/
protected $renderer;
/**
* The webform message manager.
*
* @var \Drupal\webform\WebformMessageManagerInterface
*/
protected $messageManager;
/**
* The webform request handler.
*
* @var \Drupal\webform\WebformRequestInterface
*/
protected $requestHandler;
/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container) {
$instance = parent::create($container);
$instance->renderer = $container
->get('renderer');
$instance->messageManager = $container
->get('webform.message_manager');
$instance->requestHandler = $container
->get('webform.request');
return $instance;
}
/**
* Returns a webform to be shared as the page of an iframe.
*
* @param \Symfony\Component\HttpFoundation\Request $request
* The current request object.
* @param string|null $library
* The iframe JavaScript library.
* @param string|null $version
* The iframe JavaScript library version.
*
* @return array
* The webform rendered in a page template with only the content.
*
* @see \Drupal\webform_share\Theme\WebformShareThemeNegotiator
* @see page--webform-share.html.twig
* @see webform_share.libraries.yml
*/
public function page(Request $request, $library = NULL, $version = NULL) {
$webform = $this->requestHandler
->getCurrentWebform();
$source_entity = $this->requestHandler
->getCurrentSourceEntity([
'webform',
]);
$build = [];
// Webform.
$build['webform'] = [
'#type' => 'webform',
'#webform' => $webform,
'#source_entity' => $source_entity,
'#prefix' => '<div class="webform-share-submission-form">',
'#suffix' => '</div>',
];
// Attachments.
$build['#attached']['library'][] = 'webform_share/webform_share.page';
if ($library && $version) {
$build['#attached']['library'][] = "webform_share/libraries.{$library}.{$version}";
}
// Add setting notifying AjaxCommand that this page is shared via an
// embeddded iframe.
// @see Drupal.AjaxCommands.prototype.webformRefresh
$build['#attached']['drupalSettings']['webform_share']['page'] = TRUE;
return $build;
}
/**
* Returns a webform to be shared using (java)script.
*
* @param \Symfony\Component\HttpFoundation\Request $request
* The current request object.
* @param string|null $library
* The iframe JavaScript library.
* @param string|null $version
* The iframe JavaScript library version.
*
* @return array
* The webform rendered in a page template with only the content.
*
* @see \Drupal\webform_share\Theme\WebformShareThemeNegotiator
* @see page--webform-share.html.twig
* @see webform_share.libraries.yml
*/
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;
}
/**
* Returns a preview of a webform to be shared.
*
* @param \Symfony\Component\HttpFoundation\Request $request
* The current request object.
*
* @return array
* A render array containing a review of the webform to be shared.
*/
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;
}
/**
* Returns a test of a webform to be shared.
*
* @param \Symfony\Component\HttpFoundation\Request $request
* The current request object.
*
* @return array
* A render array containing a review of the webform to be shared.
*/
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;
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
ControllerBase:: |
protected | property | The configuration factory. | |
ControllerBase:: |
protected | property | The current user service. | 1 |
ControllerBase:: |
protected | property | The entity form builder. | |
ControllerBase:: |
protected | property | The entity type manager. | |
ControllerBase:: |
protected | property | The form builder. | 2 |
ControllerBase:: |
protected | property | The key-value storage. | 1 |
ControllerBase:: |
protected | property | The language manager. | 1 |
ControllerBase:: |
protected | property | The module handler. | 2 |
ControllerBase:: |
protected | property | The state service. | |
ControllerBase:: |
protected | function | Returns the requested cache bin. | |
ControllerBase:: |
protected | function | Retrieves a configuration object. | |
ControllerBase:: |
private | function | Returns the service container. | |
ControllerBase:: |
protected | function | Returns the current user. | 1 |
ControllerBase:: |
protected | function | Retrieves the entity form builder. | |
ControllerBase:: |
protected | function | Retrieves the entity type manager. | |
ControllerBase:: |
protected | function | Returns the form builder service. | 2 |
ControllerBase:: |
protected | function | Returns a key/value storage collection. | 1 |
ControllerBase:: |
protected | function | Returns the language manager service. | 1 |
ControllerBase:: |
protected | function | Returns the module handler. | 2 |
ControllerBase:: |
protected | function | Returns a redirect response object for the specified route. | |
ControllerBase:: |
protected | function | Returns the state storage service. | |
LoggerChannelTrait:: |
protected | property | The logger channel factory service. | |
LoggerChannelTrait:: |
protected | function | Gets the logger for a specific channel. | |
LoggerChannelTrait:: |
public | function | Injects the logger channel factory. | |
MessengerTrait:: |
protected | property | The messenger. | 27 |
MessengerTrait:: |
public | function | Gets the messenger. | 27 |
MessengerTrait:: |
public | function | Sets the messenger. | |
RedirectDestinationTrait:: |
protected | property | The redirect destination service. | 1 |
RedirectDestinationTrait:: |
protected | function | Prepares a 'destination' URL query parameter for use with \Drupal\Core\Url. | |
RedirectDestinationTrait:: |
protected | function | Returns the redirect destination service. | |
RedirectDestinationTrait:: |
public | function | Sets the redirect destination service. | |
StringTranslationTrait:: |
protected | property | The string translation service. | 4 |
StringTranslationTrait:: |
protected | function | Formats a string containing a count of items. | |
StringTranslationTrait:: |
protected | function | Returns the number of plurals supported by a given language. | |
StringTranslationTrait:: |
protected | function | Gets the string translation service. | |
StringTranslationTrait:: |
public | function | Sets the string translation service to use. | 2 |
StringTranslationTrait:: |
protected | function | Translates a string to the current language or to a given language. | |
WebformShareController:: |
protected | property | The webform message manager. | |
WebformShareController:: |
protected | property | The renderer. | |
WebformShareController:: |
protected | property | The webform request handler. | |
WebformShareController:: |
public static | function |
Instantiates a new instance of this class. Overrides ControllerBase:: |
|
WebformShareController:: |
public | function | Returns a webform to be shared as the page of an iframe. | |
WebformShareController:: |
public | function | Returns a preview of a webform to be shared. | |
WebformShareController:: |
public | function | Returns a webform to be shared using (java)script. | |
WebformShareController:: |
public | function | Returns a test of a webform to be shared. |