You are here

public function WebformActions::form in Webform 6.x

Same name and namespace in other branches
  1. 8.5 src/Plugin/WebformElement/WebformActions.php \Drupal\webform\Plugin\WebformElement\WebformActions::form()

Gets the actual configuration webform array to be built.

Parameters

array $form: An associative array containing the structure of the form.

\Drupal\Core\Form\FormStateInterface $form_state: The current state of the form.

Return value

array An associative array contain the element's configuration webform without any default values.

Overrides ContainerBase::form

File

src/Plugin/WebformElement/WebformActions.php, line 107

Class

WebformActions
Provides a 'webform_actions' element.

Namespace

Drupal\webform\Plugin\WebformElement

Code

public function form(array $form, FormStateInterface $form_state) {
  $form = parent::form($form, $form_state);

  /** @var \Drupal\webform\WebformInterface $webform */
  $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' => [
        // Make sure default value is never cleared by #states API.
        // @see js/webform.states.js
        '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;
}