WebformShareScript.php in Webform 6.x
File
modules/webform_share/src/Element/WebformShareScript.php
View source
<?php
namespace Drupal\webform_share\Element;
use Drupal\Core\Render\Element\RenderElement;
use Drupal\Core\Security\TrustedCallbackInterface;
use Drupal\Core\Url;
use Drupal\webform\Plugin\WebformSourceEntity\QueryStringWebformSourceEntity;
class WebformShareScript extends RenderElement implements TrustedCallbackInterface {
public function getInfo() {
$class = get_class($this);
return [
'#webform' => NULL,
'#source_entity' => NULL,
'#query' => [],
'#theme' => 'webform_share_script',
'#pre_render' => [
[
$class,
'preRenderWebformShareScript',
],
],
];
}
public static function preRenderWebformShareScript($element) {
$webform = $element['#webform'];
$source_entity = $element['#source_entity'];
$route_name = 'entity.webform.share_script';
$route_parameters = [
'webform' => $webform
->id(),
];
$route_options = QueryStringWebformSourceEntity::getRouteOptionsQuery($source_entity);
if ($element['#query']) {
$route_options += [
'query' => [],
];
$route_options['query'] += $element['#query'];
}
$url = Url::fromRoute($route_name, $route_parameters, $route_options);
$script = preg_replace('#^https?:#', '', $url
->setAbsolute()
->toString());
$element['#script'] = $script;
return $element;
}
public static function trustedCallbacks() {
return [
'preRenderWebformShareScript',
];
}
}