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