You are here

public static function Fraction::processElement in Fraction 2.x

Same name and namespace in other branches
  1. 8 src/Element/Fraction.php \Drupal\fraction\Element\Fraction::processElement()

Builds the fraction form element.

Parameters

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

File

src/Element/Fraction.php, line 62

Class

Fraction
Provides a fraction form element.

Namespace

Drupal\fraction\Element

Code

public static function processElement(array $element, FormStateInterface $form_state, array &$complete_form) {
  $default_value = $element['#default_value'];
  if (isset($default_value) && !self::validateDefaultValue($default_value)) {
    throw new \InvalidArgumentException('The #default_value for a fraction element must be an array with "numerator" and "denominator" keys.');
  }
  $element['#tree'] = TRUE;
  $element['#attributes']['class'][] = 'form-type-fraction';
  $element['numerator'] = [
    '#type' => 'number',
    '#title' => t('Numerator'),
    '#title_display' => $element['#title_display'],
    '#default_value' => $default_value ? $default_value['numerator'] : NULL,
    '#required' => $element['#required'],
    '#size' => $element['#size'],
    '#error_no_message' => TRUE,
  ];
  $element['denominator'] = [
    '#type' => 'number',
    '#title' => t('Denominator'),
    '#title_display' => $element['#title_display'],
    '#default_value' => $default_value ? $default_value['denominator'] : NULL,
    '#description' => !empty($element['#description']) ?? '',
    '#required' => $element['#required'],
    '#size' => $element['#size'],
    '#error_no_message' => TRUE,
  ];

  // Remove the keys that were transferred to child elements.
  unset($element['#size']);
  unset($element['#description']);
  return $element;
}