You are here

public static function Measurement::processElement in Physical Fields 8

Builds the physical_measurement form element.

Parameters

array $element: The initial physical_measurement form element.

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

array $complete_form: The complete form structure.

Return value

array The built physical_measurement form element.

Throws

\InvalidArgumentException.

File

src/Element/Measurement.php, line 73

Class

Measurement
Provides a measurement form element.

Namespace

Drupal\physical\Element

Code

public static function processElement(array $element, FormStateInterface $form_state, array &$complete_form) {
  if (empty($element['#measurement_type'])) {
    throw new \InvalidArgumentException('The #measurement_type must be defined for a physical_measurement element.');
  }
  if (!is_array($element['#available_units'])) {
    throw new \InvalidArgumentException('The #available_units key must be an array.');
  }

  /** @var \Drupal\physical\UnitInterface $unit_class */
  $unit_class = MeasurementType::getUnitClass($element['#measurement_type']);
  $default_value = $element['#default_value'];
  if (isset($default_value)) {
    if (!self::validateDefaultValue($default_value)) {
      throw new \InvalidArgumentException('The #default_value for a physical_measurement element must be an array with "number" and "unit" keys.');
    }
    $unit_class::assertExists($default_value['unit']);
  }
  $element['#tree'] = TRUE;
  $element['#attributes']['class'][] = 'form-type-physical-measurement';
  $element['number'] = [
    '#type' => 'physical_number',
    '#title' => $element['#title'],
    '#default_value' => $default_value ? $default_value['number'] : NULL,
    '#required' => $element['#required'],
    '#size' => $element['#size'],
    '#maxlength' => $element['#maxlength'],
  ];
  unset($element['#size']);
  unset($element['#maxlength']);
  $units = $unit_class::getLabels();

  // Restrict the list of available units, if needed.
  if ($element['#available_units']) {
    $available_units = $element['#available_units'];

    // The current unit should always be available.
    if ($default_value) {
      $available_units[] = $default_value['unit'];
    }
    $available_units = array_combine($available_units, $available_units);
    $units = array_intersect_key($units, $available_units);
  }
  if (count($units) === 1) {
    $last_visible_element = 'number';
    $unit_keys = array_keys($units);
    $unit = reset($unit_keys);
    $element['unit'] = [
      '#type' => 'value',
      '#value' => $unit,
    ];

    // Display the unit as a text element after the textfield.
    $element['number']['#field_suffix'] = $unit;
  }
  else {
    $last_visible_element = 'unit';
    $element['unit'] = [
      '#type' => 'select',
      '#options' => $units,
      '#default_value' => $default_value ? $default_value['unit'] : $unit_class::getBaseUnit(),
      '#title_display' => 'invisible',
      '#field_suffix' => '',
    ];
  }

  // Add the help text if specified.
  if (!empty($element['#description'])) {
    $element[$last_visible_element]['#field_suffix'] .= '<div class="description">' . $element['#description'] . '</div>';
  }
  return $element;
}