You are here

public function WebformSame::form in Webform 6.x

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

File

src/Plugin/WebformElement/WebformSame.php, line 42

Class

WebformSame
Provides a 'webform_same' element.

Namespace

Drupal\webform\Plugin\WebformElement

Code

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

  /** @var \Drupal\webform\WebformInterface $webform */
  $webform = $form_state
    ->getFormObject()
    ->getWebform();
  $form = parent::form($form, $form_state);

  // Get element's as options.
  $ignored_types = [
    'webform_same',
  ];
  $flattened_elements = $webform
    ->getElementsInitializedFlattenedAndHasValue();
  $options = [];
  foreach ($flattened_elements as $element_key => $element) {
    $element_plugin = $this->elementManager
      ->getElementInstance($element);
    if (!in_array($element_plugin
      ->getPluginId(), $ignored_types)) {
      $options[(string) $element_plugin
        ->getPluginLabel()][$element_key] = $element['#admin_title'];
    }
  }
  ksort($options);
  $form['same'] = [
    '#type' => 'fieldset',
    '#title' => $this
      ->t('Same as settings'),
    '#destination' => $this
      ->t('Please note, the source and destination element must be the same element types.'),
  ];
  $form['same']['source'] = [
    '#type' => 'select',
    '#title' => $this
      ->t('Source element'),
    '#options' => $options,
    '#required' => TRUE,
  ];
  $form['same']['destination'] = [
    '#type' => 'select',
    '#title' => $this
      ->t('Destination element'),
    '#options' => $options,
    '#required' => TRUE,
  ];
  $form['same']['destination_state'] = [
    '#type' => 'select',
    '#title' => $this
      ->t('Destination state'),
    '#description' => $this
      ->t("Determine how the destination element's state is toggled when 'Same as' is checked"),
    '#options' => [
      'visible' => $this
        ->t('Show/hide'),
      'visible-slide' => $this
        ->t('Slide in/out'),
    ],
    '#required' => TRUE,
  ];
  $form['same']['destination']['#element_validate'] = [
    [
      get_called_class(),
      'validateDestination',
    ],
  ];
  return $form;
}