You are here

function template_preprocess_webform_submission_information in Webform 6.x

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

Prepares variables for webform submission information template.

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

Parameters

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

  • webform_submission: A webform submission.

File

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

Code

function template_preprocess_webform_submission_information(array &$variables) {

  /** @var \Drupal\webform\WebformSubmissionInterface $webform_submission */
  $webform_submission = $variables['webform_submission'];
  $webform = $webform_submission
    ->getWebform();
  $variables['serial'] = $webform_submission
    ->serial();
  $variables['sid'] = $webform_submission
    ->id();
  $variables['uuid'] = $webform_submission
    ->uuid();
  $variables['is_draft'] = $webform_submission
    ->isDraft() ? t('Yes') : t('No');
  $variables['current_page'] = $webform_submission
    ->getCurrentPageTitle();
  $variables['remote_addr'] = $webform_submission
    ->getRemoteAddr();
  $variables['submitted_by'] = $webform_submission
    ->getOwner()
    ->toLink();
  $variables['webform'] = $webform
    ->toLink();
  $variables['created'] = WebformDateHelper::format($webform_submission
    ->getCreatedTime());
  $variables['completed'] = WebformDateHelper::format($webform_submission
    ->getCompletedTime());
  $variables['changed'] = WebformDateHelper::format($webform_submission
    ->getChangedTime());
  $variables['sticky'] = $webform_submission
    ->isSticky() ? t('Yes') : '';
  $variables['locked'] = $webform_submission
    ->isLocked() ? t('Yes') : '';
  $variables['notes'] = $webform_submission
    ->getNotes();

  // @see \Drupal\Core\Field\Plugin\Field\FieldFormatter\LanguageFormatter::viewValue()
  $languages = \Drupal::languageManager()
    ->getNativeLanguages();
  $langcode = $webform_submission
    ->get('langcode')->value;
  $variables['language'] = isset($languages[$langcode]) ? $languages[$langcode]
    ->getName() : $langcode;
  if ($source_url = $webform_submission
    ->getSourceUrl()) {
    $variables['uri'] = Link::fromTextAndUrl($source_url
      ->setAbsolute(FALSE)
      ->toString(), $source_url);
  }
  $token_operations = [
    'view',
    'update',
    'delete',
  ];
  foreach ($token_operations as $token_operation) {
    if ($webform
      ->getSetting('token_' . $token_operation)) {
      $token_url = $webform_submission
        ->getTokenUrl($token_operation);
      $variables['token_' . $token_operation] = Link::fromTextAndUrl($token_url
        ->setAbsolute(FALSE)
        ->toString(), $token_url);
    }
  }
  if (($source_entity = $webform_submission
    ->getSourceEntity()) && $source_entity
    ->hasLinkTemplate('canonical')) {
    $variables['submitted_to'] = $source_entity
      ->toLink();
  }
  $variables['submissions_view'] = FALSE;
  if ($webform
    ->access('submission_view_any')) {
    $variables['submissions_view'] = TRUE;
  }
  elseif ($source_entity) {
    $entity_type = $source_entity
      ->getEntityTypeId();
    if (\Drupal::currentUser()
      ->hasPermission("view webform node submissions any {$entity_type}")) {
      $variables['submissions_view'] = TRUE;
    }
    elseif (\Drupal::currentUser()
      ->hasPermission("view webform node submissions own {$entity_type}") && method_exists($source_entity, 'getOwnerId') && (int) $source_entity
      ->getOwnerId() === (int) \Drupal::currentUser()
      ->id()) {
      $variables['submissions_view'] = TRUE;
    }
  }
  if ($webform_submission
    ->access('delete')) {

    /** @var \Drupal\webform\WebformRequestInterface $request_handler */
    $request_handler = \Drupal::service('webform.request');
    $current_url = Url::fromRoute('<current>')
      ->toString();
    $source_entity_url = $webform_submission
      ->getSourceUrl()
      ->setAbsolute(FALSE)
      ->toString();
    $route_options = [];
    if ($current_url === $source_entity_url) {
      $base_route_name = 'webform.user.submission.delete';
      $route_options = [
        'query' => \Drupal::destination()
          ->getAsArray(),
      ];
    }
    elseif (strpos(\Drupal::routeMatch()
      ->getRouteName(), 'webform.user.submission') !== FALSE || !$webform
      ->access('submission_view_any')) {
      $base_route_name = 'webform.user.submission.delete';
    }
    else {
      $base_route_name = 'webform_submission.delete_form';
    }

    // Append token to delete URL.
    $token = \Drupal::request()->query
      ->get('token');
    if ($webform
      ->getSetting('token_delete') && $token === $webform_submission
      ->getToken()) {
      $route_options['query']['token'] = $token;
    }
    $url = $request_handler
      ->getUrl($webform_submission, $source_entity, $base_route_name, $route_options);
    $variables['delete'] = [
      '#type' => 'link',
      '#title' => t('Delete submission'),
      '#url' => $url,
      '#attributes' => WebformDialogHelper::getModalDialogAttributes(WebformDialogHelper::DIALOG_NARROW, [
        'button',
        'button--danger',
      ]),
    ];
    WebformDialogHelper::attachLibraries($variables['delete']);
  }
}