You are here

public function Select::prepare in Webform 8.5

Same name and namespace in other branches
  1. 6.x src/Plugin/WebformElement/Select.php \Drupal\webform\Plugin\WebformElement\Select::prepare()

Prepare an element to be rendered within a webform.

Parameters

array $element: An element.

\Drupal\webform\WebformSubmissionInterface $webform_submission: A webform submission. Webform submission is optional since it is not used by composite sub elements.

Overrides OptionsBase::prepare

See also

\Drupal\webform\Element\WebformCompositeBase::processWebformComposite

1 call to Select::prepare()
WebformOptionsCustom::prepare in modules/webform_options_custom/src/Plugin/WebformElement/WebformOptionsCustom.php
Prepare an element to be rendered within a webform.
1 method overrides Select::prepare()
WebformOptionsCustom::prepare in modules/webform_options_custom/src/Plugin/WebformElement/WebformOptionsCustom.php
Prepare an element to be rendered within a webform.

File

src/Plugin/WebformElement/Select.php, line 55

Class

Select
Provides a 'select' element.

Namespace

Drupal\webform\Plugin\WebformElement

Code

public function prepare(array &$element, WebformSubmissionInterface $webform_submission = NULL) {
  $config = $this->configFactory
    ->get('webform.settings');

  // Always include empty option.
  // Note: #multiple select menu does support empty options.
  // @see \Drupal\Core\Render\Element\Select::processSelect
  if (!isset($element['#empty_option']) && empty($element['#multiple'])) {
    $required = isset($element['#states']['required']) ? TRUE : !empty($element['#required']);
    $empty_option = $required ? $config
      ->get('element.default_empty_option_required') ?: $this
      ->t('- Select -') : ($config
      ->get('element.default_empty_option_optional') ?: $this
      ->t('- None -'));
    if ($config
      ->get('element.default_empty_option')) {
      $element['#empty_option'] = $empty_option;
    }
    elseif ($required && !isset($element['#default_value']) || isset($element['#empty_value'])) {
      $element['#empty_option'] = $empty_option;
    }
  }

  // If select2, choices, or chosen is not available,
  // see if we can use the alternative.
  $select2_exists = $this->librariesManager
    ->isIncluded('jquery.select2');
  $choices_exists = $this->librariesManager
    ->isIncluded('choices');
  $chosen_exists = $this->librariesManager
    ->isIncluded('jquery.chosen');
  $default_select = $select2_exists ? '#select2' : ($choices_exists ? '#choices' : ($chosen_exists ? '#chosen' : NULL));
  if (isset($element['#select2']) && !$select2_exists) {
    $element['#' . $default_select] = TRUE;
  }
  elseif (isset($element['#choices']) && !$choices_exists) {
    $element['#' . $default_select] = TRUE;
  }
  elseif (isset($element['#chosen']) && !$chosen_exists) {
    $element['#' . $default_select] = TRUE;
  }

  // Enhance select element using select2, chosen, or choices.
  if (isset($element['#select2']) && $select2_exists) {
    $element['#attached']['library'][] = 'webform/webform.element.select2';
    $element['#attributes']['class'][] = 'js-webform-select2';
    $element['#attributes']['class'][] = 'webform-select2';
  }
  elseif (isset($element['#choices']) && $choices_exists) {
    $element['#attached']['library'][] = 'webform/webform.element.choices';
    $element['#attributes']['class'][] = 'js-webform-choices';
    $element['#attributes']['class'][] = 'webform-choices';
  }
  elseif (isset($element['#chosen']) && $chosen_exists) {
    $element['#attached']['library'][] = 'webform/webform.element.chosen';
    $element['#attributes']['class'][] = 'js-webform-chosen';
    $element['#attributes']['class'][] = 'webform-chosen';
  }

  // Set placeholder as data attributes for select2, choices or chosen.
  if (!empty($element['#placeholder'])) {
    $element['#attributes']['data-placeholder'] = $element['#placeholder'];
  }

  // Set limit as data attributes for select2, choices or chosen.
  if (isset($element['#multiple']) && $element['#multiple'] > 1) {
    $element['#attributes']['data-limit'] = $element['#multiple'];
  }

  // Attach library which allows options to be disabled via JavaScript.
  $element['#attached']['library'][] = 'webform/webform.element.select';
  parent::prepare($element, $webform_submission);
}