You are here

public function WebformOptionsForm::form in Webform 6.x

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

Gets the actual form array to be built.

Overrides EntityForm::form

See also

\Drupal\Core\Entity\EntityForm::processForm()

\Drupal\Core\Entity\EntityForm::afterBuild()

File

src/WebformOptionsForm.php, line 72

Class

WebformOptionsForm
Provides a form to set options.

Namespace

Drupal\webform

Code

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

  /** @var \Drupal\webform\WebformOptionsInterface $webform_options */
  $webform_options = $this->entity;

  /** @var \Drupal\webform\WebformOptionsStorageInterface $webform_options_storage */
  $webform_options_storage = $this->entityTypeManager
    ->getStorage('webform_options');
  $form['label'] = [
    '#type' => 'textfield',
    '#title' => $this
      ->t('Label'),
    '#maxlength' => 255,
    '#required' => TRUE,
    '#attributes' => $webform_options
      ->isNew() ? [
      'autofocus' => 'autofocus',
    ] : [],
    '#default_value' => $webform_options
      ->label(),
  ];
  $form['id'] = [
    '#type' => 'machine_name',
    '#machine_name' => [
      'exists' => '\\Drupal\\webform\\Entity\\WebformOptions::load',
      'label' => '<br/>' . $this
        ->t('Machine name'),
    ],
    '#maxlength' => 32,
    '#field_suffix' => $webform_options
      ->isNew() ? ' (' . $this
      ->t('Maximum @max characters', [
      '@max' => 32,
    ]) . ')' : '',
    '#required' => TRUE,
    '#disabled' => !$webform_options
      ->isNew(),
    '#default_value' => $webform_options
      ->id(),
  ];
  $form['category'] = [
    '#type' => 'webform_select_other',
    '#title' => $this
      ->t('Category'),
    '#options' => $webform_options_storage
      ->getCategories(),
    '#empty_option' => $this
      ->t('- None -'),
    '#default_value' => $webform_options
      ->get('category'),
  ];
  $form['likert'] = [
    '#type' => 'checkbox',
    '#title' => $this
      ->t('Use as likert'),
    '#description' => $this
      ->t("If checked, options will be available as answers to Likert elements. The 'Likert:' prefix will be removed from the option's label when listed as answers for a Likert elment."),
    '#default_value' => $webform_options
      ->get('likert'),
    '#return_value' => TRUE,
  ];

  // Call the isolated edit webform that can be overridden by the
  // webform_ui.module.
  $module_names = $this
    ->alterModuleNames();
  if (count($module_names) && !$form_state
    ->getUserInput()) {
    $t_args = [
      '%title' => $webform_options
        ->label(),
      '%module_names' => WebformArrayHelper::toString($module_names),
      '@module' => new PluralTranslatableMarkup(count($module_names), $this
        ->t('module'), $this
        ->t('modules')),
    ];
    if (empty($webform_options
      ->get('options'))) {
      $this
        ->messenger()
        ->addWarning($this
        ->t('The %title options are being set by the %module_names @module. Altering any of the below options will override these dynamically populated options.', $t_args));
    }
    else {
      $this
        ->messenger()
        ->addWarning($this
        ->t('The %title options have been customized. Resetting the below options will allow the %module_names @module to dynamically populate these options.', $t_args));
    }
  }
  $form = $this
    ->editForm($form, $form_state);
  return parent::form($form, $form_state);
}