Unlimited Number Field

Component ID

2566033

Component name

Unlimited Number Field

Component type

module

Maintenance status

Development status

Component security advisory coverage

not-covered

Downloads

6261

Component created

Component changed

Component body

Adds a custom render element and field widget allowing the user to choose "Unlimited" or a number value.

The unlimited_number element renders a radios form element with an inline number field. Custom prefix and suffixes can be applied to the number field. The radio labels can also be customized.

This field eliminates the UI oddity where form descriptions will often request the user to leave a field empty, set to 0, or set to -1, where an unlimited value takes effect.

Stack Exchange Support requests are taken on Drupal Answers with the fields tag. Ask a question!
Github Please post bug reports and feature requests to the GitHub project.

Usage

Form Element

// Define an unlimited_number form field.
// Only the #type key is required.
$form['my_field'] = [
  '#type' => 'unlimited_number',
  // Optional properties:
  '#title' => $this->t('My field'),
  '#description' => $this->t('Choose an option.'),
  '#default_value' => 1, // an integer, or the string 'unlimited'.
  '#required' => TRUE,
  '#options' => [
    // Customize the labels on the radios element.
    'unlimited' => $this->t('No limit'),
    'limited' => $this->t('Fixed value'),
  ],
  // The following are applied to the number field.
  '#min' => 1,
  '#max' => 128,
  '#step' => 1,
  '#field_suffix' => 'units',
  '#field_prefix' => NULL,
];

Field Widget

The field widget can be applied to any existing number field.

  1. Go to the Manage fields page for an entity type.
  2. Add a Number (integer) field if one does not yet exist.
  3. Go to Manage form display page.
  4. Locate the Widget select element located adjacent to your number field. Change the existing value to Unlimited or Number. Save the form.

Dependencies

  • Drupal 8