You are here

public function SettingsWebformHandler::buildConfigurationForm in Webform 8.5

Same name and namespace in other branches
  1. 6.x src/Plugin/WebformHandler/SettingsWebformHandler.php \Drupal\webform\Plugin\WebformHandler\SettingsWebformHandler::buildConfigurationForm()

Form constructor.

Plugin forms are embedded in other forms. In order to know where the plugin form is located in the parent form, #parents and #array_parents must be known, but these are not available during the initial build phase. In order to have these properties available when building the plugin form's elements, let this method return a form element that has a #process callback and build the rest of the form in the callback. By the time the callback is executed, the element's #parents and #array_parents properties will have been set by the form API. For more documentation on #parents and #array_parents, see \Drupal\Core\Render\Element\FormElement.

Parameters

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

\Drupal\Core\Form\FormStateInterface $form_state: The current state of the form. Calling code should pass on a subform state created through \Drupal\Core\Form\SubformState::createForSubform().

Return value

array The form structure.

Overrides WebformHandlerBase::buildConfigurationForm

File

src/Plugin/WebformHandler/SettingsWebformHandler.php, line 122

Class

SettingsWebformHandler
Webform submission settings handler.

Namespace

Drupal\webform\Plugin\WebformHandler

Code

public function buildConfigurationForm(array $form, FormStateInterface $form_state) {

  // Preview settings.
  $form['preview_settings'] = [
    '#type' => 'details',
    '#title' => $this
      ->t('Preview settings'),
    '#open' => TRUE,
    '#access' => !empty($this->configuration['preview_title']) || !empty($this->configuration['preview_message']) || $this
      ->getWebform()
      ->hasPreview(),
  ];
  $form['preview_settings']['preview_title'] = [
    '#type' => 'textfield',
    '#title' => $this
      ->t('Preview page title'),
    '#description' => $this
      ->t('The title displayed on the preview page.'),
    '#default_value' => $this->configuration['preview_title'],
  ];
  $form['preview_settings']['preview_message'] = [
    '#type' => 'webform_html_editor',
    '#title' => $this
      ->t('Preview message'),
    '#description' => $this
      ->t('A message to be displayed on the preview page.'),
    '#default_value' => $this->configuration['preview_message'],
  ];
  $form['preview_settings']['token_tree_link'] = $this
    ->buildTokenTreeElement();

  // Confirmation settings.
  $confirmation_type = $this
    ->getWebform()
    ->getSetting('confirmation_type');
  $has_confirmation_url = in_array($confirmation_type, [
    WebformInterface::CONFIRMATION_URL,
    WebformInterface::CONFIRMATION_URL_MESSAGE,
  ]);
  $has_confirmation_title = in_array($confirmation_type, [
    WebformInterface::CONFIRMATION_PAGE,
    WebformInterface::CONFIRMATION_MODAL,
  ]);
  $has_confirmation_message = !in_array($confirmation_type, [
    WebformInterface::CONFIRMATION_URL,
  ]);
  $form['confirmation_settings'] = [
    '#type' => 'details',
    '#title' => $this
      ->t('Confirmation settings'),
    '#open' => TRUE,
  ];
  $form['confirmation_settings']['confirmation_url'] = [
    '#type' => 'textfield',
    '#title' => $this
      ->t('Confirmation URL'),
    '#description' => $this
      ->t('URL to redirect the user to upon successful submission.'),
    '#default_value' => $this->configuration['confirmation_url'],
    '#access' => !empty($this->configuration['confirmation_url']) || $has_confirmation_url,
    '#maxlength' => NULL,
  ];
  $form['confirmation_settings']['confirmation_title'] = [
    '#type' => 'textfield',
    '#title' => $this
      ->t('Confirmation title'),
    '#description' => $this
      ->t('Page title to be shown upon successful submission.'),
    '#default_value' => $this->configuration['confirmation_title'],
    '#access' => !empty($this->configuration['confirmation_title']) || $has_confirmation_title,
  ];
  $form['confirmation_settings']['confirmation_message'] = [
    '#type' => 'webform_html_editor',
    '#title' => $this
      ->t('Confirmation message'),
    '#description' => $this
      ->t('Message to be shown upon successful submission.'),
    '#default_value' => $this->configuration['confirmation_message'],
    '#access' => !empty($this->configuration['confirmation_message']) || $has_confirmation_message,
  ];
  $form['confirmation_settings']['token_tree_link'] = $this
    ->buildTokenTreeElement();

  // Custom settings.
  $custom_settings = $this->configuration;
  unset($custom_settings['debug']);
  $custom_settings = array_diff_key($custom_settings, $this
    ->defaultConfiguration());
  $form['custom_settings'] = [
    '#type' => 'details',
    '#title' => $this
      ->t('Custom settings'),
    '#open' => TRUE,
  ];
  $form['custom_settings']['custom'] = [
    '#type' => 'webform_codemirror',
    '#mode' => 'yaml',
    '#title' => $this
      ->t('Custom settings (YAML)'),
    '#description' => $this
      ->t('Enter the setting name and value as YAML.'),
    '#default_value' => $custom_settings,
    // Must set #parents because custom is not a configuration value.
    // @see \Drupal\webform\Plugin\WebformHandler\SettingsWebformHandler::submitConfigurationForm
    '#parents' => [
      'settings',
      'custom',
    ],
  ];

  // Custom settings definitions.
  $form['custom_settings']['definitions'] = [
    '#type' => 'details',
    '#title' => $this
      ->t('Available custom settings'),
  ];
  $rows = [];
  $webform_config_settings = $this
    ->getSettingsDefinitions();
  foreach ($webform_config_settings as $name => $webform_config_setting) {
    $rows[] = [
      'name' => [
        'data' => [
          '#markup' => '<b>' . $name . '</b>',
        ],
      ],
      'label' => $webform_config_setting['label'],
      'type' => $webform_config_setting['type'],
    ];
  }
  $form['custom_settings']['definitions']['warning'] = [
    '#type' => 'webform_message',
    '#message_type' => 'warning',
    '#message_message' => $this
      ->t('All of the below webform settings can be overridden but overriding certain settings can trigger unexpected results.'),
  ];
  $form['custom_settings']['definitions']['table'] = [
    '#type' => 'table',
    '#header' => [
      'name' => $this
        ->t('Name'),
      'label' => $this
        ->t('Label'),
      'type' => $this
        ->t('Type'),
    ],
    '#rows' => $rows,
  ];

  // Development.
  $form['development'] = [
    '#type' => 'details',
    '#title' => $this
      ->t('Development settings'),
  ];
  $form['development']['debug'] = [
    '#type' => 'checkbox',
    '#title' => $this
      ->t('Enable debugging'),
    '#description' => $this
      ->t('If checked, settings will be displayed onscreen to all users.'),
    '#return_value' => TRUE,
    '#default_value' => $this->configuration['debug'],
  ];
  $this
    ->elementTokenValidate($form);
  return $this
    ->setSettingsParents($form);
}