You are here

public function WebformAdminConfigVariantsForm::buildForm in Webform 8.5

Same name and namespace in other branches
  1. 6.x src/Form/AdminConfig/WebformAdminConfigVariantsForm.php \Drupal\webform\Form\AdminConfig\WebformAdminConfigVariantsForm::buildForm()

Form constructor.

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 The form structure.

Overrides ConfigFormBase::buildForm

File

src/Form/AdminConfig/WebformAdminConfigVariantsForm.php, line 57

Class

WebformAdminConfigVariantsForm
Configure webform admin settings for variants.

Namespace

Drupal\webform\Form\AdminConfig

Code

public function buildForm(array $form, FormStateInterface $form_state) {
  $config = $this
    ->config('webform.settings');

  // Display warning about needing 'Edit webform variants' permission.
  $t_args = [
    '@href' => Url::fromRoute('user.admin_permissions', [], [
      'fragment' => 'module-webform',
    ])
      ->toString(),
  ];
  if (!$this
    ->currentUser()
    ->hasPermission('edit webform variants')) {
    $form['message'] = [
      '#type' => 'webform_message',
      '#message_message' => $this
        ->t('You need to be assigned <a href="@href">Edit webform variants</a> permission to be able create and manage variants.', $t_args),
      '#message_type' => 'warning',
    ];
  }
  else {
    $form['message'] = [
      '#type' => 'webform_message',
      '#message_message' => $this
        ->t('Users need to be assigned <a href="@href">Edit webform variants</a> permission to be able create and manage variants.', $t_args),
      '#message_type' => 'info',
      '#message_close' => TRUE,
      '#message_storage' => WebformMessage::STORAGE_SESSION,
    ];
  }

  // Variant: Types.
  $form['variant_types'] = [
    '#type' => 'details',
    '#title' => $this
      ->t('Variants'),
    '#description' => $this
      ->t('Select available variants'),
    '#open' => TRUE,
    '#weight' => 10,
  ];
  $form['variant_types']['excluded_variants'] = $this
    ->buildExcludedPlugins($this->variantManager, $config
    ->get('variant.excluded_variants') ?: []);
  $excluded_variant_checkboxes = [];
  foreach ($form['variant_types']['excluded_variants']['#options'] as $variant_id => $option) {
    if ($excluded_variant_checkboxes) {
      $excluded_variant_checkboxes[] = 'or';
    }
    $excluded_variant_checkboxes[] = [
      ':input[name="excluded_variants[' . $variant_id . ']"]' => [
        'checked' => FALSE,
      ],
    ];
  }
  $form['variant_types']['excluded_variants_message'] = [
    '#type' => 'webform_message',
    '#message_message' => $this
      ->t('All excluded variants must be manually removed from existing webforms.'),
    '#message_type' => 'warning',
    '#states' => [
      'visible' => $excluded_variant_checkboxes,
    ],
  ];
  return parent::buildForm($form, $form_state);
}