You are here

function template_preprocess_webform_confirmation in Webform 8.5

Same name and namespace in other branches
  1. 6.3 webform.module \template_preprocess_webform_confirmation()
  2. 6.2 webform.module \template_preprocess_webform_confirmation()
  3. 7.4 webform.module \template_preprocess_webform_confirmation()
  4. 7.3 webform.module \template_preprocess_webform_confirmation()
  5. 6.x includes/webform.theme.template.inc \template_preprocess_webform_confirmation()

Prepares variables for webform confirmation templates.

Default template: webform-confirmation.html.twig.

Parameters

array $variables: An associative array containing the following key:

  • webform: A webform.
  • webform_submission: A webform submission.
  • source_entity: A webform submission source entity.

File

includes/webform.theme.template.inc, line 109
Preprocessors and helper functions to make theming easier.

Code

function template_preprocess_webform_confirmation(array &$variables) {

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

  /** @var \Drupal\Core\Entity\EntityInterface $source_entity */
  $source_entity = $variables['source_entity'];

  /** @var \Drupal\webform\WebformSubmissionInterface $webform_submission */
  $webform_submission = $variables['webform_submission'];

  /** @var \Drupal\webform\WebformMessageManagerInterface $message_manager */
  $message_manager = \Drupal::service('webform.message_manager');
  $message_manager
    ->setWebformSubmission($webform_submission);

  // Must set webform and source entity because webform submission could be
  // NULL.
  $message_manager
    ->setWebform($webform);
  $message_manager
    ->setSourceEntity($source_entity);

  // Assets: Add custom shared and webform specific CSS and JS.
  // @see webform_css_alter()
  // @see webform_js_alter()
  $assets = $webform
    ->getAssets();
  foreach ($assets as $type => $value) {
    if ($value) {
      $variables['#attached']['library'][] = "webform/webform.assets.{$type}";
      $variables['#attached']['drupalSettings']['webform']['assets'][$type][$webform
        ->id()] = Crypt::hashBase64($value);
    }
  }
  $settings = $webform
    ->getSettings();

  // Set progress.
  if ($webform
    ->getPages() && $settings['wizard_confirmation'] && ($settings['wizard_progress_bar'] || $settings['wizard_progress_pages'] || $settings['wizard_progress_percentage'])) {
    $variables['progress'] = [
      '#theme' => 'webform_progress',
      '#webform' => $webform,
      '#webform_submission' => $webform_submission,
      '#current_page' => 'webform_confirmation',
    ];
  }

  // Set message.
  $variables['message'] = $message_manager
    ->build(WebformMessageManagerInterface::SUBMISSION_CONFIRMATION_MESSAGE);

  // Set attributes.
  $variables['attributes'] = new Attribute($settings['confirmation_attributes']);

  // Set back.
  $variables['back'] = $settings['confirmation_back'];
  $variables['back_label'] = $settings['confirmation_back_label'] ?: \Drupal::config('webform.settings')
    ->get('settings.default_confirmation_back_label');
  $variables['back_attributes'] = new Attribute($settings['confirmation_back_attributes']);

  // Get query string parameters.
  $query = \Drupal::request()->query
    ->all();

  // Add Ajax trigger to back link except for webform with unique limits which
  // break the ajax callback.
  // @see \Drupal\webform\WebformSubmissionForm::getCustomForm
  // @see Drupal.behaviors.webformConfirmationBackAjax (js/webform.ajax.js)
  $is_ajax = !empty($query['ajax_form']) ? TRUE : FALSE;
  $is_limit_unique = $webform && ($webform
    ->getSetting('limit_total_unique') || $webform
    ->getSetting('limit_user_unique'));
  if (!empty($is_ajax) && !$is_limit_unique) {
    $variables['back_attributes']
      ->addClass('js-webform-confirmation-back-link-ajax');
  }

  // Apply all passed query string parameters to the 'Back to form' link.
  unset($query['webform_id'], $query['ajax_form'], $query['_wrapper_format'], $query['token'], $query['page']);
  $options = $query ? [
    'query' => $query,
  ] : [];

  // Set back_url.
  if ($source_entity && $source_entity
    ->hasLinkTemplate('canonical')) {
    $source_entity = \Drupal::service('entity.repository')
      ->getTranslationFromContext($source_entity);
    $variables['back_url'] = $source_entity
      ->toUrl('canonical', $options)
      ->toString();
  }
  elseif ($webform_submission) {
    $source_url = $webform_submission
      ->getSourceUrl();
    $query = $source_url
      ->getOption('query');
    unset($query['webform_id'], $query['ajax_form'], $query['_wrapper_format'], $query['token'], $query['page']);
    $source_url
      ->setOption('query', $query);
    $variables['back_url'] = $source_url
      ->toString();
  }
  else {
    $variables['back_url'] = $webform
      ->toUrl('canonical', $options)
      ->toString();
  }
  $webform
    ->invokeHandlers('preprocessConfirmation', $variables);
}