public static function UnlimitedNumber::processUnlimitedNumber in Unlimited Number Field 8
Same name and namespace in other branches
- 2.x src/Element/UnlimitedNumber.php \Drupal\unlimited_number\Element\UnlimitedNumber::processUnlimitedNumber()
Adds form fields for radios and number field.
Return value
array The processed element.
File
- src/
Element/ UnlimitedNumber.php, line 51
Class
- UnlimitedNumber
- Provides an unlimited or number radios element
Namespace
Drupal\unlimited_number\ElementCode
public static function processUnlimitedNumber(&$element, FormStateInterface $form_state, &$complete_form) {
$value = isset($element['#default_value']) ? $element['#default_value'] : NULL;
$element['unlimited_number'] = [
'#type' => 'radios',
'#options' => NULL,
'#title' => $element['#title'],
'#description' => $element['#description'],
'#required' => !empty($element['#required']),
// Kills \Drupal\Core\Render\Element\Radios::processRadios.
'#process' => [],
];
$element['unlimited_number']['unlimited'] = [
'#prefix' => '<div class="form-item">',
'#suffix' => '</div>',
];
$element['unlimited_number']['unlimited']['radio'] = [
'#type' => 'radio',
'#title' => !empty($element['#options']['unlimited']) ? $element['#options']['unlimited'] : t('Unlimited'),
'#return_value' => 'unlimited',
'#parents' => array_merge($element['#parents'], [
'unlimited_number',
]),
'#default_value' => $value == static::UNLIMITED ? 'unlimited' : NULL,
// Errors only on parent.
'#error_no_message' => TRUE,
'#attributes' => $element['#attributes'],
'#ajax' => isset($element['#ajax']) ? $element['#ajax'] : NULL,
];
$element['unlimited_number']['limited'] = [
'#prefix' => '<div class="form-item container-inline">',
'#suffix' => '</div>',
];
$element['unlimited_number']['limited']['radio'] = [
'#type' => 'radio',
'#title' => !empty($element['#options']['limited']) ? $element['#options']['limited'] : t('Limited'),
'#return_value' => 'limited',
'#parents' => array_merge($element['#parents'], [
'unlimited_number',
]),
'#default_value' => is_numeric($value) ? 'limited' : NULL,
'#error_no_message' => TRUE,
'#attributes' => $element['#attributes'],
'#ajax' => isset($element['#ajax']) ? $element['#ajax'] : NULL,
];
$element['unlimited_number']['limited']['number'] = [
'#type' => 'number',
'#parents' => array_merge($element['#parents'], [
'number',
]),
'#default_value' => is_numeric($value) ? $value : NULL,
'#field_prefix' => isset($element['#field_prefix']) ? $element['#field_prefix'] : NULL,
'#field_suffix' => isset($element['#field_suffix']) ? $element['#field_suffix'] : NULL,
'#min' => isset($element['#min']) ? $element['#min'] : NULL,
'#max' => isset($element['#max']) ? $element['#max'] : NULL,
'#step' => isset($element['#step']) ? $element['#step'] : NULL,
'#attributes' => $element['#attributes'],
'#ajax' => isset($element['#ajax']) ? $element['#ajax'] : NULL,
];
// Must be a tree otherwise the last processed child element will leak its
// value down to the root element.
$element['#tree'] = TRUE;
// Prevent entire tree being required. Fixes empty number field adding
// errors to the entire tree.
$element['#required'] = FALSE;
// Unset options as they are not valid radios.
unset($element['#options']);
return $element;
}