You are here

public function OptionsBase::form in Webform 6.x

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

2 calls to OptionsBase::form()
Checkboxes::form in src/Plugin/WebformElement/Checkboxes.php
Gets the actual configuration webform array to be built.
Select::form in src/Plugin/WebformElement/Select.php
Gets the actual configuration webform array to be built.
2 methods override OptionsBase::form()
Checkboxes::form in src/Plugin/WebformElement/Checkboxes.php
Gets the actual configuration webform array to be built.
Select::form in src/Plugin/WebformElement/Select.php
Gets the actual configuration webform array to be built.

File

src/Plugin/WebformElement/OptionsBase.php, line 826

Class

OptionsBase
Provides a base 'options' element.

Namespace

Drupal\webform\Plugin\WebformElement

Code

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

  // 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' => mb_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' => 'webform_element_options',
    '#title' => $this
      ->t('Options'),
    '#options_description' => $this
      ->hasProperty('options_description_display'),
    '#required' => TRUE,
  ];
  $form['options']['options_display_container'] = $this
    ->getFormInlineContainer();
  $form['options']['options_display_container']['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'),
      'buttons' => $this
        ->t('Buttons - flexbox'),
      'buttons_horizontal' => $this
        ->t('Buttons - horizontal'),
      'buttons_vertical' => $this
        ->t('Buttons - vertical'),
    ],
  ];
  $form['options']['options_display_container']['options_description_display'] = [
    '#title' => $this
      ->t('Options description display'),
    '#type' => 'select',
    '#options' => [
      'description' => $this
        ->t('Description'),
      'help' => $this
        ->t('Help text'),
    ],
  ];
  $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.'),
    '#states' => [
      'visible' => [
        ':input[name="properties[multiple][container][cardinality]"]' => [
          'value' => 'number',
        ],
        ':input[name="properties[multiple][container][cardinality_number]"]' => [
          'value' => 1,
        ],
      ],
    ],
  ];
  $default_empty_option = $this->configFactory
    ->get('webform.settings')
    ->get('element.default_empty_option');
  if ($default_empty_option) {
    $default_empty_option_required = $this->configFactory
      ->get('webform.settings')
      ->get('element.default_empty_option_required') ?: $this
      ->t('- Select -');
    $form['options']['empty_option']['#description'] .= '<br />' . $this
      ->t('Required elements defaults to: %required', [
      '%required' => $default_empty_option_required,
    ]);
    $default_empty_option_optional = $this->configFactory
      ->get('webform.settings')
      ->get('element.default_empty_option_optional') ?: $this
      ->t('- None -');
    $form['options']['empty_option']['#description'] .= '<br />' . $this
      ->t('Optional elements defaults to: %optional', [
      '%optional' => $default_empty_option_optional,
    ]);
  }
  $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.'),
    '#states' => [
      'visible' => [
        ':input[name="properties[multiple][container][cardinality]"]' => [
          'value' => 'number',
        ],
        ':input[name="properties[multiple][container][cardinality_number]"]' => [
          'value' => 1,
        ],
      ],
    ],
  ];

  // Sort options (only applies to select menus).
  // @see template_preprocess_select()
  // @see webform_preprocess_select()
  $form['options']['sort_options'] = [
    '#type' => 'checkbox',
    '#title' => $this
      ->t('Sort options'),
    '#description' => $this
      ->t('Sort the options by their (translated) labels.'),
    '#return_value' => TRUE,
  ];
  $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 webform.'),
    '#return_value' => TRUE,
  ];
  if ($this
    ->hasProperty('options_randomize') && $this
    ->hasProperty('sort_options')) {
    $form['options']['options_randomize']['#states']['visible'] = [
      ':input[name="properties[sort_options]"]' => [
        'checked' => FALSE,
      ],
    ];
  }

  // Other.
  $states_textfield_or_number = [
    'visible' => [
      [
        ':input[name="properties[other__type]"]' => [
          'value' => 'textfield',
        ],
      ],
      'or',
      [
        ':input[name="properties[other__type]"]' => [
          'value' => 'number',
        ],
      ],
    ],
  ];
  $states_textbase = [
    'visible' => [
      [
        ':input[name="properties[other__type]"]' => [
          'value' => 'textfield',
        ],
      ],
      'or',
      [
        ':input[name="properties[other__type]"]' => [
          'value' => 'textarea',
        ],
      ],
    ],
  ];
  $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__field_container'] = $this
    ->getFormInlineContainer();
  $form['options_other']['other__field_container']['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_container']['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__size_container'] = $this
    ->getFormInlineContainer();
  $form['options_other']['other__size_container']['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__size_container']['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__size_container']['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__number_container'] = $this
    ->getFormInlineContainer();
  $form['options_other']['other__number_container']['other__min'] = [
    '#type' => 'number',
    '#title' => $this
      ->t('Other minimum'),
    '#description' => $this
      ->t('Specifies the minimum value.'),
    '#step' => 'any',
    '#size' => 4,
    '#states' => $states_number,
  ];
  $form['options_other']['other__number_container']['other__max'] = [
    '#type' => 'number',
    '#title' => $this
      ->t('Other maximum'),
    '#description' => $this
      ->t('Specifies the maximum value.'),
    '#step' => 'any',
    '#size' => 4,
    '#states' => $states_number,
  ];
  $form['options_other']['other__number_container']['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,
  ];
  $form['options_other']['other__textbase_container'] = [
    '#type' => 'container',
    '#states' => $states_textbase,
  ] + $this
    ->buildCounterForm('other__', 'Other count');

  // Add hide/show #format_items based on #multiple.
  if ($this
    ->supportsMultipleValues() && $this
    ->hasProperty('multiple')) {
    $form['display']['format_items']['#states'] = [
      'visible' => [
        [
          ':input[name="properties[multiple]"]' => [
            'checked' => TRUE,
          ],
        ],
      ],
    ];
  }
  $form['options_properties'] = [
    '#type' => 'details',
    '#title' => $this
      ->t('Options (custom) properties'),
    '#access' => $this
      ->hasProperty('options__properties') && $this->currentUser
      ->hasPermission('edit webform source'),
  ];
  $form['options_properties']['options__properties'] = [
    '#type' => 'webform_codemirror',
    '#mode' => 'yaml',
    '#title' => $this
      ->t('Options properties'),
    '#description' => $this
      ->t("Custom options properties must include the 'Option value' followed by option (element) properties prepended with a hash (#) character.") . "<pre>option_value:\n  '#wrapper_attributes':\n    class:\n      - disabled\n  '#disabled': true</pre>" . '<br /><br />' . $this
      ->t('These properties and callbacks are not allowed: @properties', [
      '@properties' => WebformArrayHelper::toString(WebformArrayHelper::addPrefix(WebformElementHelper::$ignoredProperties)),
    ]),
  ];
  return $form;
}