Commit cf0549db authored by catch's avatar catch

Issue #2177799 by tstoeckler, jsbalsera, mauzeh: Allow IntegerItem's to be unsigned.

parent 35a33d68
......@@ -63,9 +63,7 @@ public static function propertyDefinitions(FieldDefinitionInterface $field_defin
// https://drupal.org/node/2107249
$target_id_definition = DataDefinition::create('integer')
->setLabel(t('Entity ID'))
->setConstraints(array(
'Range' => array('min' => 0),
));
->setSetting('unsigned', TRUE);
}
else {
$target_id_definition = DataDefinition::create('string')
......
......@@ -23,6 +23,27 @@
*/
class IntegerItem extends NumericItemBase {
/**
* {@inheritdoc}
*/
public static function defaultSettings() {
return array(
'unsigned' => FALSE,
) + parent::defaultSettings();
}
/**
* {@inheritdoc}
*/
public static function defaultInstanceSettings() {
return array(
'min' => '',
'max' => '',
'prefix' => '',
'suffix' => '',
) + parent::defaultInstanceSettings();
}
/**
* {@inheritdoc}
*/
......@@ -33,6 +54,32 @@ public static function propertyDefinitions(FieldDefinitionInterface $field_defin
return $properties;
}
/**
* {@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;
}
/**
* {@inheritdoc}
*/
......@@ -42,6 +89,8 @@ public static function schema(FieldDefinitionInterface $field_definition) {
'value' => array(
'type' => 'int',
'not null' => FALSE,
// Expose the 'unsigned' setting in the field item schema.
'unsigned' => $field_definition->getSetting('unsigned'),
),
),
);
......
......@@ -134,7 +134,8 @@ public static function baseFieldDefinitions(EntityTypeInterface $entity_type) {
$fields['fid'] = FieldDefinition::create('integer')
->setLabel(t('Feed ID'))
->setDescription(t('The ID of the aggregator feed.'))
->setReadOnly(TRUE);
->setReadOnly(TRUE)
->setSetting('unsigned', TRUE);
$fields['uuid'] = FieldDefinition::create('uuid')
->setLabel(t('UUID'))
......@@ -155,7 +156,8 @@ public static function baseFieldDefinitions(EntityTypeInterface $entity_type) {
$fields['refresh'] = FieldDefinition::create('integer')
->setLabel(t('Refresh'))
->setDescription(t('How often to check for new feed items, in seconds.'));
->setDescription(t('How often to check for new feed items, in seconds.'))
->setSetting('unsigned', TRUE);
$fields['checked'] = FieldDefinition::create('timestamp')
->setLabel(t('Checked'))
......
......@@ -54,7 +54,8 @@ public static function baseFieldDefinitions(EntityTypeInterface $entity_type) {
$fields['iid'] = FieldDefinition::create('integer')
->setLabel(t('Aggregator item ID'))
->setDescription(t('The ID of the feed item.'))
->setReadOnly(TRUE);
->setReadOnly(TRUE)
->setSetting('unsigned', TRUE);
$fields['fid'] = FieldDefinition::create('entity_reference')
->setLabel(t('Aggregator feed ID'))
......
......@@ -158,7 +158,8 @@ public static function baseFieldDefinitions(EntityTypeInterface $entity_type) {
$fields['id'] = FieldDefinition::create('integer')
->setLabel(t('Custom block ID'))
->setDescription(t('The custom block ID.'))
->setReadOnly(TRUE);
->setReadOnly(TRUE)
->setSetting('unsigned', TRUE);
$fields['uuid'] = FieldDefinition::create('uuid')
->setLabel(t('UUID'))
......@@ -168,7 +169,8 @@ public static function baseFieldDefinitions(EntityTypeInterface $entity_type) {
$fields['revision_id'] = FieldDefinition::create('integer')
->setLabel(t('Revision ID'))
->setDescription(t('The revision ID.'))
->setReadOnly(TRUE);
->setReadOnly(TRUE)
->setSetting('unsigned', TRUE);
$fields['langcode'] = FieldDefinition::create('language')
->setLabel(t('Language code'))
......
......@@ -210,7 +210,8 @@ public static function baseFieldDefinitions(EntityTypeInterface $entity_type) {
$fields['cid'] = FieldDefinition::create('integer')
->setLabel(t('Comment ID'))
->setDescription(t('The comment ID.'))
->setReadOnly(TRUE);
->setReadOnly(TRUE)
->setSetting('unsigned', TRUE);
$fields['uuid'] = FieldDefinition::create('uuid')
->setLabel(t('UUID'))
......
......@@ -110,7 +110,7 @@ public static function propertyDefinitions(FieldDefinitionInterface $field_defin
if ($target_type_info->hasKey('revision') && $target_type_info->getRevisionTable()) {
$properties['revision_id'] = DataDefinition::create('integer')
->setLabel(t('Revision ID'))
->setConstraints(array('Range' => array('min' => 0)));
->setSetting('unsigned', TRUE);
}
return $properties;
......
......@@ -233,7 +233,8 @@ public static function baseFieldDefinitions(EntityTypeInterface $entity_type) {
$fields['fid'] = FieldDefinition::create('integer')
->setLabel(t('File ID'))
->setDescription(t('The file ID.'))
->setReadOnly(TRUE);
->setReadOnly(TRUE)
->setSetting('unsigned', TRUE);
$fields['uuid'] = FieldDefinition::create('uuid')
->setLabel(t('UUID'))
......@@ -263,7 +264,8 @@ public static function baseFieldDefinitions(EntityTypeInterface $entity_type) {
$fields['filesize'] = FieldDefinition::create('integer')
->setLabel(t('File size'))
->setDescription(t('The size of the file in bytes.'));
->setDescription(t('The size of the file in bytes.'))
->setSetting('unsigned', TRUE);
$fields['status'] = FieldDefinition::create('integer')
->setLabel(t('Status'))
......
......@@ -335,7 +335,8 @@ public static function baseFieldDefinitions(EntityTypeInterface $entity_type) {
$fields['nid'] = FieldDefinition::create('integer')
->setLabel(t('Node ID'))
->setDescription(t('The node ID.'))
->setReadOnly(TRUE);
->setReadOnly(TRUE)
->setSetting('unsigned', TRUE);
$fields['uuid'] = FieldDefinition::create('uuid')
->setLabel(t('UUID'))
......@@ -345,7 +346,8 @@ public static function baseFieldDefinitions(EntityTypeInterface $entity_type) {
$fields['vid'] = FieldDefinition::create('integer')
->setLabel(t('Revision ID'))
->setDescription(t('The node revision ID.'))
->setReadOnly(TRUE);
->setReadOnly(TRUE)
->setSetting('unsigned', TRUE);
$fields['type'] = FieldDefinition::create('entity_reference')
->setLabel(t('Type'))
......
......@@ -136,7 +136,8 @@ public static function baseFieldDefinitions(EntityTypeInterface $entity_type) {
$fields['id'] = FieldDefinition::create('integer')
->setLabel(t('ID'))
->setDescription(t('The ID of the shortcut.'))
->setReadOnly(TRUE);
->setReadOnly(TRUE)
->setSetting('unsigned', TRUE);
$fields['uuid'] = FieldDefinition::create('uuid')
->setLabel(t('UUID'))
......
......@@ -104,6 +104,12 @@ protected function checkValidation($entity_type) {
$this->assertEqual($violations->count(), 0, 'Validation passes.');
// Test triggering a fail for each of the constraints specified.
$test_entity = clone $entity;
$test_entity->id->value = -1;
$violations = $test_entity->validate();
$this->assertEqual($violations->count(), 1, 'Validation failed.');
$this->assertEqual($violations[0]->getMessage(), t('%name: The integer must be larger or equal to %min.', array('%name' => 'ID', '%min' => 0)));
$test_entity = clone $entity;
$test_entity->uuid->value = $this->randomString(129);
$violations = $test_entity->validate();
......
......@@ -65,7 +65,8 @@ public static function baseFieldDefinitions(EntityTypeInterface $entity_type) {
$fields['id'] = FieldDefinition::create('integer')
->setLabel(t('ID'))
->setDescription(t('The ID of the test entity.'))
->setReadOnly(TRUE);
->setReadOnly(TRUE)
->setSetting('unsigned', TRUE);
$fields['uuid'] = FieldDefinition::create('uuid')
->setLabel(t('UUID'))
......
......@@ -53,7 +53,8 @@ public static function baseFieldDefinitions(EntityTypeInterface $entity_type) {
$fields['revision_id'] = FieldDefinition::create('integer')
->setLabel(t('Revision ID'))
->setDescription(t('The version id of the test entity.'))
->setReadOnly(TRUE);
->setReadOnly(TRUE)
->setSetting('unsigned', TRUE);
$fields['default_langcode'] = FieldDefinition::create('boolean')
->setLabel(t('Default language'))
......
......@@ -58,7 +58,8 @@ public static function baseFieldDefinitions(EntityTypeInterface $entity_type) {
$fields['revision_id'] = FieldDefinition::create('integer')
->setLabel(t('Revision ID'))
->setDescription(t('The version id of the test entity.'))
->setReadOnly(TRUE);
->setReadOnly(TRUE)
->setSetting('unsigned', TRUE);
$fields['langcode']->setRevisionable(TRUE);
$fields['name']->setRevisionable(TRUE);
......
......@@ -111,7 +111,8 @@ public static function baseFieldDefinitions(EntityTypeInterface $entity_type) {
$fields['tid'] = FieldDefinition::create('integer')
->setLabel(t('Term ID'))
->setDescription(t('The term ID.'))
->setReadOnly(TRUE);
->setReadOnly(TRUE)
->setSetting('unsigned', TRUE);
$fields['uuid'] = FieldDefinition::create('uuid')
->setLabel(t('UUID'))
......@@ -150,7 +151,8 @@ public static function baseFieldDefinitions(EntityTypeInterface $entity_type) {
->setDescription(t('The parents of this term.'))
// Save new terms with no parents by default.
->setSetting('default_value', 0)
->setConstraints(array('TermParent' => array()));
->setSetting('unsigned', TRUE)
->addConstraint('TermParent', array());
$fields['changed'] = FieldDefinition::create('changed')
->setLabel(t('Changed'))
......
......@@ -448,7 +448,8 @@ public static function baseFieldDefinitions(EntityTypeInterface $entity_type) {
$fields['uid'] = FieldDefinition::create('integer')
->setLabel(t('User ID'))
->setDescription(t('The user ID.'))
->setReadOnly(TRUE);
->setReadOnly(TRUE)
->setSetting('unsigned', TRUE);
$fields['uuid'] = FieldDefinition::create('uuid')
->setLabel(t('UUID'))
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment