You are here

function template_preprocess_webform_submission_navigation in Webform 6.x

Same name and namespace in other branches
  1. 8.5 includes/webform.theme.template.inc \template_preprocess_webform_submission_navigation()
  2. 6.3 includes/webform.submissions.inc \template_preprocess_webform_submission_navigation()
  3. 7.4 includes/webform.submissions.inc \template_preprocess_webform_submission_navigation()
  4. 7.3 includes/webform.submissions.inc \template_preprocess_webform_submission_navigation()

Prepares variables for webform submission navigation templates.

Default template: webform-submission-navigation.html.twig.

Parameters

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

  • webform_submission: A webform submission.
  • rel: Webform submission link template. (canonical, edit-form, resend-form, html, text, or yaml).

File

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

Code

function template_preprocess_webform_submission_navigation(array &$variables) {

  /** @var \Drupal\webform\WebformRequestInterface $request_handler */
  $request_handler = \Drupal::service('webform.request');

  /** @var \Drupal\webform\WebformSubmissionStorageInterface $webform_submission_storage */
  $webform_submission_storage = \Drupal::entityTypeManager()
    ->getStorage('webform_submission');

  /** @var \Drupal\webform\WebformSubmissionInterface $webform_submission */
  $webform_submission = $variables['webform_submission'];
  $webform = $webform_submission
    ->getWebform();

  // Webform id and title for context.
  $variables['webform_id'] = $webform
    ->id();
  $variables['webform_title'] = $webform
    ->label();

  // Get the route name, parameters, and source entity for the current page.
  // This ensures that the user stays within their current context as they are
  // paging through submission.
  $route_name = \Drupal::routeMatch()
    ->getRouteName();
  $route_parameters = \Drupal::routeMatch()
    ->getRawParameters()
    ->all();
  $source_entity = $request_handler
    ->getCurrentSourceEntity('webform_submission');
  if (strpos(\Drupal::routeMatch()
    ->getRouteName(), 'webform.user.submission') !== FALSE) {
    $account = \Drupal::currentUser();
    $options = [
      'in_draft' => FALSE,
    ];
  }
  else {
    $account = NULL;
    $options = [
      'in_draft' => NULL,
    ];
  }
  if ($previous_submission = $webform_submission_storage
    ->getPreviousSubmission($webform_submission, $source_entity, $account, $options)) {
    $variables['prev_url'] = Url::fromRoute($route_name, [
      'webform_submission' => $previous_submission
        ->id(),
    ] + $route_parameters)
      ->toString();
  }
  if ($next_submission = $webform_submission_storage
    ->getNextSubmission($webform_submission, $source_entity, $account, $options)) {
    $variables['next_url'] = Url::fromRoute($route_name, [
      'webform_submission' => $next_submission
        ->id(),
    ] + $route_parameters)
      ->toString();
  }
  $variables['#attached']['library'][] = 'webform/webform.navigation';

  // Never cache navigation because previous and next submission will change
  // as submissions are added and deleted.
  $variables['#cache'] = [
    'max-age' => 0,
  ];
}