You are here

public function Checkboxes::prepare in Webform 6.x

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

File

src/Plugin/WebformElement/Checkboxes.php, line 93

Class

Checkboxes
Provides a 'checkboxes' element.

Namespace

Drupal\webform\Plugin\WebformElement

Code

public function prepare(array &$element, WebformSubmissionInterface $webform_submission = NULL) {
  parent::prepare($element, $webform_submission);

  // Set 'data-options-all' and 'data-options-all' attribute.
  $option_types = [
    'options_all',
    'options_none',
  ];
  foreach ($option_types as $option_type) {
    if (!empty($element['#' . $option_type])) {

      // If options are randomized, make sure the 'all' and 'none' checkboxes
      // are always last.
      if (!empty($element['#options_randomize'])) {
        unset($element['#options'][$element['#' . $option_type . '_value']]);
        $element['#options'][$element['#' . $option_type . '_value']] = $element['#' . $option_type . '_text'];
      }
      $element['#wrapper_attributes']['data-' . str_replace('_', '-', $option_type)] = $element['#' . $option_type . '_value'];
    }
  }
  $element['#attached']['library'][] = 'webform/webform.element.checkboxes';
  if (!empty($element['#options_all']) || !empty($element['#options_none'])) {
    $element['#element_validate'][] = [
      get_class($this),
      'validateCheckAllOrNone',
    ];
  }
}