You are here

public function Select::prepare in YAML Form 8

Prepare an element to be rendered within a form.

Parameters

array $element: An element.

\Drupal\yamlform\YamlFormSubmissionInterface $yamlform_submission: A form submission.

Overrides OptionsBase::prepare

File

src/Plugin/YamlFormElement/Select.php, line 36

Class

Select
Provides a 'select' element.

Namespace

Drupal\yamlform\Plugin\YamlFormElement

Code

public function prepare(array &$element, YamlFormSubmissionInterface $yamlform_submission) {
  parent::prepare($element, $yamlform_submission);
  if (empty($element['#multiple'])) {
    if (!isset($element['#empty_option'])) {
      $element['#empty_option'] = empty($element['#required']) ? $this
        ->t('- Select -') : $this
        ->t('- None -');
    }
  }
  else {
    if (!isset($element['#empty_option'])) {
      $element['#empty_option'] = empty($element['#required']) ? $this
        ->t('- None -') : NULL;
    }
    $element['#element_validate'][] = [
      get_class($this),
      'validateMultipleOptions',
    ];
  }

  // Add select2 library and classes.
  if (!empty($element['#select2'])) {
    $element['#attached']['library'][] = 'yamlform/yamlform.element.select2';
    $element['#attributes']['class'][] = 'js-yamlform-select2';
    $element['#attributes']['class'][] = 'yamlform-select2';
  }
}