IntegerItem.php 2.7 KB
Newer Older
1 2 3 4
<?php

/**
 * @file
5
 * Contains \Drupal\Core\Field\Plugin\Field\FieldType\IntegerItem.
6 7
 */

8
namespace Drupal\Core\Field\Plugin\Field\FieldType;
9

10
use Drupal\Core\Field\FieldStorageDefinitionInterface;
11
use Drupal\Core\TypedData\DataDefinition;
12 13

/**
14
 * Defines the 'integer' field type.
15
 *
16 17
 * @FieldType(
 *   id = "integer",
18 19 20 21
 *   label = @Translation("Number (integer)"),
 *   description = @Translation("This field stores a number in the database as an integer."),
 *   default_widget = "number",
 *   default_formatter = "number_integer"
22
 * )
23
 */
24
class IntegerItem extends NumericItemBase {
25

26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43
  /**
   * {@inheritdoc}
   */
  public static function defaultSettings() {
    return array(
      'unsigned' => FALSE,
    ) + parent::defaultSettings();
  }

  /**
   * {@inheritdoc}
   */
  public static function defaultInstanceSettings() {
    return array(
      'min' => '',
      'max' => '',
      'prefix' => '',
      'suffix' => '',
44 45 46
      // Valid size property values include: 'tiny', 'small', 'medium', 'normal'
      // and 'big'.
      'size' => 'normal',
47 48 49
    ) + parent::defaultInstanceSettings();
  }

50
  /**
51
   * {@inheritdoc}
52
   */
53
  public static function propertyDefinitions(FieldStorageDefinitionInterface $field_definition) {
54 55
    $properties['value'] = DataDefinition::create('integer')
      ->setLabel(t('Integer value'));
56

57
    return $properties;
58
  }
59

60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85
  /**
   * {@inheritdoc}
   */
  public function getConstraints() {
    $constraints = parent::getConstraints();

    // If this is an unsigned integer, add a validation constraint for the
    // integer to be positive.
    if ($this->getSetting('unsigned')) {
      $constraint_manager = \Drupal::typedDataManager()->getValidationConstraintManager();
      $constraints[] = $constraint_manager->create('ComplexData', array(
        'value' => array(
          'Range' => array(
            'min' => 0,
            'minMessage' => t('%name: The integer must be larger or equal to %min.', array(
              '%name' => $this->getFieldDefinition()->getLabel(),
              '%min' => 0,
            )),
          ),
        ),
      ));
    }

    return $constraints;
  }

86 87 88
  /**
   * {@inheritdoc}
   */
89
  public static function schema(FieldStorageDefinitionInterface $field_definition) {
90 91 92 93
    return array(
      'columns' => array(
        'value' => array(
          'type' => 'int',
94
          'not null' => FALSE,
95 96
          // Expose the 'unsigned' setting in the field item schema.
          'unsigned' => $field_definition->getSetting('unsigned'),
97 98 99
          // Expose the 'size' setting in the field item schema. For instance,
          // supply 'big' as a value to produce a 'bigint' type.
          'size' => $field_definition->getSetting('size'),
100 101 102 103 104
        ),
      ),
    );
  }

105
}