You are here

public function Range::prepare in Webform 8.5

Same name and namespace in other branches
  1. 6.x src/Plugin/WebformElement/Range.php \Drupal\webform\Plugin\WebformElement\Range::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 NumericBase::prepare

See also

\Drupal\webform\Element\WebformCompositeBase::processWebformComposite

File

src/Plugin/WebformElement/Range.php, line 56

Class

Range
Provides a 'range' element.

Namespace

Drupal\webform\Plugin\WebformElement

Code

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

  // Set default min/max. Default step is defined via parent::prepare().
  $element += [
    '#min' => $this
      ->getDefaultProperty('min'),
    '#max' => $this
      ->getDefaultProperty('max'),
  ];

  // If no custom range output is defined then exit.
  if (empty($element['#output'])) {
    return;
  }
  $webform_key = isset($element['#webform_key']) ? $element['#webform_key'] : 'range';
  if (in_array($element['#output'], [
    'above',
    'below',
  ])) {
    $element += [
      '#output__attributes' => [],
    ];
    $attributes = new Attribute($element['#output__attributes']);
    $attributes['for'] = $webform_key;
    $attributes['data-display'] = $element['#output'];
    if (isset($element['#output__field_prefix'])) {
      $attributes['data-field-prefix'] = $element['#output__field_prefix'];
    }
    if (isset($element['#output__field_suffix'])) {
      $attributes['data-field-suffix'] = $element['#output__field_suffix'];
    }
    $element['#children'] = Markup::create('<output' . $attributes . '></output>');
  }
  else {

    // Create output (number) element.
    $output = [
      '#type' => 'number',
      '#title' => $element['#title'],
      '#title_display' => 'invisible',
      '#id' => $webform_key . '__output',
      '#name' => $webform_key . '__output',
    ];

    // Copy range (number) properties to output element.
    $properties = [
      '#min',
      '#max',
      '#step',
      '#disabled',
    ];
    $output += array_intersect_key($element, array_combine($properties, $properties));

    // Copy custom output properties to output element.
    foreach ($element as $key => $value) {
      if (strpos($key, '#output__') === 0) {
        $output_key = str_replace('#output__', '#', $key);
        $output[$output_key] = $value;
      }
    }

    // Manually copy disabled from input to output because the output is not
    // handled by the FormBuilder.
    // @see \Drupal\Core\Form\FormBuilder::handleInputElement
    if (!empty($output['#disabled'])) {
      $output['#attributes']['disabled'] = TRUE;
    }

    // Set the output's input name to an empty string so that it is not
    // posted back to the server.
    $output['#attributes']['name'] = '';

    // Calculate the output's width based on the #max number's string length.
    $output['#attributes'] += [
      'style' => '',
    ];
    $output['#attributes']['style'] .= ($output['#attributes']['style'] ? ';' : '') . 'width:' . (strlen($element['#max'] . '') + 1) . 'em';

    // Append output element as a child.
    if ($element['#output'] === 'left') {
      if (isset($element['#field_prefix'])) {
        $element['#field_prefix'] = [
          'output' => $output,
          'delimiter' => [
            '#markup' => '<span class="webform-range-output-delimiter"></span>',
          ],
          'content' => is_array($element['#field_prefix']) ? $element['#field_prefix'] : [
            '#markup' => $element['#field_prefix'],
          ],
        ];
      }
      else {
        $element['#field_suffix'] = [
          'output' => $output,
          'delimiter' => [
            '#markup' => '<span class="webform-range-output-delimiter"></span>',
          ],
        ];
      }
    }
    else {
      if (isset($element['#field_suffix'])) {
        $element['#field_suffix'] = [
          'content' => is_array($element['#field_suffix']) ? $element['#field_suffix'] : [
            '#markup' => $element['#field_suffix'],
          ],
          'delimiter' => [
            '#markup' => '<span class="webform-range-output-delimiter"></span>',
          ],
          'output' => $output,
        ];
      }
      else {
        $element['#field_suffix'] = [
          'delimiter' => [
            '#markup' => '<span class="webform-range-output-delimiter"></span>',
          ],
          'output' => $output,
        ];
      }
    }
  }
  $element['#attached']['library'][] = 'webform/webform.element.range';
}