You are here

public static function WebformShareScript::preRenderWebformShareScript in Webform 6.x

Same name and namespace in other branches
  1. 8.5 modules/webform_share/src/Element/WebformShareScript.php \Drupal\webform_share\Element\WebformShareScript::preRenderWebformShareScript()

Webform share iframe element pre render callback.

File

modules/webform_share/src/Element/WebformShareScript.php, line 36

Class

WebformShareScript
Provides a render element that creates a <script> tag to share a webform.

Namespace

Drupal\webform_share\Element

Code

public static function preRenderWebformShareScript($element) {

  /** @var \Drupal\webform\WebformInterface $webform */
  $webform = $element['#webform'];

  /** @var \Drupal\Core\Entity\EntityInterface $source_entity */
  $source_entity = $element['#source_entity'];
  $route_name = 'entity.webform.share_script';
  $route_parameters = [
    'webform' => $webform
      ->id(),
  ];
  $route_options = QueryStringWebformSourceEntity::getRouteOptionsQuery($source_entity);

  // Append prepopulate and variant query to route options.
  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;
}