You are here

public function OptionsBase::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 YamlFormElementBase::prepare

3 calls to OptionsBase::prepare()
Checkboxes::prepare in src/Plugin/YamlFormElement/Checkboxes.php
Prepare an element to be rendered within a form.
Select::prepare in src/Plugin/YamlFormElement/Select.php
Prepare an element to be rendered within a form.
YamlFormToggles::prepare in src/Plugin/YamlFormElement/YamlFormToggles.php
Prepare an element to be rendered within a form.
3 methods override OptionsBase::prepare()
Checkboxes::prepare in src/Plugin/YamlFormElement/Checkboxes.php
Prepare an element to be rendered within a form.
Select::prepare in src/Plugin/YamlFormElement/Select.php
Prepare an element to be rendered within a form.
YamlFormToggles::prepare in src/Plugin/YamlFormElement/YamlFormToggles.php
Prepare an element to be rendered within a form.

File

src/Plugin/YamlFormElement/OptionsBase.php, line 97

Class

OptionsBase
Provides a base 'options' element.

Namespace

Drupal\yamlform\Plugin\YamlFormElement

Code

public function prepare(array &$element, YamlFormSubmissionInterface $yamlform_submission) {
  parent::prepare($element, $yamlform_submission);

  // Randomize options.
  if (isset($element['#options']) && !empty($element['#options_randomize'])) {
    shuffle($element['#options']);
  }
  $is_wrapper_fieldset = in_array($element['#type'], [
    'checkboxes',
    'radios',
  ]);
  if ($is_wrapper_fieldset) {

    // Issue #2396145: Option #description_display for form element fieldset
    // is not changing anything.
    // @see core/modules/system/templates/fieldset.html.twig
    $is_description_display = isset($element['#description_display']) ? TRUE : FALSE;
    $has_description = !empty($element['#description']) ? TRUE : FALSE;
    if ($is_description_display && $has_description) {
      switch ($element['#description_display']) {
        case 'before':
          $element += [
            '#field_prefix' => '',
          ];
          $element['#field_prefix'] = '<div class="description">' . $element['#description'] . '</div>' . $element['#field_prefix'];
          unset($element['#description']);
          break;
        case 'invisible':
          $element += [
            '#field_suffix' => '',
          ];
          $element['#field_suffix'] .= '<div class="description visually-hidden">' . $element['#description'] . '</div>';
          unset($element['#description']);
          break;
      }
    }
  }
}