You are here

public static function Dimensions::processElement in Physical Fields 8

Builds the physical_dimensions form element.

Parameters

array $element: The initial physical_dimensions 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_dimensions form element.

Throws

\InvalidArgumentException.

File

src/Element/Dimensions.php, line 78

Class

Dimensions
Provides a dimension form element.

Namespace

Drupal\physical\Element

Code

public static function processElement(array $element, FormStateInterface $form_state, array &$complete_form) {
  if (!is_array($element['#available_units'])) {
    throw new \InvalidArgumentException('The #available_units key must be an array.');
  }
  $default_value = $element['#default_value'];
  if (isset($default_value)) {
    if (!self::validateDefaultValue($default_value)) {
      throw new \InvalidArgumentException('The #default_value for a physical_dimensions element must be an array with "length", "width", "height", "unit" keys.');
    }
    LengthUnit::assertExists($default_value['unit']);
  }
  $element['#tree'] = TRUE;
  $element['#attributes']['class'][] = 'form-type-physical-dimensions';
  $properties = [
    'length' => t('Length'),
    'width' => t('Width'),
    'height' => t('Height'),
  ];
  foreach ($properties as $property => $label) {
    $element[$property] = [
      '#type' => 'physical_number',
      '#title' => $label,
      '#default_value' => $default_value ? $default_value[$property] : NULL,
      '#size' => $element['#size'],
      '#maxlength' => $element['#maxlength'],
      '#required' => $element['#required'],
      '#field_suffix' => '×',
    ];
  }
  unset($element['height']['#field_suffix']);
  unset($element['#size']);
  unset($element['#maxlength']);
  $units = LengthUnit::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 = 'height';
    $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['height']['#field_suffix'] = $unit;
  }
  else {
    $last_visible_element = 'unit';
    $element['unit'] = [
      '#type' => 'select',
      '#options' => $units,
      '#default_value' => $default_value ? $default_value['unit'] : LengthUnit::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;
}