Webform module and config export

This commit is contained in:
Rob Davies 2017-03-16 15:29:07 +00:00
parent 3e6a5cbed2
commit 0e15467384
1040 changed files with 117682 additions and 0 deletions

View file

@ -0,0 +1,75 @@
<?php
/**
* @file
* Webform libraries.
*/
/**
* Implements hook_library_info_alter().
*/
function webform_library_info_alter(&$libraries, $extension) {
// Only alter webform libraries.
if ($extension != 'webform') {
return;
}
// Add off-canvas system tray to dialog dependencies.
if (isset($libraries['webform.admin.dialog']) && (floatval(\Drupal::VERSION) >= 8.3) && \Drupal::moduleHandler()->moduleExists('outside_in')) {
$libraries['webform.admin.dialog']['dependencies'][] = 'outside_in/drupal.off_canvas';
$libraries['webform.admin.dialog']['dependencies'][] = 'outside_in/drupal.outside_in';
}
// Map /library/* paths to CDN.
// @see webform.libraries.yml.
foreach ($libraries as &$library) {
// Check CDN setting exists.
if (!isset($library['cdn'])) {
continue;
}
// Check if the CDN's source /library/* path exists.
reset($library['cdn']);
if (file_exists(DRUPAL_ROOT . key($library['cdn']))) {
continue;
}
_webform_library_info_alter_recursive($library, $library['cdn']);
}
}
/**
* Recursive through a webform library.
*
* @param array $library
* A webform library defined in webform.libraries.yml.
* @param array $cdn
* A associative array of library paths mapped to CDN URL.
*/
function _webform_library_info_alter_recursive(array &$library, array $cdn) {
foreach ($library as $key => &$value) {
// CSS and JS files and listed in associative arrays keyed via string.
if (!is_string($key) || !is_array($value)) {
continue;
}
// Ignore the CDN's associative array.
if ($key == 'cdn') {
continue;
}
// Replace the CDN sources (ie /library/*) with the CDN URL destination
// (https://cdnjs.cloudflare.com/ajax/libs/*).
foreach ($cdn as $source => $destination) {
if (strpos($key, $source) === 0) {
$uri = str_replace($source, $destination, $key);
$library[$uri] = $value;
unset($library[$key]);
break;
}
}
// Recurse downward to find nested libraries.
_webform_library_info_alter_recursive($value, $cdn);
}
}

View file

@ -0,0 +1,67 @@
<?php
/**
* @file
* Options alter hooks.
*/
use Drupal\webform\Utility\WebformOptionsHelper;
use Drupal\Core\Locale\CountryManager;
use Drupal\Core\Language\LanguageManager;
/**
* Implements hook_webform_options_WEBFORM_OPTIONS_ID_alter().
*
* @see config/install/webform.webform.example_options.yml
*/
function webform_webform_options_range_alter(array &$options, array $element = []) {
$element += [
'#min' => 1,
'#max' => 100,
'#step' => 1,
'#pad_length' => NULL,
'#pad_str' => 0,
];
$options = WebformOptionsHelper::range(
$element['#min'],
$element['#max'],
$element['#step'],
$element['#pad_length'],
$element['#pad_str']
);
}
/**
* Implements hook_webform_options_WEBFORM_OPTIONS_ID_alter().
*/
function webform_webform_options_time_zones_alter(array &$options, array $element = []) {
$options = system_time_zones();
}
/**
* Implements hook_webform_options_WEBFORM_OPTIONS_ID_alter().
*/
function webform_webform_options_country_codes_alter(array &$options, array $element = []) {
$options = CountryManager::getStandardList();
}
/**
* Implements hook_webform_options_WEBFORM_OPTIONS_ID_alter().
*/
function webform_webform_options_country_names_alter(array &$options, array $element = []) {
$countries = CountryManager::getStandardList();
$options = array_combine($countries, $countries);
}
/**
* Implements hook_webform_options_WEBFORM_OPTIONS_ID_alter().
*/
function webform_webform_options_languages_alter(array &$options, array $element = []) {
$languages = LanguageManager::getStandardLanguageList();
unset($languages['en-x-simple']);
$options = [];
foreach ($languages as $language) {
$options[$language[0]] = $language[0];
}
}

View file

