View source
<?php
namespace Drupal\webform\Element;
use Drupal\Component\Utility\Html;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Render\Element;
use Drupal\Core\Render\Element\Container;
use Drupal\webform\Utility\WebformDialogHelper;
use Drupal\webform\Utility\WebformElementHelper;
class WebformActions extends Container {
public static $buttons = [
'submit',
'reset',
'delete',
'draft',
'wizard_prev',
'wizard_next',
'preview_prev',
'preview_next',
];
public function getInfo() {
$class = get_class($this);
return [
'#process' => [
[
$class,
'processWebformActions',
],
[
$class,
'processContainer',
],
],
'#theme_wrappers' => [
'container',
],
];
}
public static function processWebformActions(&$element, FormStateInterface $form_state, &$complete_form) {
$form_object = $form_state
->getFormObject();
$webform_submission = $form_object
->getEntity();
$prefix = $element['#webform_key'] ? 'edit-' . $element['#webform_key'] . '-' : '';
if (WebformElementHelper::isType($complete_form['actions'], 'actions')) {
$element['#attributes']['class'][] = 'form-actions';
$element['#attributes']['class'][] = 'webform-actions';
}
$element += $complete_form['actions'];
if (isset($element['delete'])) {
$element['delete']['#url'] = clone $element['delete']['#url'];
if (!empty($element['#delete__dialog'])) {
$element['delete'] += [
'#attributes' => [],
];
$element['delete']['#attributes'] += [
'class' => [],
];
$dialog_attributes = WebformDialogHelper::getModalDialogAttributes(WebformDialogHelper::DIALOG_NARROW, $element['delete']['#attributes']['class']);
$element['delete']['#attributes'] += $dialog_attributes;
$element['delete']['#attributes']['class'] = $dialog_attributes['class'];
WebformDialogHelper::attachLibraries($element);
}
if (isset($element['#delete_hide']) && $element['#delete_hide'] === FALSE) {
$element['delete']['#access'] = $webform_submission
->access('delete');
unset($element['#delete_hide']);
}
}
$has_visible_button = FALSE;
foreach (static::$buttons as $button_name) {
if (!isset($element[$button_name])) {
continue;
}
$is_update_button = $button_name === 'submit' && !($webform_submission
->isNew() || $webform_submission
->isDraft());
$settings_name = $is_update_button ? 'update' : $button_name;
if ($prefix) {
$element[$button_name]['#id'] = Html::getUniqueId("{$prefix}{$button_name}");
}
if (!empty($element['#' . $settings_name . '_hide'])) {
$element[$button_name]['#access'] = FALSE;
}
$has_custom_label = !empty($element[$button_name]['#webform_actions_button_custom']);
if (!empty($element['#' . $settings_name . '__label']) && !$has_custom_label) {
if (isset($element[$button_name]['#type']) && $element[$button_name]['#type'] === 'link') {
$element[$button_name]['#title'] = $element['#' . $settings_name . '__label'];
}
else {
$element[$button_name]['#value'] = $element['#' . $settings_name . '__label'];
}
}
if (!empty($element['#' . $settings_name . '__name'])) {
$element[$button_name]['#name'] = $element['#' . $settings_name . '__name'];
}
if (!empty($element['#' . $settings_name . '__attributes'])) {
$element[$button_name] += [
'#attributes' => [],
];
foreach ($element['#' . $settings_name . '__attributes'] as $attribute_name => $attribute_value) {
if ($attribute_name === 'class') {
$element[$button_name]['#attributes'] += [
'class' => [],
];
$element[$button_name]['#attributes']['class'] = array_merge($element[$button_name]['#attributes']['class'], $attribute_value);
}
else {
$element[$button_name]['#attributes'][$attribute_name] = $attribute_value;
}
}
}
if (Element::isVisibleElement($element[$button_name])) {
$has_visible_button = TRUE;
}
}
if (Element::isVisibleElement($element)) {
$complete_form['actions']['#access'] = FALSE;
}
if (!$has_visible_button) {
$element['#access'] = FALSE;
}
return $element;
}
}