You are here

public function WebformShareController::script in Webform 8.5

Same name and namespace in other branches
  1. 6.x modules/webform_share/src/Controller/WebformShareController.php \Drupal\webform_share\Controller\WebformShareController::script()

Returns a webform to be shared using (java)script.

Parameters

\Symfony\Component\HttpFoundation\Request $request: The current request object.

string|null $library: The iframe JavaScript library.

string|null $version: The iframe JavaScript library version.

Return value

array The webform rendered in a page template with only the content.

See also

\Drupal\webform_share\Theme\WebformShareThemeNegotiator

page--webform-share.html.twig

webform_share.libraries.yml

1 string reference to 'WebformShareController::script'
webform_share.routing.yml in modules/webform_share/webform_share.routing.yml
modules/webform_share/webform_share.routing.yml

File

modules/webform_share/src/Controller/WebformShareController.php, line 127

Class

WebformShareController
Provides route responses for webform share page, script, and embed.

Namespace

Drupal\webform_share\Controller

Code

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;
}