class NumberWidget

Plugin implementation of the 'number' widget.

Plugin annotation


@Plugin(
  id = "number",
  module = "number",
  label = @Translation("Text field"),
  field_types = {
    "number_integer",
    "number_decimal",
    "number_float"
  },
  settings = {
    "placeholder" = ""
  }
)

Hierarchy

Expanded class hierarchy of NumberWidget

File

drupal/core/modules/field/modules/number/lib/Drupal/number/Plugin/field/widget/NumberWidget.php, line 31
Definition of Drupal\number\Plugin\field\widget\NumberWidget.

Namespace

Drupal\number\Plugin\field\widget
View source
class NumberWidget extends WidgetBase {

  /**
   * Implements Drupal\field\Plugin\Type\Widget\WidgetInterface::settingsForm().
   */
  public function settingsForm(array $form, array &$form_state) {
    $element['placeholder'] = array(
      '#type' => 'textfield',
      '#title' => t('Placeholder'),
      '#default_value' => $this
        ->getSetting('placeholder'),
      '#description' => t('The placeholder is a short hint (a word or short phrase) intended to aid the user with data entry. A hint could be a sample value or a brief description of the expected format.'),
    );
    return $element;
  }

  /**
   * Implements Drupal\field\Plugin\Type\Widget\WidgetInterface::formElement().
   */
  public function formElement(array $items, $delta, array $element, $langcode, array &$form, array &$form_state) {
    $field = $this->field;
    $instance = $this->instance;
    $value = isset($items[$delta]['value']) ? $items[$delta]['value'] : NULL;
    $element += array(
      '#type' => 'number',
      '#default_value' => $value,
      '#placeholder' => $this
        ->getSetting('placeholder'),
    );

    // Set the step for floating point and decimal numbers.
    switch ($field['type']) {
      case 'number_decimal':
        $element['#step'] = pow(0.1, $field['settings']['scale']);
        break;
      case 'number_float':
        $element['#step'] = 'any';
        break;
    }

    // Set minimum and maximum.
    if (is_numeric($instance['settings']['min'])) {
      $element['#min'] = $instance['settings']['min'];
    }
    if (is_numeric($instance['settings']['max'])) {
      $element['#max'] = $instance['settings']['max'];
    }

    // Add prefix and suffix.
    if (!empty($instance['settings']['prefix'])) {
      $prefixes = explode('|', $instance['settings']['prefix']);
      $element['#field_prefix'] = field_filter_xss(array_pop($prefixes));
    }
    if (!empty($instance['settings']['suffix'])) {
      $suffixes = explode('|', $instance['settings']['suffix']);
      $element['#field_suffix'] = field_filter_xss(array_pop($suffixes));
    }
    return array(
      'value' => $element,
    );
  }

  /**
   * Implements Drupal\field\Plugin\Type\Widget\WidgetInterface::errorElement().
   */
  public function errorElement(array $element, array $error, array $form, array &$form_state) {
    return $element['value'];
  }

}

Members

Namesort descending Modifiers Type Description Overrides
NumberWidget::errorElement public function Implements Drupal\field\Plugin\Type\Widget\WidgetInterface::errorElement(). Overrides WidgetBase::errorElement
NumberWidget::formElement public function Implements Drupal\field\Plugin\Type\Widget\WidgetInterface::formElement(). Overrides WidgetInterface::formElement
NumberWidget::settingsForm public function Implements Drupal\field\Plugin\Type\Widget\WidgetInterface::settingsForm(). Overrides WidgetBase::settingsForm
PluginBase::$configuration protected property Configuration information passed into the plugin. 1
PluginBase::$discovery protected property The discovery object.
PluginBase::$plugin_id protected property The plugin_id.
PluginBase::getDefinition public function Implements Drupal\Component\Plugin\PluginInterface::getDefinition(). Overrides PluginInspectionInterface::getDefinition
PluginBase::getPluginId public function Implements Drupal\Component\Plugin\PluginInterface::getPluginId(). Overrides PluginInspectionInterface::getPluginId
PluginSettingsBase::$defaultSettingsMerged protected property Whether default settings have been merged into the current $settings.
PluginSettingsBase::getDefaultSettings public function Implements Drupal\field\Plugin\PluginSettingsInterface::getDefaultSettings(). Overrides PluginSettingsInterface::getDefaultSettings
PluginSettingsBase::getSetting public function Implements Drupal\field\Plugin\PluginSettingsInterface::getSetting(). Overrides PluginSettingsInterface::getSetting
PluginSettingsBase::getSettings public function Implements Drupal\field\Plugin\PluginSettingsInterface::getSettings(). Overrides PluginSettingsInterface::getSettings
PluginSettingsBase::mergeDefaults protected function Merges default settings values into $settings.
PluginSettingsBase::setSetting public function Implements Drupal\field\Plugin\PluginSettingsInterface::setSetting(). Overrides PluginSettingsInterface::setSetting
PluginSettingsBase::setSettings public function Implements Drupal\field\Plugin\PluginSettingsInterface::setSettings(). Overrides PluginSettingsInterface::setSettings
WidgetBase::$field protected property The field definition.
WidgetBase::$instance protected property The field instance definition.
WidgetBase::$settings protected property The widget settings. Overrides PluginSettingsBase::$settings
WidgetBase::$weight protected property The widget weight.
WidgetBase::flagErrors public function Implements Drupal\field\Plugin\Type\Widget\WidgetInterface::flagErrors(). Overrides WidgetBaseInterface::flagErrors 1
WidgetBase::form public function Implements Drupal\field\Plugin\Type\Widget\WidgetInterface::form(). Overrides WidgetBaseInterface::form
WidgetBase::formMultipleElements protected function Special handling to create form elements for multiple values. 1
WidgetBase::formSingleElement protected function Generates the form element for a single copy of the widget.
WidgetBase::massageFormValues public function Implements Drupal\field\Plugin\Type\Widget\WidgetInterface::massageFormValues() Overrides WidgetInterface::massageFormValues 1
WidgetBase::sortItems protected function Sorts submitted field values according to drag-n-drop reordering.
WidgetBase::submit public function Implements Drupal\field\Plugin\Type\Widget\WidgetInterface::submit(). Overrides WidgetBaseInterface::submit
WidgetBase::__construct public function Constructs a WidgetBase object. Overrides PluginBase::__construct