You are here

public function WebformLikert::form in Webform 6.x

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

File

src/Plugin/WebformElement/WebformLikert.php, line 407

Class

WebformLikert
Provides a 'likert' element.

Namespace

Drupal\webform\Plugin\WebformElement

Code

public function form(array $form, FormStateInterface $form_state) {
  $form = parent::form($form, $form_state);
  $form['validation']['required_container']['required_error']['#description'] = $this
    ->t('If set, this message will be used when a required likert question is empty, instead of the default "X field is required." message. To include the question title in the required message, please include the @name placeholder in the required message.');
  $form['likert'] = [
    '#type' => 'fieldset',
    '#title' => $this
      ->t('Likert settings'),
  ];
  $form['likert']['questions'] = [
    '#type' => 'webform_options',
    '#title' => $this
      ->t('Questions'),
    '#label' => $this
      ->t('question'),
    '#labels' => $this
      ->t('questions'),
    '#options_value_maxlength' => 128,
    '#options_description' => TRUE,
    '#required' => TRUE,
  ];
  $form['likert']['questions_description_display'] = [
    '#title' => $this
      ->t('Questions description display'),
    '#type' => 'select',
    '#options' => [
      'description' => $this
        ->t('Description'),
      'help' => $this
        ->t('Help text'),
    ],
  ];
  $form['likert']['questions_randomize'] = [
    '#type' => 'checkbox',
    '#title' => $this
      ->t('Randomize questions'),
    '#description' => $this
      ->t('Randomizes the order of the questions when they are displayed in the webform.'),
    '#return_value' => TRUE,
  ];
  $form['likert']['answers'] = [
    '#type' => 'webform_element_options',
    '#title' => $this
      ->t('Answers'),
    '#options_description' => TRUE,
    '#likert' => TRUE,
    '#required' => TRUE,
  ];
  $form['likert']['answers_description_display'] = [
    '#title' => $this
      ->t('Answers description display'),
    '#type' => 'select',
    '#options' => [
      'description' => $this
        ->t('Description'),
      'help' => $this
        ->t('Help text'),
    ],
  ];
  $form['likert']['na_answer'] = [
    '#type' => 'checkbox',
    '#title' => $this
      ->t('Allow N/A answer'),
    '#description' => $this
      ->t('Allowing N/A is ideal for situations where you wish to make a likert element required, but still want to allow users to opt out of certain questions.'),
    '#return_value' => TRUE,
  ];
  $form['likert']['na_answer_value'] = [
    '#type' => 'textfield',
    '#title' => $this
      ->t('N/A answer value'),
    '#description' => $this
      ->t('Value stored in the database. Leave blank to store an empty string in the database.'),
    '#states' => [
      'visible' => [
        ':input[name="properties[na_answer]"]' => [
          'checked' => TRUE,
        ],
      ],
    ],
  ];
  $form['likert']['na_answer_text'] = [
    '#type' => 'textfield',
    '#title' => $this
      ->t('N/A answer text'),
    '#description' => $this
      ->t('Text displayed on the webform.'),
    '#attributes' => [
      'data-webform-states-no-clear' => TRUE,
    ],
    '#states' => [
      'visible' => [
        ':input[name="properties[na_answer]"]' => [
          'checked' => TRUE,
        ],
      ],
      'required' => [
        ':input[name="properties[na_answer]"]' => [
          'checked' => TRUE,
        ],
      ],
    ],
  ];
  $form['likert']['sticky'] = [
    '#type' => 'checkbox',
    '#title' => $this
      ->t('Enable Drupal style "sticky" table headers (Javascript)'),
    '#description' => $this
      ->t('If checked, the answers will float at the top of the page as the user scrolls-thru the questions.'),
    '#return_value' => TRUE,
  ];
  return $form;
}