You are here

public static function ScssNumber::processNumber in SCSS Compiler 1.0.x

Process the element before it gets rendered in the form.

This method will ensure that the element is initialized with the following properties before being rendered:

1. '#required': Whether or not a value is required 2. '#unit_options': An array of unit options from which to select a unit 3. '#unit_required': Whether or not a unit is required

A unit select box will only be displayed if there is at least one available unit option and at least one of the following is true:

1. A unit is not required (i.e., the user can opt for a unit-less value) 2. More than one unit option is available.

If there is only one required unit, it will be stored in a hidden input and the unit label will be applied as a suffix to the value form element.

Parameters

array $element: The element to process before being rendered.

\Drupal\Core\Form\FormStateInterface $form_state: The current state of the form.

array $complete_form: The complete form.

Return value

array The resulting element after having been processed.

File

src/Element/ScssNumber.php, line 399

Class

ScssNumber
A form element to represent Sass numbers with a unit.

Namespace

Drupal\compiler_scss\Element

Code

public static function processNumber(array &$element, FormStateInterface $form_state, array &$complete_form) {

  // A unit cannot be required if there are no units available to select.
  if (empty($element['#unit_options'] = self::getUnitOptions($element))) {
    $element['#unit_required'] = FALSE;
  }

  // Add a value element used to track the magnitude of the composite element.
  $element['value'] = [
    '#type' => 'number',
    '#title' => $element['#title'],
    '#title_display' => 'none',
    '#name' => "{$element['#name']}[value]",
    '#default_value' => self::getValue($element, 'value'),
    '#max' => $element['#max'],
    '#min' => $element['#min'],
    '#placeholder' => $element['#placeholder'],
    '#required' => $element['#required'],
    '#step' => isset($element['#step']) ? $element['#step'] : 'any',
  ];

  // Check if there is only a single, required unit available.
  if ($element['#unit_required'] && count($element['#unit_options']) === 1) {
    $element['value']['#field_suffix'] = end($element['#unit_options']);
    self::processUnitSingle($element, $form_state, $complete_form);
  }
  elseif (!empty($element['#unit_options'])) {
    self::processUnitMultiple($element, $form_state, $complete_form);
  }

  // Add references to the template '#children' property for each element.
  $element['#children']['value'] =& $element['value'];
  $element['#children']['unit'] =& $element['unit'];
  return $element;
}