View source
<?php
namespace Drupal\webform\Plugin\WebformElement;
use Drupal\Core\Form\FormStateInterface;
use Drupal\webform\Element\WebformActions as WebformActionsElement;
use Drupal\webform\WebformInterface;
use Drupal\webform\WebformSubmissionInterface;
class WebformActions extends ContainerBase {
protected function defineDefaultProperties() {
$properties = [
'title' => '',
'attributes' => [],
] + $this
->defineDefaultBaseProperties();
$buttons = array_merge(WebformActionsElement::$buttons, [
'update',
]);
foreach ($buttons as $button) {
$properties[$button . '_hide'] = FALSE;
$properties[$button . '__label'] = '';
$properties[$button . '__attributes'] = [];
}
$properties['delete_hide'] = TRUE;
$properties['delete__dialog'] = FALSE;
return $properties;
}
public function isInput(array $element) {
return FALSE;
}
public function isContainer(array $element) {
return FALSE;
}
public function isRoot() {
return FALSE;
}
protected function build($format, array &$element, WebformSubmissionInterface $webform_submission, array $options = []) {
return [];
}
public function getItemDefaultFormat() {
return NULL;
}
public function getItemFormats() {
return [];
}
public function getTestValues(array $element, WebformInterface $webform, array $options = []) {
return NULL;
}
public function preview() {
return [];
}
public function form(array $form, FormStateInterface $form_state) {
$form = parent::form($form, $form_state);
$webform = $form_state
->getFormObject()
->getWebform();
$form['actions'] = [
'#type' => 'fieldset',
'#title' => $this
->t('Buttons'),
];
$draft_enabled = $webform
->getSetting('draft') !== WebformInterface::DRAFT_NONE;
$reset_enabled = $webform
->getSetting('form_reset');
$wizard_enabled = $webform
->hasWizardPages();
$preview_enabled = $webform
->getSetting('preview') !== DRUPAL_DISABLED;
$buttons = [
'submit' => [
'title' => $this
->t('Submit'),
'label' => $this
->t('submit'),
'access' => TRUE,
],
'reset' => [
'title' => $this
->t('Reset'),
'label' => $this
->t('reset'),
'access' => $reset_enabled,
],
'draft' => [
'title' => $this
->t('Draft'),
'label' => $this
->t('draft'),
'access' => $draft_enabled,
],
'update' => [
'title' => $this
->t('Update'),
'label' => $this
->t('update'),
'description' => $this
->t('This is used after a submission has been saved and finalized to the database.'),
'access' => !$webform
->isResultsDisabled(),
],
'wizard_prev' => [
'title' => $this
->t('Wizard previous'),
'label' => $this
->t('wizard previous'),
'description' => $this
->t('This is used for the previous page button within a wizard.'),
'access' => $wizard_enabled,
],
'wizard_next' => [
'title' => $this
->t('Wizard next'),
'label' => $this
->t('wizard next'),
'description' => $this
->t('This is used for the next page button within a wizard.'),
'access' => $wizard_enabled,
],
'preview_prev' => [
'title' => $this
->t('Preview previous'),
'label' => $this
->t('preview previous'),
'description' => $this
->t('The text for the button to go backwards from the preview page.'),
'access' => $preview_enabled,
],
'preview_next' => [
'title' => $this
->t('Preview next'),
'label' => $this
->t('preview next'),
'description' => $this
->t('The text for the button that will proceed to the preview page.'),
'access' => $preview_enabled,
],
'delete' => [
'title' => $this
->t('Delete'),
'label' => $this
->t('delete'),
'description' => $this
->t('This is displayed after a draft or submission has been saved to the database. The delete button is also included within the submission information.'),
'access' => !$webform
->isResultsDisabled(),
],
];
foreach ($buttons as $name => $button) {
$t_args = [
'@title' => $button['title'],
'@label' => $button['label'],
'%label' => $button['label'],
];
$form[$name . '_settings'] = [
'#type' => 'details',
'#open' => TRUE,
'#weight' => -10,
'#title' => $this
->t('@title button', $t_args),
'#access' => $button['access'],
];
if (!empty($button['description'])) {
$form[$name . '_settings']['description'] = [
'#markup' => '<p>' . $button['description'] . '</p>',
'#access' => TRUE,
];
}
$form[$name . '_settings'][$name . '_hide'] = [
'#type' => 'checkbox',
'#title' => $this
->t('Hide @label button', $t_args),
'#return_value' => TRUE,
];
if (strpos($name, '_prev') === FALSE && !in_array($name, [
'delete',
'reset',
])) {
$form[$name . '_settings'][$name . '_hide_message'] = [
'#type' => 'webform_message',
'#access' => TRUE,
'#message_message' => $this
->t("Hiding the %label button can cause unexpected issues, please make sure to include the %label button using another 'Submit button(s)' element.", $t_args),
'#message_type' => 'warning',
'#states' => [
'visible' => [
':input[name="properties[' . $name . '_hide]"]' => [
'checked' => TRUE,
],
],
],
];
}
$form[$name . '_settings'][$name . '__label'] = [
'#type' => 'textfield',
'#title' => $this
->t('@title button label', $t_args),
'#description' => $this
->t('Defaults to: %value', [
'%value' => $this->configFactory
->get('webform.settings')
->get('settings.default_' . $name . '_button_label'),
]),
'#size' => 20,
'#attributes' => [
'data-webform-states-no-clear' => TRUE,
],
'#states' => [
'visible' => [
':input[name="properties[' . $name . '_hide]"]' => [
'checked' => FALSE,
],
],
],
];
$form[$name . '_settings'][$name . '__attributes'] = [
'#type' => 'webform_element_attributes',
'#title' => $this
->t('@title button', $t_args),
'#classes' => $this->configFactory
->get('webform.settings')
->get('settings.button_classes'),
'#states' => [
'visible' => [
':input[name="properties[' . $name . '_hide]"]' => [
'checked' => FALSE,
],
],
],
];
}
$form['delete_settings']['delete__dialog'] = [
'#type' => 'checkbox',
'#title' => $this
->t('Open delete confirmation form in a modal dialog.'),
'#states' => [
'visible' => [
':input[name="properties[delete_hide]"]' => [
'checked' => FALSE,
],
],
],
];
return $form;
}
public function buildConfigurationForm(array $form, FormStateInterface $form_state) {
$form = parent::buildConfigurationForm($form, $form_state);
$form_object = $form_state
->getFormObject();
if (!$form_object
->getWebform()
->hasActions()) {
$form['element']['title']['#default_value'] = (string) $this
->t('Submit button(s)');
}
$key = $form_object
->getKey() ?: $form_object
->getDefaultKey();
if ($key === 'actions') {
$form['element']['#access'] = FALSE;
}
return $form;
}
}