Same name and namespace in other branches
  1. 8.9.x core/modules/options/src/Plugin/Field/FieldType/ListIntegerItem.php \Drupal\options\Plugin\Field\FieldType\ListIntegerItem
  2. 9 core/modules/options/src/Plugin/Field/FieldType/ListIntegerItem.php \Drupal\options\Plugin\Field\FieldType\ListIntegerItem

Plugin implementation of the 'list_integer' field type.

Plugin annotation


@FieldType(
  id = "list_integer",
  label = @Translation("List (integer)"),
  description = {
    @Translation("Values stored are numbers without decimals"),
    @Translation("For example, 'Lifetime in days': 1 => 1 day, 7 => 1 week, 31 => 1 month"),
  },
  category = "selection_list",
  weight = -30,
  default_widget = "options_select",
  default_formatter = "list_default",
)

Hierarchy

  • class \Drupal\options\Plugin\Field\FieldType\ListIntegerItem extends \Drupal\options\Plugin\Field\FieldType\ListItemBase

Expanded class hierarchy of ListIntegerItem

1 file declares its use of ListIntegerItem
XmlEntityNormalizationQuirksTrait.php in core/modules/rest/tests/src/Functional/EntityResource/XmlEntityNormalizationQuirksTrait.php

File

core/modules/options/src/Plugin/Field/FieldType/ListIntegerItem.php, line 28

Namespace

Drupal\options\Plugin\Field\FieldType
View source
class ListIntegerItem extends ListItemBase {

  /**
   * {@inheritdoc}
   */
  public static function propertyDefinitions(FieldStorageDefinitionInterface $field_definition) {
    $properties['value'] = DataDefinition::create('integer')
      ->setLabel(new TranslatableMarkup('Integer value'))
      ->setRequired(TRUE);
    return $properties;
  }

  /**
   * {@inheritdoc}
   */
  public static function schema(FieldStorageDefinitionInterface $field_definition) {
    return [
      'columns' => [
        'value' => [
          'type' => 'int',
        ],
      ],
      'indexes' => [
        'value' => [
          'value',
        ],
      ],
    ];
  }

  /**
   * {@inheritdoc}
   */
  protected function allowedValuesDescription() {
    $description = '<p>' . $this
      ->t('The name will be used in displayed options and edit forms. The value is the stored value, and must be numeric.') . '</p>';
    $description .= '<p>' . $this
      ->t('Allowed HTML tags in labels: @tags', [
      '@tags' => FieldFilteredMarkup::displayAllowedTags(),
    ]) . '</p>';
    return $description;
  }

  /**
   * {@inheritdoc}
   */
  protected static function validateAllowedValue($option) {
    if (!preg_match('/^-?\\d+$/', $option)) {
      return new TranslatableMarkup('Allowed values list: keys must be integers.');
    }
  }

  /**
   * {@inheritdoc}
   */
  protected static function castAllowedValue($value) {
    return (int) $value;
  }

  /**
   * {@inheritdoc}
   */
  public function storageSettingsForm(array &$form, FormStateInterface $form_state, $has_data) {
    $element = parent::storageSettingsForm($form, $form_state, $has_data);
    foreach (Element::children($element['allowed_values']['table']) as $delta => $row) {

      // @see https://developer.mozilla.org/en-US/docs/Web/HTML/Element/input/number
      // @see \Drupal\Core\Field\Plugin\Field\FieldWidget\NumberWidget::formElement()
      $element['allowed_values']['table'][$delta]['item']['key']['#type'] = 'number';
    }
    return $element;
  }

}

Members