You are here

public function ShsTermSelect::form in Webform Simple Hierarchical Select 8

Overrides WebformTermReferenceTrait::form

File

src/Plugin/WebformElement/ShsTermSelect.php, line 63

Class

ShsTermSelect
Provides a 'webform_shs_term_select' Webform element.

Namespace

Drupal\webform_shs\Plugin\WebformElement

Code

public function form(array $form, FormStateInterface $form_state) {
  $form = parent::form($form, $form_state);
  $element_properties = $form_state
    ->get('element_properties');
  $form['term_reference'] = [
    '#type' => 'fieldset',
    '#title' => t('Term reference settings'),
    '#weight' => -40,
  ];
  $form['term_reference']['vocabulary'] = [
    '#type' => 'webform_entity_select',
    '#title' => $this
      ->t('Vocabulary'),
    '#target_type' => 'taxonomy_vocabulary',
    '#selection_handler' => 'default:taxonomy_vocabulary',
  ];
  $form['term_reference']['force_deepest'] = [
    '#type' => 'checkbox',
    '#title' => t('Force selection of deepest level'),
    '#default_value' => $element_properties['force_deepest'] ?? FALSE,
    '#description' => t('Force users to select terms from the deepest level.'),
  ];
  $form['term_reference']['force_deepest_error'] = [
    '#type' => 'textfield',
    '#title' => t('Custom force deepest error message'),
    '#default_value' => $element_properties['force_deepest_error'] ?? FALSE,
    '#description' => t('If set, this message will be used when a user does not choose the deepest option, instead of the default "You need to select a term from the deepest level in field X." message.'),
    '#states' => [
      'visible' => [
        ':input[name="properties[force_deepest]"]' => [
          'checked' => TRUE,
        ],
      ],
    ],
  ];
  $form['term_reference']['cache_options'] = [
    '#type' => 'checkbox',
    '#title' => t('Cache terms'),
    '#default_value' => $element_properties['cache_options'] ?? FALSE,
    '#description' => t('Speeds up the loading time for Vocabularies containing many Taxonomy Terms.'),
  ];
  $form['term_reference']['addNewLabel'] = [
    '#type' => 'textfield',
    '#title' => t('The label of "Add New Item" button'),
    '#default_value' => $element_properties['addNewLabel'] ?? '',
  ];
  $form['term_reference']['depth_labels'] = [
    '#type' => 'fieldset',
    '#title' => t('Depth Labels'),
    '#description' => t('Customize the labels that will appear in the form element for each level of depth. Fields can be left blank for the defaults.'),
    '#access' => TRUE,
    '#tree' => TRUE,
    '#prefix' => '<div id="element-depth-labels">',
    '#suffix' => '</div>',
  ];
  $deltas = $form_state
    ->get('depth_labels_total_items') ?: count($element_properties['depth_labels']) + 1;
  $form_state
    ->set('depth_labels_total_items', $deltas);
  foreach (range(1, $deltas) as $delta) {
    $form['term_reference']['depth_labels'][$delta] = [
      '#access' => TRUE,
      '#title' => $this
        ->t('Level @level', [
        '@level' => $delta,
      ]),
      '#type' => 'textfield',
      '#default_value' => isset($element_properties['depth_labels'][$delta - 1]) ? $element_properties['depth_labels'][$delta - 1] : '',
    ];
  }
  $form['term_reference']['depth_labels']['add'] = [
    '#type' => 'submit',
    '#value' => t('Add Label'),
    '#validate' => [],
    '#submit' => [
      [
        static::class,
        'addDepthLevelSubmit',
      ],
    ],
    '#access' => TRUE,
    '#ajax' => [
      'callback' => [
        static::class,
        'addDepthLevelAjax',
      ],
      'wrapper' => 'element-depth-labels',
    ],
  ];
  return $form;
}