$help_info['content'], ]; } // Build watch video link. // If help info load the video else this is the video info. $video_info = (isset($help_info['video_id'])) ? $help_manager->getVideo($help_info['video_id']) : $help_info; if (!empty($video_info['youtube_id'])) { $classes = ['button', 'button-action', 'button--small', 'button-webform-play']; $video_display = \Drupal::config('webform.settings')->get('ui.video_display'); switch ($video_display) { case 'dialog': $variables['#attached']['library'][] = 'webform/webform.ajax'; $help['link'] = [ '#type' => 'link', '#title' => t('Watch video'), '#url' => Url::fromRoute('webform.help.video', ['id' => str_replace('_', '-', $video_info['id'])]), '#attributes' => WebformDialogHelper::getModalDialogAttributes(WebformDialogHelper::DIALOG_WIDE, $classes), '#prefix' => ' ', ]; break; case 'link': $help['link'] = [ '#type' => 'link', '#title' => t('Watch video'), '#url' => Url::fromUri('https://youtu.be/' . $video_info['youtube_id']), '#attributes' => ['class' => $classes], '#prefix' => ' ', ]; break; case 'hidden': default: break; } } $variables['help'] = $help; } /** * Prepares variables for webform templates. * * Default template: webform.html.twig. * * @param array $variables * An associative array containing: * - element: An associative array containing the properties of the element. * Properties used: #action, #method, #attributes, #webform_children. */ function template_preprocess_webform(array &$variables) { template_preprocess_form($variables); } /** * Prepares variables for webform actions templates. * * Default template: webform-actions.html.twig. * * @param array $variables * An associative array containing: * - element: An associative array containing the properties and buttons. * * @see \Drupal\webform\WebformSubmissionForm::actionsElement * @see \Drupal\webform\WebformSubmissionForm::actions */ function template_preprocess_webform_actions(array &$variables) { $element = $variables['element']; // Buttons include submit, previous, next, and draft. foreach (Element::children($element) as $key) { $variables[$key] = $element[$key]; } } /** * Prepares variables for webform confirmation templates. * * Default template: webform-confirmation.html.twig. * * @param array $variables * An associative array containing the following key: * - webform: A webform. * - webform_submission: A webform submission. * - source_entity: A webform submission source entity. */ 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()] = md5($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, '#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. // @see \Drupal\webform\WebformSubmissionForm::getCustomForm // @see Drupal.behaviors.webformConfirmationBackAjax (js/webform.ajax.js) $is_ajax = (!empty($query['ajax_form'])) ? TRUE : FALSE; if (!empty($is_ajax)) { $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); } /** * Prepares variables for webform submission navigation templates. * * Default template: webform-submission-navigation.html.twig. * * @param 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). */ 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]; } /** * Prepares variables for webform submission templates. * * Default template: webform-submission.html.twig. * * @param array $variables * An associative array containing: * - elements: An array of elements to display in view mode. * - webform_submission: The webform submissions object. * - view_mode: View mode; e.g., 'html', 'text', 'table', 'yaml', etc. */ function template_preprocess_webform_submission(array &$variables) { $variables['view_mode'] = $variables['elements']['#view_mode']; $variables['webform_submission'] = $variables['elements']['#webform_submission']; if ($variables['webform_submission'] instanceof WebformSubmissionInterface) { $variables['webform'] = $variables['webform_submission']->getWebform(); } } /** * Prepares variables for webform submission information template. * * Default template: webform-submission-information.html.twig. * * @param array $variables * An associative array containing the following key: * - webform_submission: A webform submission. */ 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);; } if ($webform->getSetting('token_update')) { $token_url = $webform_submission->getTokenUrl(); $variables['token_update'] = 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') && $source_entity->getOwnerId() == \Drupal::currentUser()->id() ) { $variables['submissions_view'] = TRUE; } } if ($webform_submission->access('delete')) { /** @var \Drupal\webform\WebformRequestInterface $request_handler */ $request_handler = \Drupal::service('webform.request'); $base_route_name = (strpos(\Drupal::routeMatch()->getRouteName(), 'webform.user.submission') !== FALSE) ? 'webform.user.submission.delete' : 'webform_submission.delete_form'; $url = $request_handler->getUrl($webform_submission, $source_entity, $base_route_name); $variables['delete'] = [ '#type' => 'link', '#title' => t('Delete submission'), '#url' => $url, '#attributes' => WebformDialogHelper::getModalDialogAttributes(WebformDialogHelper::DIALOG_NARROW, ['button', 'button--danger']), ]; WebformDialogHelper::attachLibraries($variables['delete']); } } /** * Prepares variables for webform CodeMirror template. * * Default template: webform-codemirror.html.twig. * * @param array $variables * An associative array containing the following key: * - code: The code. * - type: The type of code. */ function template_preprocess_webform_codemirror(array &$variables) { $variables['mode'] = WebformCodeMirror::getMode($variables['type']); if (is_string($variables['code'])) { // Encode all HTML entities include tags. $variables['code'] = Markup::create(htmlentities($variables['code'])); } } /** * Prepares variables for webform element base HTML template. * * Default template: webform-element-base-html.html.twig. * * @param array $variables * An associative array containing the following key: * - element: The webform element. * - value: The content for the element. * - options Associative array of options for element. * - multiline: Flag to determine if value spans multiple lines. * - email: Flag to determine if element is for an email. */ function template_preprocess_webform_element_base_html(array &$variables) { $element = $variables['element']; // Set title. _template_progress_webform_set_title($variables); // Build form (item) element for HTML display. // @see form-element.html.twig // @see template_preprocess_form_element if (empty($variables['options']['email'])) { $type = $element['#type']; $variables['item'] = [ '#type' => 'item', '#title' => $variables['title'], '#name' => $element['#webform_key'], // Use wrapper attributes for the id instead of #id, // this stops the