You are here

public function OptionsBase::form in YAML Form 8

Gets the actual configuration form 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 form without any default values..

Overrides YamlFormElementBase::form

1 call to OptionsBase::form()
Select::form in src/Plugin/YamlFormElement/Select.php
Gets the actual configuration form array to be built.
1 method overrides OptionsBase::form()
Select::form in src/Plugin/YamlFormElement/Select.php
Gets the actual configuration form array to be built.

File

src/Plugin/YamlFormElement/OptionsBase.php, line 446

Class

OptionsBase
Provides a base 'options' element.

Namespace

Drupal\yamlform\Plugin\YamlFormElement

Code

public function form(array $form, FormStateInterface $form_state) {
  $form = parent::form($form, $form_state);
  $form['general']['default_value']['#description'] = $this
    ->t('The default value of the field identified by its key.');
  $form['general']['default_value']['#description'] .= ' ' . $this
    ->t('For multiple options, use commas to separate multiple defaults.');

  // Issue #2836374: Wrapper attributes are not supported by composite
  // elements, this includes radios, checkboxes, and buttons.
  if (preg_match('/(radios|checkboxes|buttons)/', $this
    ->getPluginId())) {
    $t_args = [
      '@name' => Unicode::strtolower($this
        ->getPluginLabel()),
      ':href' => 'https://www.drupal.org/node/2836364',
    ];
    $form['element_attributes']['#description'] = $this
      ->t('Please note: That the below custom element attributes will also be applied to the @name fieldset wrapper. (<a href=":href">Issue #2836374</a>)', $t_args);
  }

  // Options.
  $form['options'] = [
    '#type' => 'fieldset',
    '#title' => $this
      ->t('Element options'),
    '#open' => TRUE,
  ];
  $form['options']['options'] = [
    '#type' => 'yamlform_element_options',
    '#title' => $this
      ->t('Options'),
    '#required' => TRUE,
  ];
  $form['options']['options_display'] = [
    '#title' => $this
      ->t('Options display'),
    '#type' => 'select',
    '#options' => [
      'one_column' => $this
        ->t('One column'),
      'two_columns' => $this
        ->t('Two columns'),
      'three_columns' => $this
        ->t('Three columns'),
      'side_by_side' => $this
        ->t('Side by side'),
    ],
  ];
  $form['options']['empty_option'] = [
    '#type' => 'textfield',
    '#title' => $this
      ->t('Empty option label'),
    '#description' => $this
      ->t('The label to show for the initial option denoting no selection in a select element.'),
  ];
  $form['options']['empty_value'] = [
    '#type' => 'textfield',
    '#title' => $this
      ->t('Empty option value'),
    '#description' => $this
      ->t('The value for the initial option denoting no selection in a select element, which is used to determine whether the user submitted a value or not.'),
  ];
  $form['options']['multiple'] = [
    '#title' => $this
      ->t('Multiple'),
    '#type' => 'checkbox',
    '#return_value' => TRUE,
    '#description' => $this
      ->t('Check this option if the user should be allowed to choose multiple values.'),
  ];
  $form['options']['options_randomize'] = [
    '#type' => 'checkbox',
    '#title' => $this
      ->t('Randomize options'),
    '#description' => $this
      ->t('Randomizes the order of the options when they are displayed in the form.'),
    '#return_value' => TRUE,
  ];

  // Other.
  $states_textfield_or_number = [
    'visible' => [
      [
        ':input[name="properties[other__type]"]' => [
          'value' => 'textfield',
        ],
      ],
      'or',
      [
        ':input[name="properties[other__type]"]' => [
          'value' => 'number',
        ],
      ],
    ],
  ];
  $states_textarea = [
    'visible' => [
      ':input[name="properties[other__type]"]' => [
        'value' => 'textarea',
      ],
    ],
  ];
  $states_number = [
    'visible' => [
      ':input[name="properties[other__type]"]' => [
        'value' => 'number',
      ],
    ],
  ];
  $form['options_other'] = [
    '#type' => 'fieldset',
    '#title' => $this
      ->t('Other option settings'),
  ];
  $form['options_other']['other__type'] = [
    '#type' => 'select',
    '#title' => $this
      ->t('Other type'),
    '#options' => [
      'textfield' => $this
        ->t('Text field'),
      'textarea' => $this
        ->t('Textarea'),
      'number' => $this
        ->t('Number'),
    ],
  ];
  $form['options_other']['other__option_label'] = [
    '#type' => 'textfield',
    '#title' => $this
      ->t('Other option label'),
  ];
  $form['options_other']['other__title'] = [
    '#type' => 'textfield',
    '#title' => $this
      ->t('Other title'),
  ];
  $form['options_other']['other__placeholder'] = [
    '#type' => 'textfield',
    '#title' => $this
      ->t('Other placeholder'),
  ];
  $form['options_other']['other__description'] = [
    '#type' => 'textfield',
    '#title' => $this
      ->t('Other description'),
  ];
  $form['options_other']['other__size'] = [
    '#type' => 'number',
    '#title' => $this
      ->t('Other size'),
    '#description' => $this
      ->t('Leaving blank will use the default size.'),
    '#min' => 1,
    '#size' => 4,
    '#states' => $states_textfield_or_number,
  ];
  $form['options_other']['other__maxlength'] = [
    '#type' => 'number',
    '#title' => $this
      ->t('Other maxlength'),
    '#description' => $this
      ->t('Leaving blank will use the default maxlength.'),
    '#min' => 1,
    '#size' => 4,
    '#states' => $states_textfield_or_number,
  ];
  $form['options_other']['other__field_prefix'] = [
    '#type' => 'textfield',
    '#title' => $this
      ->t('Other field prefix'),
    '#description' => $this
      ->t('Text or code that is placed directly in front of the input. This can be used to prefix an input with a constant string. Examples: $, #, -.'),
    '#size' => 10,
    '#states' => $states_textfield_or_number,
  ];
  $form['options_other']['other__field_suffix'] = [
    '#type' => 'textfield',
    '#title' => $this
      ->t('Other field suffix'),
    '#description' => $this
      ->t('Text or code that is placed directly after the input. This can be used to add a unit to an input. Examples: lb, kg, %.'),
    '#size' => 10,
    '#states' => $states_textfield_or_number,
  ];
  $form['options_other']['other__rows'] = [
    '#type' => 'number',
    '#title' => $this
      ->t('Other rows'),
    '#description' => $this
      ->t('Leaving blank will use the default rows.'),
    '#min' => 1,
    '#size' => 4,
    '#states' => $states_textarea,
  ];
  $form['options_other']['other__min'] = [
    '#type' => 'number',
    '#title' => $this
      ->t('Other min'),
    '#description' => $this
      ->t('Specifies the minimum value.'),
    '#step' => 'any',
    '#size' => 4,
    '#states' => $states_number,
  ];
  $form['options_other']['other__max'] = [
    '#type' => 'number',
    '#title' => $this
      ->t('Other max'),
    '#description' => $this
      ->t('Specifies the maximum value.'),
    '#step' => 'any',
    '#size' => 4,
    '#states' => $states_number,
  ];
  $form['options_other']['other__step'] = [
    '#type' => 'number',
    '#title' => $this
      ->t('Other steps'),
    '#description' => $this
      ->t('Specifies the legal number intervals. Leave blank to support any number interval.'),
    '#step' => 'any',
    '#size' => 4,
    '#states' => $states_number,
  ];
  return $form;
}