You are here

public function Select::form in Webform 6.x

Same name and namespace in other branches
  1. 8.5 src/Plugin/WebformElement/Select.php \Drupal\webform\Plugin\WebformElement\Select::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 OptionsBase::form

2 calls to Select::form()
WebformImageSelect::form in modules/webform_image_select/src/Plugin/WebformElement/WebformImageSelect.php
Gets the actual configuration webform array to be built.
WebformOptionsCustom::form in modules/webform_options_custom/src/Plugin/WebformElement/WebformOptionsCustom.php
Gets the actual configuration webform array to be built.
2 methods override Select::form()
WebformImageSelect::form in modules/webform_image_select/src/Plugin/WebformElement/WebformImageSelect.php
Gets the actual configuration webform array to be built.
WebformOptionsCustom::form in modules/webform_options_custom/src/Plugin/WebformElement/WebformOptionsCustom.php
Gets the actual configuration webform array to be built.

File

src/Plugin/WebformElement/Select.php, line 130

Class

Select
Provides a 'select' element.

Namespace

Drupal\webform\Plugin\WebformElement

Code

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

  // Select2, Chosen, and/or Choices enhancements.
  // @see \Drupal\webform\Plugin\WebformElement\WebformCompositeBase::form
  $select2_exists = $this->librariesManager
    ->isIncluded('jquery.select2');
  $choices_exists = $this->librariesManager
    ->isIncluded('choices');
  $chosen_exists = $this->librariesManager
    ->isIncluded('jquery.chosen');
  $form['options']['select2'] = [
    '#type' => 'checkbox',
    '#title' => $this
      ->t('Select2'),
    '#description' => $this
      ->t('Replace select element with jQuery <a href=":href">Select2</a> select box.', [
      ':href' => 'https://select2.github.io/',
    ]),
    '#return_value' => TRUE,
    '#states' => [
      'disabled' => [
        ':input[name="properties[chosen]"]' => [
          'checked' => TRUE,
        ],
      ],
    ],
  ];
  if (!$select2_exists) {
    $form['options']['select2']['#access'] = FALSE;
  }
  $form['options']['choices'] = [
    '#type' => 'checkbox',
    '#title' => $this
      ->t('Choices'),
    '#description' => $this
      ->t('Replace select element with <a href=":href">Choice.js</a> select box.', [
      ':href' => 'https://joshuajohnson.co.uk/Choices/',
    ]),
    '#return_value' => TRUE,
    '#states' => [
      'disabled' => [
        ':input[name="properties[select2]"]' => [
          'checked' => TRUE,
        ],
      ],
    ],
  ];
  if (!$choices_exists) {
    $form['options']['choices']['#access'] = FALSE;
  }
  $form['options']['chosen'] = [
    '#type' => 'checkbox',
    '#title' => $this
      ->t('Chosen'),
    '#description' => $this
      ->t('Replace select element with jQuery <a href=":href">Chosen</a> select box.', [
      ':href' => 'https://harvesthq.github.io/chosen/',
    ]),
    '#return_value' => TRUE,
    '#states' => [
      'disabled' => [
        ':input[name="properties[select2]"]' => [
          'checked' => TRUE,
        ],
      ],
    ],
  ];
  if (!$chosen_exists) {
    $form['options']['chosen']['#access'] = FALSE;
  }
  if ($select2_exists + $chosen_exists + $choices_exists > 1) {
    $select_libraries = [];
    if ($select2_exists) {
      $select_libraries[] = $this
        ->t('Select2');
    }
    if ($choices_exists) {
      $select_libraries[] = $this
        ->t('Choices');
    }
    if ($chosen_exists) {
      $select_libraries[] = $this
        ->t('Chosen');
    }
    $t_args = [
      '@libraries' => WebformArrayHelper::toString($select_libraries),
    ];
    $form['options']['select_message'] = [
      '#type' => 'webform_message',
      '#message_type' => 'warning',
      '#message_message' => $this
        ->t('@libraries provide very similar functionality, only one should be enabled.', $t_args),
      '#access' => TRUE,
    ];
  }

  // Add states to placeholder if custom library is supported and the
  // select menu supports multiple values.
  $placeholder_states = [];
  if ($select2_exists) {
    $placeholder_states[] = [
      ':input[name="properties[select2]"]' => [
        'checked' => TRUE,
      ],
    ];
  }
  if ($chosen_exists) {
    if (isset($form['form']['placeholder']['#states']['visible'])) {
      $placeholder_states[] = 'or';
    }
    $placeholder_states[] = [
      ':input[name="properties[chosen]"]' => [
        'checked' => TRUE,
      ],
    ];
  }
  if ($choices_exists) {
    if (isset($form['form']['placeholder']['#states']['visible'])) {
      $placeholder_states[] = 'or';
    }
    $placeholder_states[] = [
      ':input[name="properties[choices]"]' => [
        'checked' => TRUE,
      ],
    ];
  }
  if ($placeholder_states) {
    $form['form']['placeholder']['#states']['visible'] = [
      [
        ':input[name="properties[multiple][container][cardinality]"]' => [
          'value' => 'number',
        ],
        ':input[name="properties[multiple][container][cardinality_number]"]' => [
          '!value' => 1,
        ],
      ],
      $placeholder_states,
    ];
  }
  else {
    $form['form']['placeholder']['#access'] = FALSE;
  }

  // Update multiple select size property.
  $form['form']['size_container']['size']['#description'] = $this
    ->t('Specifies the number of visible options.');
  $form['form']['size_container']['#states'] = [
    'visible' => [
      ':input[name="properties[multiple][container][cardinality_number]"]' => [
        '!value' => 1,
      ],
    ],
  ];
  return $form;
}