@ -0,0 +1,815 @@
<?php
/**
* @file
* Preprocessors and helper functions to make theming easier.
*/
use Drupal\Core\Link;
use Drupal\Core\Render\Element;
use Drupal\Core\Url;
use Drupal\Core\Serialization\Yaml;
use Drupal\Core\Template\Attribute;
use Drupal\Component\Utility\Xss;
use Drupal\webform\Element\WebformCodeMirror;
use Drupal\webform\WebformMessageManagerInterface;
use Drupal\webform\Utility\WebformYaml;
use Drupal\webform\Utility\WebformDateHelper;
use Drupal\webform\Utility\WebformDialogHelper;
use Drupal\webform\Utility\WebformElementHelper;
/**
* Prepares variables for webform help.
*
* Default template: webform_help.html.twig.
*
* @param array $variables
* An associative array containing:
* - title: Help title.
* - content: Help content.
*/
function template_preprocess_webform_help(array &$variables) {
$help_info = $variables['info'];
$variables += $help_info;
$help = [];
if (is_array($help_info['content'])) {
$help['content'] = $help_info['content'];
}
else {
$help['content'] = [
'#markup' => $help_info['content'],
];
}
// Build watch video link.
/** @var \Drupal\webform\WebformHelpManagerInterface $help_manager */
$help_manager = \Drupal::service('webform.help_manager');
// 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':
$help['link'] = [
'#type' => 'link',
'#title' => t('Watch video'),
'#url' => Url::fromRoute('webform.help', ['id' => str_replace('_', '-', $video_info['id'])]),
'#attributes' => WebformDialogHelper::getModalDialogAttributes(1000, $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_submission: A webform submission.
*/
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->setWebform($webform);
$message_manager->setSourceEntity($source_entity);
$message_manager->setWebformSubmission($webform_submission);
$settings = $webform->getSettings();
// Set progress.
if ($webform->getPages() && $settings['wizard_complete'] && ($settings['wizard_progress_bar'] || $settings['wizard_progress_pages'] || $settings['wizard_progress_percentage'])) {
$variables['progress'] = [
'#theme' => 'webform_progress',
'#webform' => $webform,
'#current_page' => 'complete',
];
}
// Set message.
$variables['message'] = $message_manager->build(WebformMessageManagerInterface::SUBMISSION_CONFIRMATION);
// 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']);
// Apply all passed query string parameters to the 'Back to form' link.
$query = \Drupal::request()->query->all();
unset($query['webform_id']);
$options = ($query) ? ['query' => $query] : [];
// Set back_url.
if ($source_entity && $source_entity->hasLinkTemplate('canonical')) {
$variables['back_url'] = $source_entity->toUrl('canonical', $options)->toString();
}
elseif ($webform_submission) {
$variables['back_url'] = $webform_submission->getSourceUrl()->toString();
}
else {
$variables['back_url'] = $webform->toUrl('canonical', $options)->toString();
}
}
/**
* 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'];
// 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();
}
else {
$account = NULL;
}
if ($previous_submission = $webform_submission_storage->getPreviousSubmission($webform_submission, $source_entity, $account)) {
$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)) {
$variables['next_url'] = Url::fromRoute($route_name, ['webform_submission' => $next_submission->id()] + $route_parameters)->toString();
}
$variables['#attached']['library'][] = 'webform/webform.navigation';
}
/**
* 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['webform_id'] = $webform->id();
$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['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;
}
}
}
/**
* Prepares variables for webform submission HTML template.
*
* Default template: webform-submission-html.html.twig.
*
* @param array $variables
* An associative array containing the following key:
* - webform_submission: A webform submission.
*/
function template_preprocess_webform_submission_html(array &$variables) {
/** @var \Drupal\webform\WebformSubmissionInterface $webform_submission */
$webform_submission = $variables['webform_submission'];
/** @var \Drupal\webform\WebformSubmissionViewBuilderInterface $view_builder */
$view_builder = \Drupal::entityTypeManager()->getViewBuilder('webform_submission');
$webform = $webform_submission->getWebform();
$data = $webform_submission->getData();
$elements = $webform->getElementsInitialized();
$variables['data'] = $view_builder->buildElements($elements, $data);
}
/**
* Prepares variables for webform submission table template.
*
* Default template: webform-submission-table.html.twig.
*
* @param array $variables
* An associative array containing the following key:
* - webform_submission: A webform submission.
*/
function template_preprocess_webform_submission_table(array &$variables) {
/** @var \Drupal\webform\WebformSubmissionInterface $webform_submission */
$webform_submission = $variables['webform_submission'];
/** @var \Drupal\webform\WebformSubmissionViewBuilderInterface $view_builder */
$view_builder = \Drupal::entityTypeManager()->getViewBuilder('webform_submission');
$webform = $webform_submission->getWebform();
$data = $webform_submission->getData();
$elements = $webform->getElementsInitializedFlattenedAndHasValue();
$variables['table'] = $view_builder->buildTable($elements, $data);
}
/**
* Prepares variables for webform submission plain text template.
*
* Default template: webform-submission-text.html.twig.
*
* @param array $variables
* An associative array containing the following key:
* - webform_submission: A webform submission.
*/
function template_preprocess_webform_submission_text(array &$variables) {
/** @var \Drupal\webform\WebformSubmissionInterface $webform_submission */
$webform_submission = $variables['webform_submission'];
$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->getCurrentPage();
$variables['remote_addr'] = $webform_submission->getRemoteAddr();
$variables['submitted_by'] = $webform_submission->getOwner()->label();
$variables['webform'] = $webform_submission->getWebform()->label();
$variables['created'] = WebformDateHelper::format($webform_submission->getCreatedTime());
$variables['completed'] = WebformDateHelper::format($webform_submission->getCompletedTime());
$variables['changed'] = WebformDateHelper::format($webform_submission->getChangedTime());
// @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'] = $source_url->toString();
}
if ($source_entity = $webform_submission->getSourceEntity()) {
$variables['submitted_to'] = $source_entity->label();
}
/** @var \Drupal\webform\WebformSubmissionViewBuilderInterface $view_builder */
$view_builder = \Drupal::entityTypeManager()->getViewBuilder('webform_submission');
$webform = $webform_submission->getWebform();
$data = $webform_submission->getData();
$elements = $webform->getElementsInitialized();
$variables['data'] = $view_builder->buildElements($elements, $data, [], 'text');
}
/**
* Prepares variables for webform submission YAML template.
*
* Default template: webform-submission-yaml.html.twig.
*
* @param array $variables
* An associative array containing the following key:
* - webform_submission: A webform submission.
*/
function template_preprocess_webform_submission_yaml(array &$variables) {
/** @var \Drupal\webform\WebformSubmissionInterface $webform_submission */
$webform_submission = $variables['webform_submission'];
$data = $webform_submission->toArray(TRUE, TRUE);
$yaml = Yaml::encode($data);
$yaml = WebformYaml::tidy($yaml);
$variables['yaml'] = [
'#markup' => $yaml,
'#allowed_tags' => Xss::getAdminTagList(),
];;
}
/**
* 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'])) {
$variables['code'] = [
'#markup' => $variables['code'],
'#allowed_tags' => Xss::getAdminTagList(),
];
}
}
/**
* 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'];
$variables['title'] = (WebformElementHelper::isTitleDisplayed($element)) ? $element['#title'] : NULL;
}
/**
* Prepares variables for webform element base text template.
*
* Default template: webform-element-base-text.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_text(array &$variables) {
template_preprocess_webform_element_base_html($variables);
}
/**
* Prepares variables for webform container base HTML template.
*
* Default template: webform-container-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_container_base_html(array &$variables) {
$element = $variables['element'];
$variables['title'] = (isset($element['#title'])) ? $element['#title'] : NULL;
$variables['id'] = $element['#webform_id'];
}
/**
* Prepares variables for webform container base text template.
*
* Default template: webform-container-base-text.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_container_base_text(array &$variables) {
$element = $variables['element'];
$variables['title'] = (isset($element['#title'])) ? $element['#title'] : NULL;
}
/**
* Prepares variables for webform 'wizard' progress template.
*
* Default template: webform-progress.html.twig.
*
* @param array $variables
* An associative array containing the following key:
* - webform: A webform.
* - current_page: The current wizard page.
*/
function template_preprocess_webform_progress(array &$variables) {
/** @var \Drupal\webform\WebformInterface $webform */
$webform = $variables['webform'];
$current_page = $variables['current_page'];
$pages = $webform->getPages();
$page_keys = array_keys($pages);
$page_indexes = array_flip($page_keys);
$current_index = $page_indexes[$current_page];
$total = count($page_keys);
if ($webform->getSetting('wizard_progress_bar')) {
$variables['bar'] = [
'#theme' => 'webform_progress_bar',
'#webform' => $variables['webform'],
'#current_page' => $variables['current_page'],
];
}
if ($webform->getSetting('wizard_progress_pages')) {
$variables['summary'] = t('Page @start of @end', ['@start' => $current_index + 1, '@end' => $total]);
}
if ($webform->getSetting('wizard_progress_percentage')) {
$variables['percentage'] = number_format(($current_index / ($total - 1)) * 100, 0) . '%';
}
}
/**
* Prepares variables for webform 'wizard' progress bar template.
*
* Default template: webform-progress-bar.html.twig.
*
* @param array $variables
* An associative array containing the following key:
* - webform: A webform.
* - current_page: The current wizard page.
*/
function template_preprocess_webform_progress_bar(array &$variables) {
/** @var \Drupal\webform\WebformInterface $webform */
$webform = $variables['webform'];
$current_page = $variables['current_page'];
$pages = $webform->getPages();
$page_keys = array_keys($pages);
$page_indexes = array_flip($page_keys);
$current_index = $page_indexes[$current_page];
$variables['current_index'] = $current_index;
// Reset the pages variable.
$variables['progress'] = [];
foreach ($pages as $page) {
$variables['progress'][] = (isset($page['#title'])) ? $page['#title'] : '';
}
}
/******************************************************************************/
// Element templates
/******************************************************************************/
/**
* Prepares variables for Webform message templates.
*
* Default template: webform-message.html.twig.
*
* @param array $variables
* An associative array containing:
* - element: An associative array containing the properties of the element.
* Properties used: #id, #attributes, #children.
*
* @see template_preprocess_container()
*/
function template_preprocess_webform_message(array &$variables) {
$variables['has_parent'] = FALSE;
$element = $variables['element'];
// Ensure #attributes is set.
$element += ['#attributes' => []];
// Special handling for webform elements.
if (isset($element['#array_parents'])) {
// Assign an html ID.
if (!isset($element['#attributes']['id'])) {
$element['#attributes']['id'] = $element['#id'];
}
$variables['has_parent'] = TRUE;
}
$variables['message'] = $element['#message'];
$variables['attributes'] = $element['#attributes'];
if (isset($element['#closed'])) {
$variables['closed'] = $element['#closed'];
}
}
/******************************************************************************/
// Composite templates
/******************************************************************************/
/**
* Prepares variables for address composite element templates.
*
* Default template: webform-element-address.html.twig.
*
* @param array $variables
* An associative array containing:
* - element: An associative array containing the properties of the element.
* Properties used: #title, #value, #options, #description, #required,
* #attributes.
*/
function template_preprocess_webform_composite_address(array &$variables) {
$element = $variables['element'];
foreach (Element::children($element) as $key) {
if ($element[$key]['#access']) {
$variables['content'][$key] = $element[$key];
}
}
$variables['flexbox'] = (isset($element['#flexbox'])) ? $element['#flexbox'] : FALSE;
}
/**
* Prepares variables for contact composite element templates.
*
* Default template: webform-element-contact.html.twig.
*
* @param array $variables
* An associative array containing:
* - element: An associative array containing the properties of the element.
* Properties used: #title, #value, #options, #description, #required,
* #attributes.
*/
function template_preprocess_webform_composite_contact(array &$variables) {
$element = $variables['element'];
foreach (Element::children($element) as $key) {
if ($element[$key]['#access']) {
$variables['content'][$key] = $element[$key];
}
}
$variables['flexbox'] = (isset($element['#flexbox'])) ? $element['#flexbox'] : FALSE;
}
/**
* Prepares variables for creditcard composite element templates.
*
* Default template: webform-element-creditcard.html.twig.
*
* @param array $variables
* An associative array containing:
* - element: An associative array containing the properties of the element.
* Properties used: #title, #value, #options, #description, #required,
* #attributes.
*/
function template_preprocess_webform_composite_creditcard(array &$variables) {
$element = $variables['element'];
foreach (Element::children($element) as $key) {
if ($element[$key]['#access']) {
$variables['content'][$key] = $element[$key];
}
}
$variables['flexbox'] = (isset($element['#flexbox'])) ? $element['#flexbox'] : FALSE;
}
/**
* Prepares variables for location composite element templates.
*
* Default template: webform-element-location.html.twig.
*
* @param array $variables
* An associative array containing:
* - element: An associative array containing the properties of the element.
* Properties used: #title, #value, #options, #description, #required,
* #attributes.
*/
function template_preprocess_webform_composite_location(array &$variables) {
$variables['content'] = $variables['element'];
}
/**
* Prepares variables for name composite element templates.
*
* Default template: webform-element-name.html.twig.
*
* @param array $variables
* An associative array containing:
* - element: An associative array containing the properties of the element.
* Properties used: #title, #value, #options, #description, #required,
* #attributes.
*/
function template_preprocess_webform_composite_name(array &$variables) {
$element = $variables['element'];
foreach (Element::children($element) as $key) {
if ($element[$key]['#access']) {
$variables['content'][$key] = $element[$key];
}
}
$variables['flexbox'] = (isset($element['#flexbox'])) ? $element['#flexbox'] : FALSE;
}
/**
* Prepares variables for telephone composite element templates.
*
* Default template: webform-element-telephone.html.twig.
*
* @param array $variables
* An associative array containing:
* - element: An associative array containing the properties of the element.
* Properties used: #title, #value, #options, #description, #required,
* #attributes.
*/
function template_preprocess_webform_composite_telephone(array &$variables) {
$element = $variables['element'];
foreach (Element::children($element) as $key) {
if ($element[$key]['#access']) {
$variables['content'][$key] = $element[$key];
}
}
}
/******************************************************************************/
// Element templates
/******************************************************************************/
/**
* Prepares variables for a managed file element.
*
* @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.
* - file: The element's File object.
*/
function template_preprocess_webform_element_managed_file(array &$variables) {
/** @var \Drupal\file\FileInterface $file */
$file = $variables['file'];
$variables['uri'] = file_create_url($file->getFileUri());
$variables['extension'] = strtolower(pathinfo($variables['uri'], PATHINFO_EXTENSION));
$variables['type'] = \Drupal::service('file.mime_type.guesser')->guess($variables['uri']);
$variables['file_link'] = [
'#theme' => 'file_link',
'#file' => $file,
];
}
/**
* Prepares variables for an audio file element.
*
* Default template: webform-element-audio-file.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.
* - file: The element's File object.
*/
function template_preprocess_webform_element_audio_file(array &$variables) {
template_preprocess_webform_element_managed_file($variables);
}
/**
* Prepares variables for a document file element.
*
* Default template: webform-element-document-file.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.
* - file: The element's File object.
*/
function template_preprocess_webform_element_document_file(array &$variables) {
template_preprocess_webform_element_managed_file($variables);
}
/**
* Prepares variables for an image file element.
*
* Default template: webform-element-image-file.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.
* - file: The element's File object.
*/
function template_preprocess_webform_element_image_file(array &$variables) {
template_preprocess_webform_element_managed_file($variables);
}
/**
* Prepares variables for a video file element.
*
* Default template: webform-element-video-file.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.
* - file: The element's File object.
*/
function template_preprocess_webform_element_video_file(array &$variables) {
template_preprocess_webform_element_managed_file($variables);
}

View file

@ -0,0 +1,136 @@
<?php
/**
* @file
* Webform module translation hooks.
*/
use Drupal\Core\Form\FormStateInterface;
use Drupal\webform\Entity\Webform;
use Drupal\webform\Utility\WebformYaml;
use Drupal\Core\Serialization\Yaml;
use Drupal\webform\Utility\WebformElementHelper;
/**
* Implements hook_form_FORM_ID_alter().
*/
function webform_form_config_translation_add_form_alter(&$form, FormStateInterface $form_state, $is_new = TRUE) {
// Manually apply YAML editor to text field that store YAML data.
foreach ($form['config_names'] as $config_name => &$config_element) {
if ($config_name == 'webform.settings') {
_webform_form_config_translate_add_form_alter_yaml_element($config_element['test']['types']);
_webform_form_config_translate_add_form_alter_yaml_element($config_element['test']['names']);
}
elseif (strpos($config_name, 'webform.webform_options.') === 0) {
_webform_form_config_translate_add_form_alter_yaml_element($config_element['options']);
}
elseif (strpos($config_name, 'webform.webform.') === 0) {
$webform_id = str_replace('webform.webform.', '', $config_name);
$webform = Webform::load($webform_id);
/** @var \Drupal\webform\WebformTranslationManagerInterface $translation_manager */
$translation_manager = \Drupal::service('webform.translation_manager');
$translation_langcode = $form_state->get('config_translation_language')->getId();;
$source_elements = $translation_manager->getSourceElements($webform);
$translation_elements = $translation_manager->getTranslationElements($webform, $translation_langcode);
$source_value = trim(Yaml::encode($source_elements));
$translation_value = trim(Yaml::encode($translation_elements));
_webform_form_config_translate_add_form_alter_yaml_element($config_element['elements'], $source_value, $translation_value);
$config_element['elements']['translation']['#description'] = t('Please note: Custom properties will be automatically removed.');
$form_state->set('webform_config_name', $config_name);
$form_state->set('webform_source_elements', $source_elements);
$form['#validate'][] = '_webform_form_config_translate_add_form_validate';
}
}
}
/**
* Validate callback; Validates and cleanups webform elements.
*/
function _webform_form_config_translate_add_form_validate(&$form, FormStateInterface $form_state) {
$values = $form_state->getValues();
$config_name = $form_state->get('webform_config_name');
$source_elements = $form_state->get('webform_source_elements');
$submitted_translation_elements = Yaml::decode($values['translation']['config_names'][$config_name]['elements']);
$translation_elements = $source_elements;
// Remove all custom translation properties.
WebformElementHelper::merge($translation_elements, $submitted_translation_elements);
// Remove any translation property that has not been translated.
_webform_form_config_translate_add_form_filter_elements($translation_elements, $source_elements);
// Update webform value.
$values['translation']['config_names'][$config_name]['elements'] = ($translation_elements) ? Yaml::encode($translation_elements) : '';
$form_state->setValues($values);
}
/**
* Merge element properties.
*
* @param array $translation_elements
* An array of elements.
* @param array $source_elements
* An array of elements to be merged.
*/
function _webform_form_config_translate_add_form_filter_elements(array &$translation_elements, array $source_elements) {
foreach ($translation_elements as $key => &$translation_element) {
if (!isset($source_elements[$key])) {
continue;
}
$source_element = $source_elements[$key];
if ($translation_element == $source_element) {
unset($translation_elements[$key]);
}
elseif (is_array($translation_element)) {
_webform_form_config_translate_add_form_filter_elements($translation_element, $source_element);
if (empty($translation_element)) {
unset($translation_elements[$key]);
}
}
}
}
/**
* Implements hook_form_FORM_ID_alter().
*/
function webform_form_config_translation_edit_form_alter(&$form, FormStateInterface $form_state) {
webform_form_config_translation_add_form_alter($form, $form_state, FALSE);
}
/**
* Alter translated config entity property.
*
* @param array $element
* A webform element containing 'source' and 'translation'.
* @param string $source_value
* (optional) The custom config source value.
* @param string $translation_value
* (optional) The custom config translation value.
*/
function _webform_form_config_translate_add_form_alter_yaml_element(array &$element, $source_value = NULL, $translation_value = NULL) {
// Source.
$element['source']['#wrapper_attributes']['class'][] = 'webform-translation-source';
$element['source']['value'] = [
'#type' => 'webform_codemirror',
'#mode' => 'yaml',
'#value' => WebformYaml::tidy($source_value ?: trim(strip_tags($element['source']['#markup']))),
'#disabled' => TRUE,
'#attributes' => ['readonly' => TRUE],
];
unset($element['source']['#markup']);
// Translation.
$element['translation']['#type'] = 'webform_codemirror';
$element['translation']['#mode'] = 'yaml';
if ($translation_value) {
$element['translation']['#default_value'] = $translation_value;
}
$element['translation']['#default_value'] = trim($element['translation']['#default_value']);
$element['#attached']['library'][] = 'webform/webform.admin.translation';
}