View source
<?php
namespace Drupal\physical\Element;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Render\Element\FormElement;
use Drupal\physical\MeasurementType;
class Measurement extends FormElement {
public function getInfo() {
$class = get_class($this);
return [
'#measurement_type' => NULL,
'#available_units' => [],
'#size' => 10,
'#maxlength' => 128,
'#default_value' => NULL,
'#attached' => [
'library' => [
'physical/admin',
],
],
'#process' => [
[
$class,
'processElement',
],
[
$class,
'processAjaxForm',
],
[
$class,
'processGroup',
],
],
'#pre_render' => [
[
$class,
'preRenderGroup',
],
],
'#input' => TRUE,
'#theme_wrappers' => [
'container',
],
];
}
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.');
}
$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();
if ($element['#available_units']) {
$available_units = $element['#available_units'];
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,
];
$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' => '',
];
}
if (!empty($element['#description'])) {
$element[$last_visible_element]['#field_suffix'] .= '<div class="description">' . $element['#description'] . '</div>';
}
return $element;
}
public static function validateDefaultValue($default_value) {
if (!is_array($default_value)) {
return FALSE;
}
if (!array_key_exists('number', $default_value) || !array_key_exists('unit', $default_value)) {
return FALSE;
}
return TRUE;
}
}